#include<stdio.h>
#include<stdlib.h>
struct ListNode {
int data;
ListNode* next;
};
ListNode* insert( ListNode * L, int l, int e,int* len);
ListNode* newlist(ListNode* L, int* len);
void print(ListNode L);
bool locationright(int i, int e);
ListNode deletem(ListNode* L, int l,int len);
int getelem(ListNode L, int loca);
void freem(ListNode* L);
int main()
{
int i, j, k,temp;
int length = 0;
ListNode* L;
L = NULL;
printf("* 1、建立单链表(初始化) \n");
printf(" 2、查找元素(定位) \n");
printf(" 3、插入元素 \n");
printf(" 4、删除元素 \n");
printf(" 5、显示元素 \n");
printf(" 6、修改元素(存储) \n");
printf(" 7、判断是否为空表 \n");
printf(" 8、计算数据表长度 \n");
printf(" 9、销毁数据表 \n");
printf(" 0、退出程序 \n");
printf("-----------------------------------------------------------------\n");
printf(“提示:请首先初始化线性表!\n”);
while (1)
{
printf(“请输入你的选项:0-9:\n”);
scanf_s("%d", &temp);
if (temp == 0)
break;
switch (temp)
{
case 1:
{
L = newlist(L, &length);
printf(“the list is ready\n”);
printf(“the list is :”);
print(L);
break;
}
case 2:
{
printf(“please input the location:\n”);
scanf_s("%d", &i);
if (!locationright(i, length))
{
printf(“wrong location\n”);
}
else
{
j = getelem(L, i);
printf(“the elem is: %d\n”, j);
}
break;
}
case 3:
{
printf(“please input the location and the elem to insert:\n”);
scanf_s("%d", &i);
if (i > length + 1)
printf(“wrong location\n”);
else
{
scanf_s("%d", &j);
L = insert(L, i, j, &length);
printf(“the list is :”);
print(L);
}
break;
}
case 4:
{
printf(“please inpput the location to deletem:\n”);
scanf_s("%d", &i);
if (locationright(i, length))
{
L = deletem(L, i, &length);
printf(“the list is :”);
print(L);
}
else
{
printf(“wrong location\n”);
}
break;
}
case 5:
{
printf(“please input the location to get:\n”);
scanf_s("%d", &i);
if (locationright(i, length))
{
getelem(L, i);
}
else
printf(“wrong location\n”);
break;
}
case 6:
{
scanf_s("%d", &i, &j);
if (locationright(length, i))
{
L = deletem(L, i, &length);
L = insert(L, i, j, &length);
printf(“the list is :”);
print(L);
}
else
{
printf(“wrong location\n”);
}
break;
}
case 7:
{
if (length)
printf(“it is a list length %d\n”, length);
else
printf(“it is empty\n”);
break;
}
case 8:
{
printf(“its length is %d\n”, length);
break;
}
case 9:
{
freem(L);
printf(“it has been deleted\n”);
break;
}
default:
break;
}
}
return 0;
}
ListNode insert(ListNode* L, int l, int e,int len)
{
ListNode head, temp, p;
p = (ListNode*)(malloc(sizeof(ListNode)));
head = L;
temp = L;
for (int i = 1; i < l; i++)
{
L = temp->next;
if (L == NULL)
break;
temp = L;
}
p->data = e;
p->next = NULL;
if (l != 1)
{
temp->next = p;
}
else
{
head= p;
}
(len)++;
return head;
}
ListNode newlist(ListNode* L, int* len)
{
int e;
int length = 0;
printf(“please input the length:\n”);
scanf_s("%d",len);
printf(“please input the data:\n”);
for (int i = 1; i <= len; i++)
{
scanf_s("%d", &e);
L = insert(L, i, e, &length);
}
return L;
}
void print(ListNode L)
{
while (L)
{
printf("%d “, L->data);
L = L->next;
}
printf(”\n");
return;
}
bool locationright(int i, int e)
{
if (i > e||i<1)
return 0;
return 1;
}
ListNode* deletem(ListNode* L, int l,intlen)
{
ListNode head, prior;
head = L;
prior = L->next;
if (l == 1)
L = L->next;
else
{
for (int i = 1; i < l; i++)
{
head = prior;
prior = prior->next;
}
head->next = prior->next;
}
len–;
return L;
}
int getelem(ListNode L, int loca)
{
int elem;
for (int i = 1; i < loca; i++)
{
L = L->next;
}
elem = L->data;
return elem;
}
void freem(ListNode L)
{
while (L)
{
free(L);
L = L->next;
}
return;
}
由于我是用插入操作代替链表的建立过程,所以这其中牵扯到一堆花里胡哨的问题,比如有的函数如果不设返回值的话就要用指针的指针(也可以将指针和引用混合使用),还遇到了设p为空后还要用到p->next所出现的bug,总体上算是加强了对指针以及链式结构的理解。(编译器为visual studio,所以用scanf_s)
单链表的详细操作
最新推荐文章于 2022-10-25 21:24:53 发布