|
发表于 2018-4-3 22:04:04
|
显示全部楼层
- # include<stdio.h>
- # include<stdlib.h>
- typedef struct LNode {
- int data;
- struct LNode *next;
- }LNode, *Linklist;
- void initlist(Linklist *L) //初始化链表
- {
- (*L) = (Linklist)malloc(sizeof(LNode));
- if(!(*L))
- printf("对不起,无法进行地址分配哦~\n");
- (*L)->next = *L;
- printf("初始化成功 ~\n");
- (*L)->data = 1234; // debug
- }
- void creatlist(Linklist *L)//尾插法创建链表
- {
- int n, i;
- Linklist r;//指向尾节点
- r = *L;
- printf("请输入需要的元素个数:");
- scanf("%d", &n);
- for(i = 0; i<n; i++)
- {
- Linklist p = malloc(sizeof(LNode));
- if(!p)
- printf("对不起,无法正常分配空间哦\n");
- else
- {
- printf("请输入第%d个元素", i + 1);
- scanf("%d", &p->data);
- p->next = *L;
- r->next = p;
- r = p;
- }
- }
- }
- void showlist(Linklist *L) //显示链表
- {
- if(*L == NULL)
- return -1;
- Linklist p;
- p = (*L)->next;
- if((*L)->next == *L)
- printf("循环链表中没有元素哦,无法显示。\n");
- else
- {
- while(p != (*L))
- {
- printf("%d ", p->data);
- p = p->next;
- }
- }
- printf("\n");
- }
- /*void destroylist(Linklist *L) //销毁链表
- {
- Linklist p;
- int len=lengthlist(&L);
- int i;
- for(i=0;i<len;i++)
- {
- p=(*L)->next;
- free(*L);
- *L=p;
- }
- printf("您的链表已被销毁~\n");
- }*/
- int destroylist(Linklist *L)
- {
- Linklist p, pnext, p1 = (*L)->next;
- for(p = p1; p->next != p1; p = pnext)
- {
- pnext = p->next;
- free(p);
- }
- free(*L);
- printf("您的链表已被销毁~\n");
- *L = NULL;
- return 0;
- }
- int lengthlist(Linklist *L) //求链表长度
- {
- if(*L == NULL)
- return -1;
- Linklist p;
- int length = 0;
- p = (*L)->next;
- while(p != (*L))
- {
- length++;
- p = p->next;
- }
- printf("您的循环链表长度为%d\n", length);
- return length;
- }
- void clearlist(Linklist *L) //清空链表
- {
- Linklist p, q;
- p = (*L)->next;
- while(p != (*L))
- {
- q = p->next;
- free(p);
- p = q;
- }
- p->next = (*L);
- }
- void isempty(Linklist *L) //判断表是否为空
- {
- if((*L)->next == (*L))
- printf("该链表为空\n");
- else
- printf("该链表不为空~\n");
- }
- void locateelemlist(Linklist *L) //根据元素内容判断元素位置
- {
- int e, i = 0;
- Linklist p;
- printf("请输入您想要查询的元素内容:");
- scanf("%d", &e);
- p = (*L)->next;
- if((*L)->next == *L)
- printf("循环链表中没有元素哦,无法查询。\n");
- while(p->next != (*L))
- {
- i++;
- if(p->data == e)
- printf("您查询的元素内容%d是第%d号元素\n", e, i);
- p = p->next;
- }
- }
- void getelemlist(Linklist *L) //根据元素位置查找元素内容。
- {
- int i = 0, e, j;
- Linklist p = (*L)->next;
- printf("请输入您想要查询的元素位置:");
- scanf("%d", &j);
- if((*L)->next == *L)
- printf("循环链表中没有元素哦,无法查询。\n");
- else
- {
- while(p != (*L))
- {
- i++;
- if(i == j)
- {
- e = p->data;
- printf("您所要查询的位置的元素内容为%d\n", e);
- break;
- }
- p = p->next;
- }
- if(i != j)
- printf("尚未查询到您所需要的元素。\n");
- }
- }
- void insertlist(Linklist *L) //将指定的元素位置之前插入一个元素
- {
- int j, i, e;
- Linklist p, s;
- p = *L;
- j = 0;
- printf("请输入您想要在几号元素之前插入什么元素内容");
- scanf("%d,%d", &i, &e);
- while(p->next != (*L) && j<i - 1)
- {
- p = p->next;
- j++;
- }
- if(!p || j>i - 1)
- printf("对不起插入位置有误。\n");
- else
- {
- s = (Linklist)malloc(sizeof(LNode));
- s->data = e;
- s->next = p->next;
- p->next = s;
- }
- }
- void deletelist(Linklist *L)
- {
- int i, j;
- Linklist p, q;
- p = *L;
- j = 0;
- printf("请问您想要在删除哪一号元素:");
- scanf("%d", &i);
- while(p->next != (*L) && j<i - 1)
- {
- p = p->next;
- j++;
- }
- if(!p&&j>i - 1)
- printf("对不起删除位置有误\n");
- else
- {
- q = p->next;
- p->next = q->next;
- }
- }
- int main()
- {
- Linklist L;
- initlist(&L);
- creatlist(&L);
- showlist(&L);
- lengthlist(&L);
- destroylist(&L);
- //locateelemlist(&L);
- lengthlist(&L);
- showlist(&L);
- //getelemlist(&L);
- /*insertlist(&L);
- showlist(&L);
- deletelist(&L);
- showlist(&L);
- clearlist(&L);
- showlist(&L);*/
- return 0;
- }
复制代码 |
|