#include
#include
#include
typedef int ElemType; 数据元素类型
typedef struct LNode
{
ElemType data; 数据子域
struct LNode *next; 指针子域
}LNode; 顺序存储的结构体类型
LNode *L;
LNode *creat_L();
void out_L(LNode *L);
void insert_L(LNode *L,int i,ElemType e);
ElemType delete_L(LNode *L,int i);
int locat_L(LNode *L,ElemType e);
主函数
void main()
{
int i,k,loc;
ElemType e,x;
char ch;
do
{
printf("\n");
printf("\n 1.建立单链表");
printf("\n 2.插入数据元素");
printf("\n 3.删除数据元素");
printf("\n 4.查找数据元素");
printf("\n 0.结束程序运行");
printf("\n==========================================");
printf("\n 请输入你的选择(1,2,3,4,0)");
scanf("%d",&k);
switch(k)
{
case 1:{
L=creat_L();
out_L(L);
}break;
case 2:{
printf("\n请输入插入位置:");
scanf("%d",&i);
printf("\n请输入要插入的数据元素值:");
scanf("%d",&e);
insert_L(L,i,e);
out_L(L);
}break;
case 3:{
printf("\n请输入删除位置:");
scanf("%d",&i);
x=delete_L(L,i);
out_L(L);
if(x!=-1)
{
printf("\n删除数据元素为:%d\n",x);
printf("删除%d后的单链表为:\n",x);
out_L(L);
}
else
printf("要删除的数据元素不存在!");
}break;
case 4:{
printf("\n请输入要查找的数据元素值:");
scanf("%d",&e);
loc=locat_L(L,e);
if(loc==-1)
printf("\n未找到指定数据元素!");
else
printf("\n已找到,数据元素位置是%d",loc);
}break;
}
printf("\n-----------------------------------");
}while(k>=1&&k<5);
printf("\n 按回车键,返回...\n");
ch=getchar();
}
建立线性链表
LNode *creat_L()
{
LNode *h,*p,*s;
ElemType x;
h=(LNode *)malloc(sizeof(LNode));
h->next=NULL;
p=h;
printf("请输入第一个数据元素:");
scanf("%d",&x);
while(x!=-999)
{
s=(LNode *)malloc(sizeof(LNode));
s->data=x;
s->next=NULL;
p->next=s;
p=s;
printf("请输入下一个数据元素:(输入-999表示结束。)");
scanf("%d",&x);
}
return(h);
}
输出单链表中的数据元素
void out_L(LNode *L)
{
LNode *p;
p=L->next;
printf("\n\n");
while(p!=NULL)
{
printf("%5d",p->data);
p=p->next;
}
}
在单链表中的第i个位置插入数据元素e
void insert_L(LNode *L,int i,ElemType e)
{
}
删除第i个数据元素,返回其值
ElemType delete_sq(SqList *L,int i)
{
}
查找值为e的元素,返回它的值
int locat_L(LNode *L,ElemType e)
{
LNode *p;
int j=1;
p=L->next;
while(p!=NULL&&p->data!=e)
{
p=p->next;
j++;
}
if(p!=NULL)
return(j);
else
return(-1);
}