文章目錄
- 一、思維導圖
- 二、單鏈表代碼
- head.h
- text.c
- main.c
- 現象
一、思維導圖
二、單鏈表代碼
head.h
#ifndef __HEAD_H__
#define __HEAD_H__#include <stdlib.h>
#include <stdio.h>
#include <string.h>enum A
{FAULSE=-1,//失敗返回SUCCESS//成功返回};//給普通節點的數據域類型起別名
typedef int datatype;//定義節點的結構體
typedef struct Node
{//數據域union{datatype data;//普通元素的數據域int len;//頭結點的數據元素:數據長度};//指針域:存儲下一個節點的地址struct Node *next;
}*linklist;//單鏈表的節點創建
linklist creat_node(int flag);
//頭插
int insert_head(linklist head,datatype element);
//輸出
int output(linklist head);
//尾插
int insert_end(linklist head,datatype element);
//頭刪
int delate_head(linklist head);
//尾刪
int delate_end(linklist head);
//按位置查找
int search_place(linklist head,int place);
//按位置刪除
int delate_place(linklist head,int place);
//按位置插入
int insert_place(linklist head,int place,datatype element);
//按位置修改
int change_place(linklist head,int place,datatype element);
//按元素查找
int search_number(linklist head,datatype element);
//按元素修改
int change_number(linklist head,datatype element,datatype number);
//按元素刪除
int delate_number(linklist head,datatype element);
//逆置
int reverse(linklist head);
//找倒數第n個節點
int find_n(linklist head,int n);
//單鏈表釋放內存
int my_free(linklist head);
//單鏈表排序
int bubble(linklist head);#endif
text.c
#include "head.h"//單鏈表的節點創建
linklist creat_node(int flag)
{linklist s=(linklist)malloc(sizeof(struct Node));if(s==NULL){return NULL;}//內存申請成功//flag==1給頭結點初始化if(flag==1){s->len=0;}//flag==0給普通節點初始化else if(flag==0){s->data==0;}//初始化指針域s->next=NULL;return s;
}//頭插
int insert_head(linklist head,datatype element)
{//判斷頭結點是否存在if(head==NULL){return FAULSE;}//創建節點linklist s=creat_node(0);if(s==NULL){return FAULSE;}//節點創建成功s->data=element;//對新節點的數據域賦值//插入新節點s->next=head->next;head->next=s;//鏈表長度加1head->len++;return SUCCESS;
}//輸出
int output(linklist head)
{//判斷head是否為null//判斷內容是否為空if(head==NULL||head->len==0){return FAULSE;}linklist L=head;/*for(int i=0;i<head->len;i++){printf("%d ",L->next->data);L=L->next;}*/while(L->next!=NULL){printf("%d ",L->next->data);L=L->next;}putchar(10);return SUCCESS;
}//尾插
int insert_end(linklist head,datatype element)
{//判斷頭結點是否存在if(head==NULL){return FAULSE;}linklist end=head;while(end->next!=NULL)//尋找尾節點{end=end->next;}//創建尾節點linklist s=creat_node(0);//輸入尾節點的值s->data=element;//讓尾節點指向新的尾節點end->next=s;head->len++;return SUCCESS;}//頭刪
int delate_head(linklist head)
{//判斷頭結點是否存在//判斷頭結點是否為空if(head==NULL||head->next==NULL){return FAULSE;}//標記要刪除的節點linklist index=head->next;//讓頭結點指向要刪除節點的下一個節點head->next=index->next;//釋放刪除節點的內存free(index);//防止野指針index=NULL;head->len--;return SUCCESS;
}//尾刪
int delate_end(linklist head)
{if(head==NULL||head->next==NULL){return FAULSE;}//定義尾節點的上一個節點linklist index=head;while(index->next->next!=NULL){index=index->next;}//刪除尾節點free(index->next);index->next=NULL;head->len--;return SUCCESS;
}//按位置查找
int search_place(linklist head,int place)
{//判斷頭結點是否存在//判斷位置是否合法if(head==NULL||place<1||place>head->len){return FAULSE;}linklist p=head;for(int i=0;i<place;i++){p=p->next;}printf("第%d個元素的數值為%d\n",place,p->data);return SUCCESS;
}//按位置刪除
int delate_place(linklist head,int place)
{//判斷頭結點是否存在//判斷位置是否合法if(head==NULL||place<1||place>head->len){return FAULSE;}linklist p=head;for(int i=0;i<place-1;i++){p=p->next;}linklist L=p->next;p->next=L->next;free(L);L->next=NULL;head->len--;return SUCCESS;}//按位置插入
int insert_place(linklist head,int place,datatype element)
{//判斷頭結點是否存在//判斷位置是否合法if(head==NULL||place<1||place>head->len+1){return FAULSE;}//尋找插入節點前一個節點linklist p=head;for(int i=0;i<place-1;i++){p=p->next;}//創建節點linklist L=creat_node(0);if(L==NULL)return FAULSE;//傳值L->data=element;L->next=p->next;p->next=L;head->len++;return SUCCESS;}//按位置修改
int change_place(linklist head,int place,datatype element)
{//判斷頭結點是否存在//判斷位置是否合法if(head==NULL||place<1||place>head->len){return FAULSE;}linklist p=head;for(int i=0;i<place;i++){p=p->next;}p->data=element;return SUCCESS;
}//按元素查找
int search_number(linklist head,datatype element)
{//判斷節點是否存在if(head==NULL){return FAULSE;}linklist p=head;int pos=0;while(p!=NULL){if(p->data==element){return pos;}pos++;p=p->next;}return FAULSE;
}//按元素修改
int change_number(linklist head,datatype element,datatype number)
{int pos=search_number(head,element);if(pos==FAULSE)return FAULSE;change_place(head,pos,number);return SUCCESS;
}//按元素刪除
int delate_number(linklist head,datatype element)
{int pos=search_number(head,element);if(pos==FAULSE)return FAULSE;delate_place(head,pos);return SUCCESS;}
//逆置
int reverse(linklist head)
{//判斷頭結點是否存在//判斷其他節點是0個或1個if(NULL==head||head->len<=1){return FAULSE;}//逆置linklist p=head->next;head->next=NULL;for(int i=0;i<head->len;i++){linklist temp=p;p=p->next;//把temp頭插temp->next=head->next;head->next=temp;}return SUCCESS;
}//找倒數第n個節點
int find_n(linklist head,int n)
{if(head==NULL||n<1||n>head->len){return FAULSE;}linklist p=head;linklist q=head;for(int i=0;i<n;i++){q=q->next;}while(q!=NULL){p=p->next;q=q->next;}printf("倒數第%d個節點的值為%d\n",n,p->data);return SUCCESS;
}//單鏈表釋放內存
int my_free(linklist head)
{if(head==NULL){return FAULSE;}int len=head->len;for(int i=0;i<len;i++){delate_end(head);}free(head);
}//單鏈表排序
int bubble(linklist head)
{if(NULL==head||head->len<=1){return FAULSE;}for(int i=0;i<head->len-1;i++){int flag=0;linklist p=head->next;for(int j=0;j<head->len-1-i;j++){if(p->data>p->next->data){datatype temp=p->data;p->data=p->next->data;p->next->data=temp;flag=1;}p=p->next;}if(flag==0){break;}}return SUCCESS;
}
main.c
#include "head.h"
int main(int argc, const char *argv[])
{//創建頭結點linklist head=creat_node(1);//頭插insert_head(head,15);insert_head(head,13);insert_head(head,12);insert_head(head,21);insert_head(head,19);insert_head(head,12);insert_head(head,54);insert_head(head,14);printf("頭插后的結果:\n");output(head);//尾插insert_end(head,11);printf("尾插后的結果:\n");output(head);//按位置刪除delate_place(head,3);printf("按位置刪除后的結果:\n");output(head);//按位置查找search_place(head,2);//頭刪delate_head(head);printf("頭刪后的結果:\n");output(head);//按位置插入insert_place(head,2,14);printf("按位置插入的結果:\n");output(head);//按位置修改change_place(head,2,98);printf("按位置修改后的結果:\n");output(head);//尾刪delate_end(head);printf("按尾刪后的結果:\n");output(head);//逆置reverse(head);printf("逆置后的結果:\n");output(head);//按元素查找int pos=search_number(head,13);printf("查找到元素為第%d個\n",pos);//按元素修改change_number(head,15,99);printf("按元素修改后的結果:\n");output(head);//按元素刪除delate_number(head,54);printf("按元素刪除后的結果:\n");output(head);//找倒數第n個節點find_n(head,2);//單鏈表排序bubble(head);printf("排序之后的結果:\n");//輸出output(head);//單鏈表釋放內存my_free(head);head=NULL;return 0;
}
現象
頭插后的結果:
14 54 12 19 21 12 13 15
尾插后的結果:
14 54 12 19 21 12 13 15 11
按位置刪除后的結果:
14 54 19 21 12 13 15 11
第2個元素的數值為54
頭刪后的結果:
54 19 21 12 13 15 11
按位置插入的結果:
54 14 19 21 12 13 15 11
按位置修改后的結果:
54 98 19 21 12 13 15 11
按尾刪后的結果:
54 98 19 21 12 13 15
逆置后的結果:
15 13 12 21 19 98 54
查找到元素為第2個
按元素修改后的結果:
99 13 12 21 19 98 54
按元素刪除后的結果:
99 13 12 21 19 98
倒數第2個節點的值為19
排序之后的結果:
12 13 19 21 98 99