您的位置:首页 > 其它

带头节点的单链表

2015-09-20 16:10 253 查看
#pragma once
 #include
<stdio.h>
 #include
<assert.h>
 #include
<malloc.h>
  
 typedef
int DataType;
  
 typedef
struct Node
 {
 DataType data;
 struct Node* next;
 }Node;
  
 typedef
struct List
 {
 Node* head; // 指向单链表的头
 size_t size;
 }List;
  
 Node* _CreateNode(DataType x)
 {
 Node* tmp = (Node*)malloc(sizeof(Node));
 tmp->data = x;
 tmp->next = NULL;
  
 return tmp;
 }
  
 void
InitList(List* pList)
 {
 assert(pList);
 pList->head = NULL;
 pList->size = 0;
 }
  
 void
PushBack(List* pList, DataType x)
 {
 assert(pList);
  
 if (pList->head ==
NULL)
 {
 pList->head = _CreateNode(x);
 }
 else
 {
 Node* end = pList->head;
 while(end->next !=
NULL)
 {
 end = end->next;
 }
  
 end->next = _CreateNode(x);
 }
  
 ++pList->size;
 }
  
 void
PrintList(List* pList)
 {
 Node* begin = pList->head;
 assert(pList);
  
 while (begin)
 {
 printf("%d->", begin->data);
 begin = begin->next;
 }
  
 printf("NULL\n");
 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息