建立单链表,并且从头到尾打印单链表


#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h> 

#include<stdlib.h>

#include<assert.h>

typedef int DataType;

typedef struct LinkNode

{

  DataType data;//定义节点的数据

  struct LinkNode *next;//保存下一个节点的地址

}LinkNode,*pList,*pLinkNode;//指向节点的指针

 

pLinkNode BuyNode(DataType x)

{

pLinkNode NewNode=(pLinkNode)malloc(sizeof(LinkNode));

NewNode->data=x;

NewNode->next=NULL;

}

 

void PushBack(pList *pHead,DataType x)

{

pLinkNode cur=*pHead;

pLinkNode NewNode=BuyNode(x);

    assert(pHead);

if(cur==NULL)

{

*pHead=NewNode;

return;

}

while(cur->next)

{

cur=cur->next;

}

cur->next=NewNode;

}

 

void InitLinkList(pList *pHead)

{

 assert(pHead);

(*pHead)->next=NULL;

}

void printList(pList list)

{

pLinkNode cur=list;

printf("list is:");

while(cur)

{

printf("%d->",cur->data);

cur=cur->next;

}

   printf("\n");

}

int main()

{

pList myList;

InitLinkList(&myList);

PushBack(&myList, 1);

PushBack(&myList, 2);

PushBack(&myList, 3);

PushBack(&myList, 4);

PushBack(&myList, 5);

PushBack(&myList, 6);

printList(myList);

system("pause");

return 0;

 

}