1、结构体
typedef struct Node
{
int data;//数据域
struct Node* next;//后继指针
}Node,*List;
注意:单链表最后一个节点的next域未NULL
2、头插(重点)
//头插,考试重点
bool Insert_head(List plist, int val)
{
assert(plist != NULL);
if (plist == NULL)
return false;
//动态申请节点
Node* p = (Node*)malloc(sizeof(Node));
assert(plist != NULL);
//将数据放入新节点
p->data = val;
//插入新节点
p->next = plist->next;//先连接后面的节点
plist->next = p;
}
3、尾插
//尾插
bool Insert_tail(List plist, int val)//时间复杂度:O(n)
{
assert(plist != NULL);
if (plist == NULL)
return false;
//动态申请节点
Node* p = (Node*)malloc(sizeof(Node));
assert(plist != NULL);
//将数据放入新节点
p->data = val;
//找尾巴
Node* q;
for (q = plist; q->next != NULL; q = q->next)
{
;
}
//插入新节点
p->next = q->next;
q->next = p;
}
4、在指定位置插入数据
//插入数据,在plist链表的pos位置插入val;
bool Insert(List plist, int pos, int val)
{
if (pos < 1 || pos >= Getlength(plist))
{
return false;
}
Node* p = plist;
int j = 0;
//获得前结点
while (p != NULL && j < pos - 1)
{
p = p->next;
j++;
}
if (p == NULL)
{
return false;
}
//插入新结点
Node* q = (Node*)malloc(sizeof(Node));
q->data = val;
q->next = p->next;
p->next = q;
return true;
}
5、删除指定位置的数据
//删除pos位置的值
bool DelPos(List plist, int pos)
{
assert(plist != NULL);
if (plist == NULL)
return NULL;
if (pos < 1 || pos >= Getlength(plist))
{
return false;
}
Node* p = plist;
int j = 0;
//获得前结点
while (p != NULL && j < pos - 1)
{
p = p->next;
j++;
}
if (p == NULL)
{
return false;
}
Node* q = p->next;
p->next = p->next->next;
free(q);
return true;
}
6、删除指定数据(重点)
//删除第一个val的值,考试重点
bool DelVal(List plist, int val)
{
//获得前驱
Node* p = plist;
Node* q = NULL;
while (p->next != NULL)
{
if (p->next->data == val)
{
q = p->next;//保存被删结点的地址
p->next = p->next->next;
break;
}
p = p->next;
}
//释放
if (q != NULL)
free(q);
return 0;
}
完成代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include <string.h>
#include<ctype.h>
#include<assert.h>
#include<stdlib.h>
typedef struct Node
{
int data;//数据域
struct Node* next;//后继指针
}Node,*List;
//初始化
void InitList(List plist)
{
//头结点的数据域不使用
assert(plist != 0);//参数判断
if (plist == NULL)
return;
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(plist != NULL);
//将数据放入新节点
p->data = val;
//插入新节点
p->next = plist->next;//先连接后面的节点
plist->next = p;
}
//尾插
bool Insert_tail(List plist, int val)//O(n)
{
assert(plist != NULL);
if (plist == NULL)
return false;
//动态申请节点
Node* p = (Node*)malloc(sizeof(Node));
assert(plist != NULL);
//将数据放入新节点
p->data = val;
//找尾巴
Node* q;
for (q = plist; q->next != NULL; q = q->next)
{
;
}
//插入新节点
p->next = q->next;
q->next = p;
}
//获取数据节点的个数
int Getlength(List plist)
{
int count = 0;
assert(plist != NULL);
if (plist == NULL)
return -1;
for (Node* p = plist->next; p != NULL; p = p->next)
count++;
return count;
}
//插入数据,在plist链表的pos位置插入val;
bool Insert(List plist, int pos, int val)
{
if (pos < 1 || pos >= Getlength(plist))
{
return false;
}
Node* p = plist;
int j = 0;
//获得前结点
while (p != NULL && j < pos - 1)
{
p = p->next;
j++;
}
if (p == NULL)
{
return false;
}
//插入新结点
Node* q = (Node*)malloc(sizeof(Node));
q->data = val;
q->next = p->next;
p->next = q;
return val;
}
//判空
bool IsEmpyt(List plist)
{
assert(plist != NULL);
if (plist == NULL)
return false;
return plist->next == NULL;
}
//在plist中查找第一个key值,找到返回节点地址,没有找到返回NULL
Node* Search(List plist, int key)
{
assert(plist != NULL);
if (plist == NULL)
return NULL;
for (Node* p = plist->next; p->next != 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 NULL;
if (pos < 1 || pos >= Getlength(plist))
{
return false;
}
Node* p = plist;
int j = 0;
//获得前结点
while (p != NULL && j < pos - 1)
{
p = p->next;
j++;
}
if (p == NULL)
{
return false;
}
Node* q = p->next;
p->next = p->next->next;
free(q);
return true;
}
//删除第一个val的值,考试重点
bool DelVal(List plist, int val)
{
//获得前驱
Node* p = plist;
Node* q = NULL;
while (p->next != NULL)
{
if (p->next->data == val)
{
q = p->next;//保存被删结点的地址
p->next = p->next->next;
break;
}
p = p->next;
}
//释放
if (q != NULL)
free(q);
return 0;
}
//返回key的前驱地址,如果不存在返回NULL
Node* GetPrio(List plist, int key)
{
assert(plist != NULL);
if (plist == NULL)
return NULL;
for (Node* p = plist->next; p->next != NULL; p = p->next)
{
if (p->next->data == key)
return p;
}
return NULL;
}
//返回key的后继地址,如果不存在返回NULL
Node* GetNext(List plist, int key)
{
Node* p = Search(plist, key);
return p->next;
}
//输出
void Show(List plist)
{
assert(plist != NULL);
if (plist == NULL)
return;
for (Node* p = plist->next; p != NULL; p = p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
//销毁整个内存
void Destroy(List plist)
{
Node* p;
while (plist->next != NULL)
{
p = plist->next;
plist->next = plist->next->next;
free(p);
}
}
//清空数据
void Clear(List plist)
{
Destroy(plist);
}
int main()
{
Node head;
InitList(&head);
for (int i = 0; i < 20; i++)
{
//Insert_head(&head, i);
Insert_tail(&head, i);
}
int len = Getlength(&head);
printf("len=%d\n", len);
Show(&head);
Node* p = Search(&head, 10);
if (p != NULL)
printf("%d\n", p->data);
else
printf("没有找到\n");
DelVal(&head, 12);
Show(&head);
Insert(&head, 2, 100);
Show(&head);
DelPos(&head, 6);
Show(&head);
Node* s = GetNext(&head, 16);
if (s != NULL)
printf("%d\n", s->data);
else
printf("没有找到\n");
Node* w = GetPrio(&head, 100);
if (w != NULL)
printf("%d\n", w->data);
else
printf("没有找到\n");
Destroy(&head);
Show(&head);
return 0;
}
总结
头插,头删 时间复杂度是O(1)
尾插,尾删 时间复杂度是O(n)
标签:Node,单链,return,int,next,数据结构,NULL,plist From: https://blog.csdn.net/quite123_/article/details/140175784P初始化成什么?
如果我们要修改表的结构(或者说依赖于前驱,比如插入,删除):遍历:
for(Node *p=plist;p->next!=NULL;p=p->next)如果我们不修改表的结构(或者说不依赖于前驱,比如求长度,打印,查找) :遍历:
for(Node*p=plist->next;p!= NULL;p=p->next)