单链表的各种操作整合(C语言版)
关于昨天的vs code C/C++环境配置要补充一点
就是MInGW 只能适配英文环境,
就是说文件保存路径、文件名、和里面的代码(除了注释)之外都是英文的
下载路径https://github.com/SDchao/AutoVsCEnv_WPF/releases/latest
复制到Chrome 或者Microsoft Edge 食用为佳
今日用了一下VS Code 写代码
确实比Visual Studio 和Code::Blocks舒服(但还是好累!)
今天的内容是:
单链表的各种操作的代码,整合成一个微小的程序
因为不能编译中文,所以有些句子就用英文勉强凑合hh
单链表是c语言入门的结业课
也是数据结构(c语言版)的 南天门
参考:刘畅主编,上海交通大学出版社出版的《数据结构(c语言版)》
以及感谢王岚老师的帮助
中国移动
16:21
实名感谢
下面就开始源代码:
输出的风格是很辣眼睛
主要是掌握关键步骤
//单链表的应用,数上的应用程序;//线性表子系统#include <stdlib.h>#include<stdio.h>typedef int DateType;typedef struct Linknode{DateType data;struct Linknode *next;}LinkList; //修改Linknode的名字;LinkList *InitList() //初始化单链表,把head设置为空内容{LinkList *head;head=(LinkList*)malloc(sizeof(LinkList));head->next=NULL;return head;}void CreatListH(LinkList *head,int n)//头插法创建单链表{LinkList *s;int i;printf("Please input %d int numbers",n);for (i=0;i<n;i++){s=(LinkList*)malloc(sizeof(LinkList));scanf("%d",&s->data);s->next=head->next; //最重要的两步,需要仔细理解head->next=s; //头插法的最终奥义是:头指针head始终处于最后一个输入的数据上}printf("Success to create a LinkList!");}void CreateListL(LinkList *head,int n) //尾插法创建单链表{LinkList *s,*tail; //因为head是不能移动的,所以需要引进一个tail指针一直存在数据的尾部int i;tail=head;printf("Please input %d int numbers",n);for (i=0;i<n;i++){s=(LinkList*)malloc(sizeof(LinkList));scanf("%d",&s->data);s->next=NULL; //常常忽略,但很重要tail->next=s; //尾插法的奥义是:链表后面始终加了个尾巴,而这个尾巴可以吸引(连接)新的数据进入链表中tail=s; //尾巴一直都要在尾部.}printf("Success to create a LinkList!");}int LengthList(LinkList *head) //求链表的表长,挨个遍历一下,再计数就可以了{LinkList *p=head->next;int j=0;while(p!=NULL){p=p->next;j++;}return j;}void Locate(LinkList *head,DateType x) //输入元素的数据寻找元素的所在位置{int j=1;LinkList*p;p=head->next; //由于需要一个一个遍历来查找,所以需要引进新的指针while (p!=NULL && p->data!=x) //如果既没有找到,也没有找完 的时候进行循环遍历{p=p->next;j++;}if(p!=NULL) //如果找到了{printf("%d is in the list of %d position",x,j);}else //如果找完了,还没找到{printf("I don't find the number of %d ",x);}}void SearchList(LinkList *head,int i) //寻找单链表某一个位置上面的元素数据。{LinkList *p; //寻找就是要遍历,遍历就是要新指针int j=0;p=head;if(i>LengthList(head)) //如果输入的位置,超出了链表的长度{printf("fail to find,because the %d is more than the List Length!",i);}while(p->next!=NULL && j<i) //在没有找完和没有找到的情况下遍历{p=p->next;j++;}if(j==i) //好不容易找到了{printf("the position %d ,which number is %d",i,p->data);}else //找了半天找不到{printf("fail to find!");}}void InsertList(LinkList *head, int i ,DateType x) //插入一个元素{int j=0;LinkList *p,*s; //还是,寻找位置需要遍历,遍历就要指针p=head;while(p->next!=NULL && j<i-1) //插入的时候是需要找到插入位置的前一个结点,用来对接。{ //在位置不是末尾和位置前一个结点的情况下遍历p=p->next;j++;}if(p!=NULL) //如果 ,这个位置的前一个结点不是空的(有的人输入一个超过链表长度+1的数值,就很离谱){s=(LinkList*)malloc(sizeof(LinkList));s->data=x; //把这个数据先放在s指针里面s->next=p->next; //s后一个指针和s进行对接(尾巴对接)p->next=s; // 然后用前一个指针和后一个指针进行对接(脑子对接)printf("Success to insert!!");}else{printf("fail to insert!!");}}void DeleteList(LinkList *head,int i) // 删除单链表某一个位置的操作{int j=0;LinkList *s,*p; //s指针的目的是标定那个要删除的结点,p。。不想说了DateType x;p=head;while (p->next!=NULL && j<i-1) //遍历,找到s前面那个结点{p=p->next;j++;}if(p->next!=NULL && j==i-1) //已经到了那个位置,p的目的是标定s前面的一个结点{s=p->next; //把p后面的一个结点定义为s指针x=s->data; //储存一下s指针里面的值p->next=s->next; //p指针和s后面的那个指针对接free(s); //s没有用了,要释放掉printf("success to delete the postion of %d statistic %d",i,x);}else{printf("fail to delete ,because the position is fault!");}}void Display(LinkList *head) // 打印单链表的数值{LinkList *p;p=head->next; //遍历单链表,依次输出,没难度while(p!=NULL){printf("%6d",p->data);p=p->next;}}void MenuLine() //及其不好看的菜单门面{printf("\n THE LIST OF APPLICATION");printf("\n======================================================================");printf("\n| 1 BUILD A LIST |");printf("\n| 2 INSERT A NUMBER |");printf("\n| 3 DELETE A NUMBER |");printf("\n| 4 FIND BY POSITION |");printf("\n| 5 FIND BY DATA |");printf("\n| 6 LENGTH OF LIST |");printf("\n| 0 BACK |");printf("\n======================================================================");printf("\n YEZIPENG TO DO \n");}int main(){LinkList *head;DateType x;int i,n;char ch1,ch2,a;ch1='y';while(ch1=='y'||ch1=='Y') //如果输入的是y,那,快活{MenuLine();scanf("%c",&ch2); // 这就是0~6随便输getchar();switch(ch2){case '1':head=InitList();printf("please input the length number of List:");scanf("%d",&n);CreateListL(head,n);printf("The end of List is :\n");Display(head);break;case '2':printf("Please input which position would you want to insert:");scanf("%d",&i);getchar();printf("How much would you want to insert");scanf("%d",&x);InsertList(head,i,x);printf("The end of List is :\n");Display(head);break;case '3':printf("Please input which position would you want to delete:");scanf("%d",&i);DeleteList(head,i);printf("The end of List is :\n");Display(head);break;case '4':printf("Please input which position would you want to find:");scanf("%d",&i);SearchList(head,i);printf("The end of List is :\n");Display(head);break;case '5':printf("Please input the number of int you want to find :");scanf("%d",&x);Locate(head,i);break;case '6':printf("the List of Length is %d\n",LengthList(head));break;case '0':ch1='n';break;default:printf("fail to input ,change it "); //有的人居心叵测,输入不对,想浪费内存}if(ch2!='0') //{printf("\npress Enter is keep on,and you can press any other button to out\n");a=getchar(); //输入一个。。回车或其他if(a!='\xA') //换行的ASCII码是10,就相当于是回车{getchar();ch1='n'; //不按回车,咱就结束}}}}
247行代码
写了俩小时
加油
欢迎大家私信我一起探讨问题
我的Popi:
https://www.popiask.cn/7QZ4Ms
进入这个网站后,你所有的提问都将是匿名的
