單鏈表定義:
1、表中的資料是以結點來表示的,每個結點的構成:元素(資料元素的映象) + 指標(指示后繼元素存盤位置),元素就是存盤資料的存盤單元,指標就是連接每個結點的地址資料,
2、鏈表的結點結構
┌───┬───┐
│data│next│
└───┴───┘
data域–存放結點值的資料域
next域–存放結點的直接后繼的地址(位置)的指標域(鏈域),
單鏈表的基本操作:
1.頭檔案 資料型別準備
2.單鏈表的初始化
3.單鏈表的顯示
4.單鏈表的頭插法
5.單鏈表的插入方法
6.單鏈表的洗掉
7.單鏈表的測驗
8.原始碼
1.頭檔案 資料型別準備
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <malloc.h>
#include<string.h>
//鏈表結點個數
int length = 0;
//定義結點
struct Node
{
char student[10];//為了方便測驗 資料域中放入學生的姓名
Node* next; //指標域
}
2.單鏈表的初始化
struct Node* createList()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//為頭結點分配一塊記憶體空間
headNode->next = nullptr;//初始化頭結點
return headNode;
}
headNode->next = nullptr;
有很多初學的小伙伴就會問了為什么要讓頭結點的指標域指向空指標?
因為在初始化指標的時候,如果指標沒有指向的內容,就要讓他指向空指標,否則就產生“野指標”,
3.單鏈表的顯示
void printf_List(struct Node* head)
{
struct Node* move = head;
while (move->next)//當move不為最后一個結點時,繼續向下遍歷
{
move = move->next;
printf("%s", move->student);
printf("\n");
}
}
4.單鏈表頭插法
void head_insert(struct Node* head)
{
struct Node *newNode= (struct Node*)malloc(sizeof(struct Node));//為新結點開辟記憶體空間
char student1[10];
scanf("%s", student1);//插入的學生姓名
newNode->next = head->next;
head->next = newNode;
newNode->next = nullptr;
strcpy(newNode->student, student1);
length++;//結點數量+1
}
小伙伴們看到這三行代碼是可能有點懵
但是不要緊,畫個圖就好了
newNode->next = head->next;
head->next = newNode;
newNode->next = nullptr;

按照鏈表的邏輯來看
新結點的指標應該指向頭結點所指向的結點 , 對應的代碼就是newNode->next = head->next;
頭結點的指標應該指向新結點, 對應的代碼就是第二句head->next = newNode
最后讓新結點的指標指向空指標newNode->next = nullptr;
注意了家人們!!!!前兩句代碼的順序不能調換!!!
來分析一下為什么不能調換
head->next = newNode;
newNode->next = head->next;
第一步執行完head->next = newNode;頭結點的指向就已經是新結點了,而原來頭結點所指向的結點內容就找不到了,所以肯定是不行的
5.單鏈表的插入方法
void insert(struct Node* head)
{
int a = 0;
char student1[10];
struct Node* move = head;
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
printf_List(head);
printf("請輸入你要插入的位置");
scanf("%d", &a);
for (int i = 0; i < a; i++)
{
move = move->next;
}
newNode->next = move->next;
move->next = newNode;
printf("請輸入你要插入的內容");
scanf("%s", student1);
strcpy(newNode->student, student1);
length++;
}
使用for回圈移動指標到要插入的位置
指標的指向部分與頭插法相似,請大家開動腦筋自己思考思考(太懶了不想寫了)
6.單鏈表的洗掉
void delete_(struct Node* head)
{
char student1[10];
struct Node* move = head;
printf("請輸入你要洗掉的資料");
scanf("%s", student1);
while (move->next)
{
if (0 == strcmp(move->next->student, student1))//如果二個字串相等 回傳0
{
Node* temp = move->next;
move->next = move->next->next;
free(temp);
length--;
return;
}
move=move->next;
}
printf("沒有找到該資料");
}
分析一下

while回圈中的幾句代碼
上圖
Node* temp = move->next;先將要洗掉的結點地址用temp保存下來,
move->next = move->next->next;使被洗掉結點上一個結點指向被洗掉結點的下一個結點(說的反而不清楚了,看圖理解吧)
free(temp);最后將被洗掉結點的地址釋放掉,
7.代碼測驗
int main()
{
Node* List;
List = createList();
head_insert(List);
insert(List);
printf_List(List);
delete_(List);
printf_List(List);
return 0;
}

最后附上原始碼
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <malloc.h>
#include<string.h>
int length = 0;
struct Node
{
char student[10];
Node* next;
};
struct Node* createList()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->next = nullptr;
return headNode;
}
void printf_List(struct Node* head)
{
printf("學生資訊:\n");
struct Node* move = head;
while (move->next)
{
move = move->next;
printf("%s", move->student);
printf("\n");
}
}
void head_insert(struct Node* head)
{
char student1[10];
printf("請輸入你要插入的內容\n");
scanf("%s", student1);
struct Node *newNode= (struct Node*)malloc(sizeof(struct Node));
newNode->next = head->next;
head->next = newNode;
strcpy(newNode->student, student1);
newNode->next = nullptr;
length++;
}
void insert(struct Node* head)
{
int a = 0;
char student1[10];
struct Node* move = head;
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
printf_List(head);
printf("請輸入你要插入的位置\n");
scanf("%d", &a);
for (int i = 0; i < a; i++)
{
move = move->next;
}
newNode->next = move->next;
move->next = newNode;
printf("請輸入你要插入的內容\n");
scanf("%s", student1);
strcpy(newNode->student, student1);
length++;
}
void delete_(struct Node* head)
{
char student1[10];
struct Node* move = head;
printf("請輸入你要洗掉的資料\n");
scanf("%s", student1);
while (move->next)
{
if (0 == strcmp(move->next->student, student1))
{
Node* temp = move->next;
move->next = move->next->next;
free(temp);
length--;
return;
}
move=move->next;
}
printf("沒有找到該資料\n");
}
int main()
{
Node* List;
List = createList();
head_insert(List);
insert(List);
printf_List(List);
delete_(List);
printf_List(List);
return 0;
}
喜歡的家人們給個三連 !!!!

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/292719.html
標籤:其他
