赞
踩
可供操作
- printf("\n输入你要选择的命令\n");
- printf("1、展示一切信息\n");
- printf("2、插入\n");
- printf("3、销毁表\n");
- printf("4、初始化表\n");
- printf("5、查找信息\n");
- printf("6、删除信息\n");
- printf("7、退出\n");
代码使用了后插入法
- #define _CRT_SECURE_NO_WARNINGS
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
-
- typedef int dataType;
- typedef struct node
- {
- int data;//数据-车牌
- char color[20];//颜色
- int length;//长度
- struct node *next;
- }Node,*LinkList;
-
- void insertLinkList(LinkList L,int i,dataType x,char s[]);//插入函数,在L中第i个位置插入x,s;
- int searchLinkList(LinkList L,dataType x);//查找x是否在L中,若有返回x在L中的位序,若没有返回0
- int delLinkList(LinkList L,int i);//删除L中第i个元素,并返回其值
- LinkList init(int n);//创建链表
- void show(LinkList L);//展示链表
- void destorylist(LinkList L);//销毁表
- void mingling(LinkList L,int n);//命令执行
- void choose(LinkList L);//选项
- int getnumber();
- //主函数
- int main()
- {
- int n;
- LinkList L;
- n=getnumber();
- L=init(n);
- show(L);
- choose(L);
- free(L);
- }
- //得到要输入的车辆数量
- int getnumber()
- {
- int n;
- printf("请输入你要录入的车辆数量:");
- scanf("%d",&n);
- return n;
- }
- /*建立链表
- 前插入法创造链表
- 算法时间复杂度为O(n)
- */
- //LinkList init(int n)
- //{
- // int i;
- // LinkList L;
- // Node *p;
- // L=(LinkList)malloc(sizeof(Node));
- // L->next=NULL;
- // for(i=1;i<=n;i++)
- // {
- // p=(Node *)malloc(sizeof(Node));//生成新的结点p
- // printf("输入第%d车辆的车牌号:",i);
- // scanf("%d",&p->data);
- // p->next=L->next;//将新结点插入头结点后
- // L->next=p;
- // }
- // L->length=n;
- // return L;//返回表
- //
- //}
-
- /*建立链表
- 后插入法创造链表
- 算法时间复杂度为O(n)
- */
- LinkList init(int n)
- {
- int i;
- LinkList L;
- Node *r;
- Node *p;
- L=(LinkList)malloc(sizeof(Node));//建立一个带头指针的空链表
- L->next=NULL;
- r=L;//尾指针r指向头结点
- for(i=1;i<=n;i++)
- {
- p=(Node *)malloc(sizeof(Node));//生成新结点
- printf("输入第%d车辆的车牌号:",i);//输入元素值赋给新结点*p的数据域
- scanf("%d",&p->data);
- printf("输入第%d车辆的车颜色:",i);
- scanf("%s",p->color);
- p->next=NULL;//将新结点*p插入尾结点*r之后
- r->next=p;//r指向新的尾结点*p
- r=p;
- }
- L->length=n;
- return L;
- }
- //展示信息
- //算法的时间复杂度未O(n),n为单链表中的数据节点的个数
- void show(LinkList L)
- {
- int i=0;
- Node *p;
- p=L->next;
- while(p)
- {
- i++;
- printf("第%d车辆的车牌号:%d\n",i,p->data);
- printf("第%d车辆的车颜色:%s \n",i,p->color);
- p=p->next;
-
- }
- printf("\n信息已全部输出\n");
-
- }
-
- //查找信息
- int searchLinkList(LinkList L,dataType x)
- {
- Node *p;int j;
- p=L;
- j=0;
- //判断车牌x是否在表中
- while(j<L->length&&p&&p->data!=x)
- {
- p=p->next;
- j++;
- }
- if(!p||j>L->length)
- {
- return -1;
- }
- else
- return j;//返回车牌所在的位序
- }
- //插入
- //在L中第i个位置插入x
- void insertLinkList(LinkList L,int i,dataType x,char s[])
- {
- int a=1;
- Node *c;
- Node * temp;
- temp=L;//创建临时结点temp等于L
- //首先找到要插入位置的上一个结点
- for ( ;a<i; a++) {
- if (temp==NULL) {
- printf("插入位置无效\n");
- }
- temp=temp->next;
- }
- //创建插入结点c
- c=( Node*)malloc(sizeof(Node));
- c->data=x;
- strcpy(c->color,s);
- //向链表中插入结点
- c->next=temp->next;
- temp->next=c;
- }
- //销毁表
- void destorylist(LinkList L)
- {
- free(L);
- }
- //删除目标信息
- //删除L中第i个元素,并返回其值。
- int delLinkList(LinkList L,int i)
- {
- int j=0,temp;
- LinkList p,q;p=L;
- while(j<i&&p!=NULL)
- {
- j++;
- p=p->next;
- }
- if(p==NULL)
- return -1;
- else
- {
- temp=p->data;
- q=p->next;
- if(q==NULL)
- return -1;
- p->data=q->data;
- strcpy(p->color,q->color);
- p->next=q->next;
- free(q);
- return temp;
- }
- }
- //选择
- void choose(LinkList L)
- {
- int n;
- printf("\n输入你要选择的命令\n");
- printf("1、展示一切信息\n");
- printf("2、插入\n");
- printf("3、销毁表\n");
- printf("4、初始化表\n");
- printf("5、查找信息\n");
- printf("6、删除信息\n");
- printf("7、退出\n");
- scanf("%d",&n);
- mingling(L,n);
- }
- //命令执行
- void mingling(LinkList L,int n)
- {
- int i,flag;int e;char s[20];
- switch(n)
- {
- case 1: show(L);choose(L);break;
- case 2:
- {
- printf("你想插入的位置 数据(车牌) 颜色:\n");
- scanf("%d %d %s",&i,&e,s);
- insertLinkList(L,i,e,s);
- show(L);
- choose(L);
- }break;
- case 3:destorylist(L);choose(L);break;
- case 4:init(n);choose(L);break;
- case 5:
- {
- printf("你想查找的数据(车牌):\n");
- scanf("%d",&e);
- flag=searchLinkList(L,e);
- if(flag==-1)printf("未找到该信息\n");
- else printf("找到该信息,信息为:数据(车牌)%d位于表中第%d位\n",e,flag);
- choose(L);
- }break;
- case 6:
- {
- printf("你想删除第几位的数据:\n");
- scanf("%d",&i);
- flag=delLinkList(L,i);
- if(flag==-1)printf("未执行命令");
- else printf("删除了第%d位信息\n被删除的第%d位上数据(车牌)为%d",i,i,flag);
- choose(L);
- }break;
- case 7:exit(0);break;
- default: choose(L);break;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
前插入法
- /*建立链表
- 前插入法创造链表
- 算法时间复杂度为O(n)
- */
- LinkList init(int n)
- {
- int i;
- LinkList L;
- Node *p;
- L=(LinkList)malloc(sizeof(Node));
- L->next=NULL;
- for(i=1;i<=n;i++)
- {
- p=(Node *)malloc(sizeof(Node));//生成新的结点p
- printf("输入第%d车辆的车牌号:",i);
- scanf("%d",&p->data);
- p->next=L->next;//将新结点插入头结点后
- L->next=p;
- }
- L->length=n;
- return L;//返回表
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
输出
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。