//This piece of code contains all the needs of the data structure, the first chapter master function
//InitList,createFromhead,createFromtail display,insert, findValue,findPosition,deleteValue,deletePosition,merege.
// Created by Liuyao in 2016.
// Copyright (c) 2016 Liuyao. All rights reserved.
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}Node, *Linklist;
int initList(Linklist *L1,Linklist *L2);//初始化
int createFromhead(Linklist L);//头插法建立链表
int createFromtail(Linklist L);//尾插法建立链表
int insert(Linklist L, int *c);//插入操作
int findValue(Linklist L, int *c);//按值查找
int findPosition(Linklist L, int *c);//按位置查找
int deleteValue(Linklist L, int *c);//按值删除
int deletePosition(Linklist L, int *c);//按位置删除
Linklist mergeList(Linklist L1, Linklist L2);//线性表的合并
void display(Linklist L);//打印输出
int main(void)
{
int a;//用来接收菜单值
int b;//用来接收函数返回值
int c;//主函数传递的值
Linklist L1 = NULL;//头插法
Linklist L2 = NULL;//尾插法
Linklist L3 = NULL;//接收合并后的
while (1)
{
printf("\n");
printf("***************************\n");
printf("1:InitList\n");
printf("2:CreateFromHead(L1)\n");
printf("3:CreateFromTail(L2)\n");
printf("4:Insert(L1)\n");
printf("5:Find by Value(L1)\n");
printf("6;Find by Position(L1)\n");
printf("7:Delete by Value(L1)\n");
printf("8:Delete by Position(L1)\n");
printf("9:MergeList\n");
printf("10:Display\n");
printf("11:EXIT\n");
printf("***************************\n");
printf("\n");
scanf("%d", &a);
switch (a)
{
case 1:b = initList(&L1,&L2);//注意此处是将L1,L2的地址传过去
if (b == -1)
{
printf("Apply Memory Fail\n");
exit(0);
}
if (b == 1)
{
printf("Apply Memory Success!\n");
}
break;
case 2:b = createFromhead(L1);
if (b == -1)
{
printf("Can't Create!\n");
}
if (b == 1)
{
printf("Ceate Success!\n");
display(L1);
}
break;
case 3:b = createFromtail(L2);
if (b == -1)
{
printf("Can't Create!\n");
}
if (b == 1)
{
printf("Ceate Success!\n");
display(L2);
}
break;
case 4:b = insert(L1, &c);
if (b == -1)
{
printf("Insert Fail\n");
}
if (b >=0 )
{
printf("Success!\n");
printf("The position is %d,the number is %d\n",b,c);
}
break;
case 5:b = findValue(L1,&c);
if (b == -1)
{
printf("Don't find!\n");
}
if (b >= 0)
{
printf("Find Success!\n");
printf("The position is %d the figure is %d\n",b,c);
}
break;
case 6:b = findPosition(L1, &c);
if (b == -1)
{
printf("Don't find!\n");
}
if (b > 0)
{
printf("Find Success!\n");
printf("The position is %d the figure is %d\n", b, c);
}
break;
case 7:b = deleteValue(L1, &c);
if (b == -1)
{
printf("Delete Fail!\n");
}
if (b > 0)
{
printf("Delete Success!\n");
printf("The position is %d the figure is %d\n", b, c);
printf("\n");
display(L1);
}
break;
case 8:b = deletePosition(L1, &c);
if (b == -1)
{
printf("Delete Fail!\n");
}
if (b > 0)
{
printf("Delete Success!\n");
printf("The position is %d the figure is %d\n", b, c);
printf("\n");
display(L1);
}
break;
case 9:L3 = mergeList(L1, L2);
display(L3);
break;
case 10:display(L1);//注意主函数只提供打印L1
break;
case 11:exit(0);
default:printf("You input the number of the menu is error:\n");
printf("Please input the number again!\n");
}
}
return 0;
}
int initList(Linklist *L1, Linklist *L2)//注意此处为*L,表示指向头结点指针类型的变量
{
(*L1) = (Linklist)malloc(sizeof(Node));
(*L2) = (Linklist)malloc(sizeof(Node));
if ((*L1) == NULL || (*L2) == NULL)
{
return -1;
}
(*L1)->next = NULL;
(*L2)->next = NULL;
return 1;
}//初始化单链表
int createFromhead(Linklist L)
{
Node *p;
int c;
int flag = 1;//循环控制标志
printf("Please input figures of L1(end by zero):\n");
while (flag)
{
scanf("%d", &c);
if (c == 0)
{
flag = 0;
}
else
{
p = (Node*)malloc(sizeof(Node));
if (p == NULL)//如果没申请到内存,退出
{
return -1;
}
p->data = c;
p->next = L->next;//将头结点的指针给刚刚申请的
L->next = p;
}
}
return 1;
}//头插法建立单链表
int createFromtail(Linklist L)
{
Node *q=L;
Node *p = NULL;
int flag = 1;
int a;
printf("Please input the figures of L2(end by zero):\n");
while (flag)
{
scanf("%d",&a);
if (a == 0)
{
flag = 0;
q->next = NULL;//注意尾插法要在此处加上最后的指向NULL
}
else
{
p = (Node*)malloc(sizeof(Node));
if (p == NULL)
{
return -1;
}
p->data = a;
q->next = p;
q = q->next;
}
}
return 1;
}//尾插法建立链表
int insert(Linklist L, int *c)
{
int i;//接收地址
int e;//接收值
int j=0;
Node *p = L;
Node *q = NULL;
printf("Please input the number that you want to insert;\n");
scanf("%d",&e);
printf("Please input the position of the figure:\n");
scanf("%d",&i);
while ((p != NULL) && (j < i-1))//注意此处循环控制为i-1,要在i-1的后面插入
{
j++;
p = p->next;
}
if (!p)
{
return -1;
}
q = (Node *)malloc(sizeof(Node));
q->data = e;
*c = e;//将值返回给主函数
q->next = p->next;
p->next = q;
return i;
}//插入操作
int findValue(Linklist L, int *c)
{
int e;
int j = 1;
Node *p = L->next;
printf("Please input the figure that you want to find:\n");
scanf("%d",&e);
*c = e;
while ((p != NULL) && (p->data!=e))//按值查找的循环控制条件
{
p = p->next;
j++;
}
if (!p)
{
return -1;
}
return j;//返回元素位置
}//按值查找
int findPosition(Linklist L, int *c)
{
int i;
int j = 0;
Node *p = L->next;
printf("Please input the position that you want to find:\n");
scanf("%d",&i);
while ((p != NULL) && (j < i-1))
{
p = p->next;
j++;
}
if (!p)
{
return -1;
}
else
{
*c = p->data;//传值回去
return i;
}
}//按位置查找
int deleteValue(Linklist L, int *c)
{
int a;
int j=1;
Node *p = L->next;
Node *q = NULL;
printf("Please input the figure that you want to delete:\n");
scanf("%d",&a);
*c = a;
while ((p != NULL) && (p->data != a))
{
q = p;//注意此处可以设置一个q将p的上一个保存下来
p = p->next;
j++;
}
if (!p)
{
return -1;
}
if (j == 1)//特别注意删除的如果是第一个的话,特容易忽略
{
L->next = p->next;//注意
free(p);
return j;
}
else
{
q->next = p->next;
free(p);
return j;
}
}//按值删除
int deletePosition(Linklist L, int *c)
{
int i;
int j=1;
Node *p = L->next;
Node *q = NULL;
printf("Please input the position that you want to delete:\n");
scanf("%d",&i);
if (i == 1)//同理要注意当删除的是第一个位置时
{
*c = (p->data);
L->next = p->next;
free(p);
return j;
}
while ((p != NULL) && (j < (i - 1)))
{
p = p->next;
j++;
}
if (!p)
{
return -1;
}
else
{
q = p->next;
*c = (q->data);
p->next = q->next;
free(q);
return i;
}
}//按位置删除
Linklist mergeList(Linklist L1, Linklist L2)
{
Linklist L3;
L3 = L1;//将L3也指向L1首地址
Node *pa = L1->next;
Node *pb = L2->next;
L3->next = NULL;//注意此句话一定要在前两句之后,否则只会出现L2,因为L1的后面已被置空
Node *r = L3;//定义一个Node型的指正指向L3
while ((pa!=NULL) && (pb!=NULL))
{
if (pa->data <= pb->data)
{
r->next = pa;
r = pa;
pa=pa->next;
}
else
{
r->next = pb;
r = pb;
pb = pb->next;
}
}
if (pa)
{
r->next = pa;
}
if (pb)
{
r->next = pb;
}
free(L2);
return L3;
}//线性表的合并
void display(Linklist L)
{
Linklist p;
p = L->next;//从L-next开始
while (p != NULL)
{
printf("%d\t", p->data);
p = p->next;
}
printf("\n");
}//打印输出单链表
数据结构之链表
最新推荐文章于 2020-11-11 21:17:58 发布