首先是基本框架:
1. 录入链表
2. 插入一个元素
3. 删除一个元素
4. 遍历链表,输出所有
5. 链表排序
6. 退出
看了一下资料,了解了不少,写了基本框架,今天先把录入和输出给实现了吧!
先把代码贴出来:
首先是主函数:
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
typedef struct Node{
int Data;
Node *next;
}Node;
int main()
{
int where,select,back;
Node *L;
while(1)
{
system("color 1c");
system("cls");
back=0;
select=front(); //主屏函数
switch(select)
{
case 1:L=Build();break; //创建链表
case 2:printf("开发中……\n");break;
case 3:printf("开发中……\n");break;
case 4:Show(L);break; //遍历链表
case 5:back=1;break;
}
if(back) break;
}
return 0;
}
然后是主屏函数函数:
int front() //显示主屏
{
int opt;
printf("***************************************\n");
printf(" 1.录入一个新链表\n");
printf(" 2.插入一个元素\n");
printf(" 3.删除一个元素\n");
printf(" 4.显示当前链表内元素\n");
printf(" 5.退出\n");
printf("***************************************\n");
printf("请选择:[ ]\b\b");
scanf("%d",&opt);
return opt;
}
其次是创建链表函数:
1.头插法:
Node *Build() //创建链表函数
{
system("cls");
int n;
printf("请输入链表结点个数:[ ]\b\b\b");
scanf("%d",&n);
Node *M, *N, *L;
L=( Node*)malloc(sizeof(Node)); //开辟内存
M=( Node*)malloc(sizeof(Node));
printf("请输入%d个数字,以空格隔开:\n",n);
scanf("%d",&M->Data);
M->next=NULL;
L->next=M;
for(int i=1;i<n;i++)
{
N=( Node*)malloc(sizeof(Node));
scanf("%d",&N->Data);
N->next=NULL;
M->next=N;
M=N;
}
return L;
}
2.尾插法
Node *Build() //创建链表函数
{
system("cls");
int n;
printf("请输入链表结点个数:[ ]\b\b\b");
scanf("%d",&n);
Node *N, *L;
L=( Node*)malloc(sizeof(Node));
printf("请输入%d个数字,以空格隔开:\n",n);
L->next=NULL;
for(int i=1;i<=n;i++)
{
N=( Node*)malloc(sizeof(Node));
scanf("%d",&N->Data);
N->next=L->next;
L->next=N;
}
return L;
}
然后是遍历函数:
void Show(Node *L) //遍历函数
{
printf("当前链表为:\n");
int a=0;
L=L->next;
while(L)
{
a++;
printf("%d:%d\n",a,L->Data);
L=L->next;
}
system("PAUSE");
}
好吧,先写到这里,一会再把后面的部分写出来。写一下博客,思路清晰多了,嘿;嘿……
插入函数:
void Inset(Node* L) //在链表中一个插入元素
{
system("cls");
Node *M;
int a;
printf("请确定你要插入的结点位置:");
scanf("%d",&a);
for(int i=1;i<a;i++)
L=L->next;
M=( Node*)malloc(sizeof(Node));
printf("请输入你要插入的元素:");
scanf("%d",&M->Data);
M->next=L->next;
L->next=M;
}
删除函数:
void Delete(Node* L) //删除函数
{
Node* M;
M=( Node*)malloc(sizeof(Node));
int n,temp=1;
printf("请确定你要删除的元素:");
scanf("%d",&n);
L=L->next;
while(L)
{
if(L->Data==n)
{
M->next=L->next;
temp=0;
printf("删除成功!\a\n");
break;
}
M=L;
L=L->next;
}
if(temp) printf("元素不存在!!\a\n");
system("PAUSE");
}
退出函数:
int Exit(Node* L) //退出函数
{
int n;
Node* M;
printf("是否退出?1.是/2.否[ ]\b\b");
scanf("%d",&n);
if(n==2) return 0;
L=L->next;
while(L)
{
M=L;
L=L->next;
free(M); //释放内存
}
return 1;
}
呜~~~~~~终于搞定了,不过那个排序的算法没有写,太麻烦了,以后如果真用的话,就用结构体,爽呆了!!
现在把我的整体结果给贴出来吧,哈哈!
这是头插法的:
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
typedef struct Node{
int Data;
Node *next;
}Node;
Node *Build() //创建链表函数
{
system("cls");
int n;
printf("请输入链表结点个数:[ ]\b\b\b");
scanf("%d",&n);
Node *M, *N, *L;
L=( Node*)malloc(sizeof(Node));
M=( Node*)malloc(sizeof(Node));
printf("请输入%d个数字,以空格隔开:\n",n);
scanf("%d",&M->Data);
M->next=NULL;
L->next=M;
for(int i=1;i<n;i++)
{
N=( Node*)malloc(sizeof(Node));
scanf("%d",&N->Data);
N->next=NULL;
M->next=N;
M=N;
}
return L;
}
void Show(Node *L) //遍历函数
{
printf("当前链表为:\n");
int a=0;
L=L->next;
while(L)
{
a++;
printf("%d:%d\n",a,L->Data);
L=L->next;
}
system("PAUSE");
}
void Inset(Node* L) //在链表中一个插入元素
{
system("cls");
Node *M;
int a;
printf("请确定你要插入的结点位置:");
scanf("%d",&a);
for(int i=1;i<a;i++)
L=L->next;
M=( Node*)malloc(sizeof(Node));
printf("请输入你要插入的元素:");
scanf("%d",&M->Data);
M->next=L->next;
L->next=M;
}
void Delete(Node* L) //删除函数
{
Node* M;
M=( Node*)malloc(sizeof(Node));
int n,temp=1;
printf("请确定你要删除的元素:");
scanf("%d",&n);
L=L->next;
while(L)
{
if(L->Data==n)
{
M->next=L->next;
temp=0;
printf("删除成功!\a\n");
break;
}
M=L;
L=L->next;
}
if(temp) printf("元素不存在!!\a\n");
system("PAUSE");
}
int Exit(Node* L) //退出函数
{
int n;
Node* M;
printf("是否退出?1.是/2.否[ ]\b\b");
scanf("%d",&n);
if(n==2) return 0;
L=L->next;
while(L)
{
M=L;
L=L->next;
free(M); //释放内存
}
return 1;
}
int front() //显示主屏
{
int opt;
printf("***************************************\n");
printf(" 1.录入一个新链表\n");
printf(" 2.插入一个元素\n");
printf(" 3.删除一个元素\n");
printf(" 4.显示当前链表内元素\n");
printf(" 5.退出\n");
printf("***************************************\n");
printf("请选择:[ ]\b\b");
scanf("%d",&opt);
return opt;
}
int main()
{
int where,select,back;
Node *L;
L=( Node*)malloc(sizeof(Node));
L->next=NULL;
while(1)
{
system("color 1c");
system("cls");
back=0;
select=front();
switch(select)
{
case 1:L=Build();break; //建立
case 2:Inset(L);Show(L);break; //插入
case 3:Delete(L);break; //删除
case 4:Show(L);break; //遍历
case 5:back=Exit(L);break; //退出
}
if(back) break;
}
return 0;
}
运行结果: