單鏈表各種操作的代碼實作
文章目錄
- 單鏈表各種操作的代碼實作
- SList.h:
- SList.c:
- Test.c:
今天簡簡單單的復習一下單鏈表的各種操作,
具體操作有:列印,尾插,頭插,尾刪,頭刪,在任意結點之前插入,洗掉任意結點,malloc一個新結點,在所給鏈表中查找資料x,并回傳它的結點等,
標注的比較詳細,可以借助注釋食用哦,
代碼要分為三個項:(全部在一個項中是不規范的)
SList.h:包的參考和函式的宣告
SList.c:各個操作的實作
SList.c:各個操作的實作
SList.h:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define _CRT_SECURE_NO_WARNINGS 1
typedef int SLTDataType;
struct SListNode
{
SLTDataType data; //鏈表的資料
struct SListNode* next; //鏈表的指標
};
typedef struct SListNode SLTNode;//改個名字
void SListPrint(SLTNode* phead);//列印
void SListPushBack(SLTNode** pphead,SLTDataType);//尾插
void SListPushFront(SLTNode** pphead, SLTDataType x);//頭插
void SListPopBack(SLTNode** pphead);//尾刪
void SListPopFront(SLTNode** pphead);//頭刪
void SListInsert(SLTNode** pphead, int pos, SLTDataType x);//在任意位置插入
void SListErase(SLTNode** pphead, int pos);//在任意位置洗掉
SLTNode* BuySListNode(SLTDataType x);//malloc一個結點
SLTNode* SListFind(SLTNode*phead, SLTDataType x);//在所給鏈表中查找資料x,并回傳它的結點
SList.c:
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
SLTNode* BuySListNode(SLTDataType x)//malloc一個結點
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPrint(SLTNode* phead)//列印
{
SLTNode* cur = phead;
if (phead != NULL)
{
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
else
{
printf("您輸入的鏈表為空");
}
}
void SListPushBack(SLTNode** pphead, SLTDataType x)//尾插
{
SLTNode* newnode = BuySListNode(x);
SLTNode* tail = *pphead;
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
//找尾節點的指標
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushFront(SLTNode** pphead, SLTDataType x)//頭插
{
SLTNode* newnode = BuySListNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SListPopFront(SLTNode** pphead)//頭刪
{
if (*pphead == NULL)
return;
else
{
SLTNode* tmp = *pphead;
*pphead = tmp->next;
free(tmp);
}
}
void SListPopBack(SLTNode** pphead)//尾刪
{
SLTNode* tmpt = *pphead;
if (*pphead == NULL)
{
return;
}
else if (tmpt->next == NULL)
{
free(*pphead);
*pphead = NULL;//千萬不要把==看成=!!
}
else
{
//SLTNode* tmpt2 = tmpt->next;
while (tmpt->next->next != NULL)
{
tmpt = tmpt->next;
}
free(tmpt->next);
tmpt->next = NULL;
}
}
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)//在任意結點之前插入資料
{
SLTNode* newnode = BuySListNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
newnode->next = NULL;
}
else if (pos == *pphead)
{
newnode->next = *pphead;
*pphead = newnode;
}
else
{
SLTNode* tmpt = *pphead;
while (tmpt->next != pos)
{
tmpt = tmpt->next;
}
tmpt->next = newnode;
newnode->next = pos;
}
}
SLTNode* SListFind(SLTNode* phead, SLTDataType x)//查找
{
SLTNode* cur = phead;
while (cur)
{
if (cur->data == x)
return cur;
else
cur = cur->next;
}
return NULL;
}
void SListErase(SLTNode** pphead, SLTNode* pos)//洗掉任意結點
{
SLTNode* cur = *pphead;
if (*pphead == NULL)
{
return;
}
else if(pos==*pphead)
{
*pphead = cur->next;
free(cur);
cur = NULL;
}
else
{
while (cur->next != pos)
{
cur = cur->next;
}
cur->next = pos->next;
free(pos);
}
}
Test.c:
#pragma once
#include"SList.h"
test1()
{
SLTNode* phead = NULL;;
SListPushBack(&phead, 1);
SListPushBack(&phead, 2);
SListPushBack(&phead, 3);
SListPrint(phead);
//想在1前面插入0.
SLTNode* pos = SListFind(phead, 1);//先找到1
if (pos != NULL)
{
SListInsert(&phead, pos, 0);
SListPrint(phead);
}
else
printf("沒有找到您要找的位置");
//想要洗掉鏈表中的2.
SLTNode* pos2 = SListFind(phead,2);//先找到2
if (pos2 != NULL)
{
SListErase(&phead, pos2);
SListPrint(phead);
}
else
{
printf("沒有找到您要洗掉的元素");
}
}
int main()
{
test1();
return 0;
}
覺得對自己有幫助的小伙伴可以點個贊哦
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/300244.html
標籤:其他
