【数据结构】-单链表

1.单链表概念

在这里插入图片描述
链表是由一个一个的节点(结点)组成的

一个节点有两个部分组成,要存储的数据+指针(结构体指针)

2.节点的结构

typedef int SLDataType;
typedef struct SListNode{
SLDataType data;
struct ListNode *next;
}SLNode;```

申请空间:#include<stdlib.h>
不存在扩容:malloc

3.链表的打印

在这里插入图片描述
在这里插入图片描述

3.1 链表申请节点
void BuyNode(SLNode **pphead,SLDataType x){
while(ptail->next==NULL){
SLNode *newnode=(SLNode*)malloc(sizeof(SLNode));
if(patil->next==newnode){
printf("malloc failed");
exit(-1);
}
}


}
3.2链表的尾插

在这里插入图片描述
对一级指针取地址,必须用二级指针来接受一级指针的地址

void PushBack(SLNode**pphead,SLDataType x){
assert(pphead);
SLNode *newnode=BuyNode(x);
//链表为空,新节点作为pphead
if(*pphead==NULL){
*pphead=newnode;
return ;
}
//链表不为空
SLNode*ptail=*pphead;
while(ptail->next){
ptail=ptail->next;
}
patil->next=newnode;
}
3.3链表的头插

在这里插入图片描述

3.4链表的尾删
void PopBack(ListNode**pphead){
assert(pphead);
assert(*pphead);
if((*pphead)->next==NULL){
free(*pphead);
*pphead=NULL;
}
ListNode *prev=NULL;
ListNode*ptail=*pphead;
while(ptail->next){
prev=ptail;
ptail=ptail->next;
}
prev->next=NULL;
//销毁尾节点
free(ptail);
ptail=NULL;
}
3.5链表的头删

在这里插入图片描述

void PopFront(ListNode**pphead){
assert(pphead);
assert(*pphead);//链表不能为空
ListNode *cur=*pphead;//让第二个节点成为新的头
*pphead=(*pphead)->next;//将旧的头释放掉
free(cur);
}
3.6链表的查找
LNode* FindLNode(LNode **pphead,SLDataType x){
assert(pphead);
LNode *pcur=*pphead;
while(pcur){
if(pcur->data==x){
return pcur;
}
pcur=pcur->next;
}//没有找到
return NULL;
} 
3.7在指定位置之前插入数据
void Insert(LNode*pphead,LNode* pos,SLDataType x){
assert(pphead);
assert(pos);
LNode*newnode=BuyNode(x);
if(pos==*pphead){
PushFront(pphead,x);
return ;
}
LNode*prev=*pphead;
while(prev->next!=pos){
prev=prev->next;
}
prev->next=newnode;
newnode->next=pos;
}
3.8在指定位置之后插入数据

在这里插入图片描述

void InsertAfter(LNode* pos,SLDataType x){
assert(pphead);
assert(pos);
LNode*newnode=BuyNode(x);
LNode*ptail=*pphead;
newnode->next=ptail;
pos->next=newnode;
}
3.9删除pos节点
void LErase(LNode*pphead,LNode* pos){
assert(pphead);
assert(*pphead);
assert(pos);
if(pos==*pphead){
PopFront(pphead);
return;
}
LNode*prev=*pphead;
while(prev->next!=pos){
prev=prev->next;
}
prev->next=pos->next;
free(pos);
pos=NULL;
}

在这里插入图片描述

3.10删除pos之后的节点
void EraseAfterPos(LNode*pphead,LNode* pos){
assert(pos);
asssert(pos->next);
LNode*del=pos->next;
pos->next=pos->next->next;
free(del);
del=NULL;
}
3.11链表的销毁
void Destory(LNode*pphead){
assert(pphead);
assert(*pphead);
 LNode*pcur=*pphead;
 while(pcur){
SLNode* next=pcur->next;
free(pcur);
pcur=next;
}
*pphead=NULL;
}

4.链表的结构

单链表:不带头单向不循环链表

一共有8种链表

单链表中提到的“头节点”是第一个有效的节点

带头结点的“头”是无效的节点

在这里插入图片描述
在这里插入图片描述

虽然链表的种类非常之多,但是使用比较多的只有两种:单链表(不带头单向不循环链表)和双向链表(带头双向循环链表)

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用C语言实现的单链表原地逆转的代码: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void reverseList(struct Node** head) { struct Node* prev = NULL; struct Node* current = *head; struct Node* next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *head = prev; } void printList(struct Node* head) { while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } void push(struct Node** headRef, int newData) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = newData; newNode->next = *headRef; *headRef = newNode; } int main() { struct Node* head = NULL; push(&head, 3); push(&head, 2); push(&head, 1); printf("Original list: "); printList(head); reverseList(&head); printf("Reversed list: "); printList(head); return 0; } ``` 在上述代码中,我们首先定义了一个 `Node` 结构体来表示单链表中的每个节点,包括节点的值和指向下一个节点的指针。然后我们定义了 `reverseList` 函数来实现原地逆转单链表的功能。该函数接受一个指向指针的指针 `head`,这是因为我们需要通过指针来修改单链表的头节点,所以我们传递指向指针的指针。在函数内部,我们使用三个指针 `prev`、`current` 和 `next` 来依次遍历单链表,并将每个节点的指针指向前一个节点,从而实现原地逆转单链表的目的。 最后,我们定义了一个 `push` 函数来添加新节点到单链表的头部,并定义了 `printList` 函数来打印单链表中所有节点的值。在 `main` 函数中,我们创建了一个包含三个节点的单链表,并调用 `reverseList` 函数来原地逆转该单链表,最后打印出原始和逆转后的单链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值