一.单链表的设计
1.单链表的结构定义:
typedef struct Node{int data;//数据域struct Node* next;//后继指针
}Node,*List;
2.单链表的设计示意图:
3.注意,单链表的最后一个节点的next域为NULL;
4.为什么要有一个头节点?(简单方便,不用传二级指针);
二.单链表的实现
//初始化
void InitList(List plist)
{assert(plist != NULL);if (plist == NULL)return;//plist->data;//头节点的数据域不使用plist->next = NULL;
}//考试重点
//头插
bool Insert_head(List plist, int val)
{assert(plist != NULL);if (plist == NULL)return false;//动态申请一个节点Node* p = (Node *)malloc(sizeof(Node));assert(p != NULL);//将数据val放入到新节点p->data = val;//插入新节点p->next = plist->next;plist->next = p;return true;
}//考试重点
//尾插
bool Insert_tail(List plist, int val)
{assert(plist != NULL);if (plist == NULL)return false;//申请节点Node* p = (Node*)malloc(sizeof(Node));assert(p != NULL);//将数据val放入到新节点p->data = val;//找尾巴Node* q;for(q=plist;q->next!=NULL;q=q->next){;}//插入新节点p->next = q->next;//p->next=NULL;q->next = p;return true;
}//插入数据,在plist链表的pos位置插入val;
bool Insert(List plist, int pos, int val)
{assert(plist != NULL);if (plist == NULL)return false;if (pos<0 || pos>GetLength(plist)){return false;}Node* p = (Node*)malloc(sizeof(Node));assert(p != NULL);p->data = val;//找到位置Node* q;int i;for (q = plist, i = 0; i < pos; i++, q = q->next){;}// 插入p->next = q->next;q->next = p;return true;
}//判空
bool IsEmpty(List plist)
{assert(plist != NULL);if (plist == NULL)return false;return plist->next == NULL;
}//获取数据节点的个数
int GetLength(List plist)
{assert(plist != NULL);if (plist == NULL)return 0;int count = 0;for (Node* p = plist->next; p!= NULL; p = p->next){count++;}return count;}//在plist中查找第一个key值,找到返回节点地址,没有找到返回NULL;
Node* Search(List plist, int key)
{assert(plist != NULL);if (plist == NULL)return NULL;for (Node* p = plist->next; p != NULL; p = p -> next){if (p->data == key){return p;}}return NULL;
}//删除pos位置的值
bool DelPos(List plist, int pos)
{assert(plist != NULL);if (plist == NULL)return false;if (pos < 0 || pos >= GetLength(plist)){return false;}Node* p;int i;for (p = plist, i = 0; i < pos; i++, p = p->next){;}//删除p后面的节点Node* q = p->next;p->next = q->next;free(q);return true;
}//考试重点
//删除第一个val的值
bool DelVal(List plist, int val)
{Node* p = GetPrio(plist, val);if (p == NULL)return false;Node* q = p->next;//删除qp->next = q->next;//p->next=p->next->next;//释放qfree(q);return true;
}//返回key的前驱地址,如果不存在返回NULL;
Node* GetPrio(List plist, int key)
{for (Node* p = plist; p->next != NULL; p = p->next){if (p->next->data == key)return p;}return NULL;
}//返回key的后继地址,如果不存在返回NULL;
Node* GetNext(List plist, int key)
{assert(plist != NULL);if (plist == NULL)return NULL;Node* p = Search(plist, key);if (p == NULL)return NULL;return p->next;
}//输出
void Show(List plist)
{//注意,头节点不能访问datafor (Node* p = plist->next; p != NULL; p = p->next){printf("%d ", p->data);}printf("\n");
}//清空数据
void Clear(List plist)
{Destroy(plist);
}void Destroy(List plist)
{//总是删除第一个数据节点Node* p;while (plist->next != NULL){p = plist->next;plist->next = p->next;free(p);//error//plist->next = plist->next->next;//free(plist->next);}
}
三.单链表的总结
头插,头删 时间复杂度是O(1)
尾插,尾删 时间复杂度是O(n)
本篇完!