链式存储结构
含头结点
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LinkList,*LList;
//创建链表
LList CreatList()
{
LList L = (LList)malloc(sizeof(LinkList));
L->data = -1;
L->next = NULL;
return L;
}
//创建节点
LList CreatNode(ElemType data)
{
LList node = (LList)malloc(sizeof(LinkList));
node->data = data;
node->next = NULL;
return node;
}
//判断链表是否为空
int ListIsEmpty(LList L)
{
if(L->next == NULL)
{
return 1;
}
return 0;
}
//打印链表
void PrintList(LList L)
{
if(ListIsEmpty(L))
{
printf("链表为空,无法打印!\n");
exit(0);
}
LList moveNode = L->next;
while(moveNode)
{
printf("%d->",moveNode->data);
moveNode=moveNode->next;
}
printf("\n");
}
//头插法建立链表
void InsertNodeByHead(LList L, ElemType data)
{
LList newnode = CreatNode(data);
newnode->next = L->next;
L->next = newnode;
printf("节点--%d--插入成功!\n",data);
}
//尾插发建立链表
void InsertNodeByTail(LList L,ElemType data)
{
LList newnode = CreatNode(data);
while(L->next!=NULL)
{
L=L->next;
}
L->next=newnode;
printf("节点--%d--插入成功!\n",data);
}
/*反转链表:将链表中的数据提取出来逐个使用头插法建立新的链表*/
LList InvertedLIst(LList L)
{
if(ListIsEmpty(L))
{
printf("链表为空!\n");
exit(0);
}
LList invertedList = CreatList();
LList swap = L->next; //将链表的首节点给了swap
//当swap不为空时,进行头插法建立反转链表
while(swap!=NULL)
{
InsertNodeByHead(invertedList,swap->data);
swap=swap->next;
}
printf("反转成功!\n");
return invertedList;
}
int main()
{
return 0;
}