单链表
被自己蠢哭,找了几天的错误,后面才发现算法上出了问题,看来还是不行啊,自己写的算法检查不出错误。
#include<stdio.h>
#include<stdlib.h>
int i;
typedef int datatype;
typedef struct node{
datatype data;
struct node *next;//链接指针
}Linknode;//*Linklist;//Linknode为结构体类型,Linklist为结构体类型指针
/*
Linknode *Initial(Linknode *first)//用头插法创建链表 ,输出的值为逆序
{
int x;
Linknode *s;
printf("请输入数据:\n");
scanf("%d",&x);
first->next = NULL;
while(x!=0){
s = (Linknode*)malloc(sizeof(Linknode));
if(s==NULL){
printf("存储分配错误");
exit(1);
}
s->data = x;
s->next = first->next;
first->next = s;
scanf("%d",&x);
}
printf("初始化成功");
return first;
}
*/
// 用尾插法创建链表
Linknode *Initial(Linknode *first) {
int x;
Linknode *s,*r;
r = first;
printf("请输入数据;\n");
scanf("%d",&x);
while(x!=0){
s = (Linknode*)malloc(sizeof(Linknode));
if(s==NULL){
printf("存储分配错误");
exit(1);
}
s->data = x;
r->next = s;
r = s;
scanf("%d",&x);
}
r->next = NULL;
printf("初始化成功\n");
}
int length(Linknode *first){
Linknode *p = first->next;
int count = 0;
while(p!=NULL){
p = p->next;
count++;
}
return count;
}
int search(Linknode *first,datatype x){
Linknode *p = first->next;
while(p!=NULL&&p->data!=x){
p=p->next;
} return p->data;
}
Linknode *locate(Linknode *first,int i){
if(i<0)
printf("输入的参数不合理");
Linknode *p = first;
int k = 0;
while(p!=NULL&&k<i){
p = p->next;
k++;
}
return p;
}
int Insert(Linknode *first,int i,int x){
if(!first||i==1){
Linknode *s = (Linknode *)malloc(sizeof(Linknode));
if(!s){
printf("存储分配错误\n");
exit(1);
}
s->data = x;
s->next= first->next;
first->next= s;
}
else{
Linknode *p = locate(first,i-1);
if(p==NULL&&first==NULL){
printf("无效的插入位置");
return 0;
}
else{
Linknode *s = (Linknode *)malloc(sizeof(Linknode));
if(!s){
printf("存储分配错误\n");
exit(1);
}
s->data = x;
s->next = p->next;
p->next = s;
}
}
return 1;
}
int remove(Linknode *first,int i){
Linknode *p = locate(first,i-1);
if(p==NULL||p->next==NULL)
return 0;
Linknode *q = p->next;
p->next = q->next;
free(q);
return 1;
}
void show(Linknode *first)
{
Linknode *p = first->next;
while(p != NULL)
{
printf("%d\n",p->data);
p = p->next;
}
}
void destoryLinknode(Linknode *first){
Linknode *pre = first,*p = pre->next;
while(p!=NULL){
free(pre);
pre = p;
p = p->next;
}
free (pre);
printf("单链表摧毁成功\n");
}
void menu(Linknode *first)
{
system("cls");
printf("\n\n\n\n");
printf("\t\t|---------------单链表--------------------------|\n");
printf("\t\t|\t\t\t\t\t |\n");
printf("\t\t|\t\t 1.单链表初始化 \t |\n");
printf("\t\t|\t\t 2.计算单链表长度\t |\n");
printf("\t\t|\t\t 3.插入元素\t |\n");
printf("\t\t|\t\t 4.删除元素\t |\n");
printf("\t\t|\t\t 5.元素定位\t |\n");
printf("\t\t|\t\t 6.查找元素\t |\n");
printf("\t\t|\t\t 7.输出链表元素 \t |\n");
printf("\t\t|\t\t 8.单链表摧毁成功\t |\n");
printf("\t\t|\t\t\t\t\t |\n");
printf("\t\t|-----------------------------------------------|\n\n");
printf("\t\t\t请选择(1-7):");
int n;
scanf("%d",&n);
switch(n){
case 1:
system("cls");
Initial(first);
printf("按任意键返回主菜单....");
getchar();
getchar();
menu(first);
break;
case 2:
system("cls");
printf("链表长度为%d\n",length(first));
printf("按任意键返回主菜单....");
getchar();
getchar();
menu(first);
break;
case 3:
system("cls");
int x;
printf("请输入插入的数据及位置\n");
scanf("%d %d",&i,&x);
if(Insert(first,i,x)){
printf("插入成功\n");
}else{
printf("插入失败\n");
}
printf("按任意键返回主菜单....");
getchar();
getchar();
menu(first);
break;
case 4:
system("cls");
printf("请输入要删除的元素位置\n");
scanf("%d",&i);
if(remove(first,i)){
printf("第%d个元素删除成功\n",i);
}else
printf("输入的参数不合理");
printf("按任意键返回主菜单....");
getchar();
getchar();
menu(first);
break;
case 5:
system("cls");
printf("请输入要定位元素的位置\n");
scanf("%d",&i);
printf("该元素的地址为%d\n",locate(first,i));
printf("按任意键返回主菜单....");
getchar();
getchar();
menu(first);
break;
case 6:
system("cls");
printf("请输入要查找的元素的位置\n");
scanf("%d",&i);
printf("该元素为%d\n",search(first,i));
printf("按任意键返回主菜单....");
getchar();
getchar();
menu(first);
break;
case 7:
system("cls");
show(first);
printf("按任意键返回主菜单....");
getchar();
getchar();
menu(first);
break;
case 8:
system("cls");
destoryLinknode(first);
printf("按任意键退出程序....");
getchar();
getchar();
break;
}
}
int main(){
Linknode *first = (Linknode*)malloc(sizeof(Linknode));
if(first==NULL){
printf("存储分配错误");
exit(1);
}
menu(first);
}