#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node *next;
}node;
node *q;
int size=0;
node *tailcreatlist();
node *headcreatlist();
int search(elemtype e);
node *get(int i);
void indicate(node *q);
void diplaymenu();
void delet(int i);
void insert(int i,elemtype e);
void insert(int i,elemtype e)
{
int j=0;
node *s,*p;
p=q;
while(p!=NULL&&j<i-1)
{
p=p->next;
j++;
}
s=(node *)malloc(sizeof(node));
s->data=e;
s->next=p->next;
p->next=s;
size++;
}
void delet(int i)
{
node *p,*f;
elemtype x;
int j=0;
p=q;
while(p->next!=NULL&&j<i-1)
{
p=p->next;
j++;
}
f=p->next;
x=f->data;
p->next=f->next;
free(f);
size--;
}
int search(elemtype e)
{
int j=1;
node *p;
p=q->next;
while(p!=NULL&&p->data!=e)
{
p=p->next;
j++;
}
if(p!=NULL)
{
return(j);
}
else
{
return(-1);
}
}
node *get(int i)
{
int j=0;
node *p;
p=q;
while(p->next!=NULL&&j<i)
{
p=p->next;
j++;
}
if(i==j)
return(p);
else
return NULL;
}
node *tailcreatlist()
{
node *s,*p,*h;
int x;
h=(node *)malloc(sizeof(node));
h->next=NULL;
p=h;
printf("请输入您要创建的线性表的数(以-111结束):\t\t");
scanf("%d",&x);
while(x!=-111)
{
s=(node *)malloc(sizeof(node));
s->data=x;
s->next=NULL;
p->next=s;
p=s;
printf("请输入您要创建的线性表的数(以-111结束):\t\t");
scanf("%d",&x);
size++;
}
return(h);
}
node *headcreatlist()
{
int x;
node *s,*h;
h=(node *)malloc(sizeof(node));
h->next=NULL;
printf("请输入您要创建的线性表的数(以-111结束):\t\t");
scanf("%d",&x);
while(x!=-111)
{
s=(node *)malloc(sizeof(node));
s->data=x;
s->next=h->next;
h->next=s;
printf("请输入您要创建的线性表的数(以-111结束):\t\t");
scanf("%d",&x);
size++;
}
return(h);
}
void indicate(node *h)
{
node *p;
p=h->next;
while(p!=NULL)
{
printf("%3d",p->data);
p=p->next;
}
printf("\n该线性表的大小为:\t\t%d\n",size);
}
void displaymenu()
{
elemtype e;
int i,k;
int *x;
do
{
printf("1---------------尾插法创建线性表\n");
printf("2---------------头插法创建线性表\n");
printf("3---------------查询第i个节点数值e\n");
printf("4---------------查询值为e的节点数\n");
printf("5---------------删除第i个节点的元素\n");
printf("6---------------在第i个位置插入元素e\n");
printf("7---------------退出\n");
printf("请输入您的选择:\t\t");
scanf("%d",&k);
}while(k>7&&k<1);
switch(k)
{
case 1:
system("cls");
q=tailcreatlist();
indicate(q);
getch();
system("cls");
displaymenu();
break;
case 2:
system("cls");
q=headcreatlist();
indicate(q);
getch();
system("cls");
displaymenu();
break;
case 3:
system("cls");
printf("请输入您要查询的节点序号:\t");
scanf("%d",&i);
x=get(i);
if(get(i))
printf("\n\n您查询的第%d个节点的数值为:e=%d\t\t",i,*x);
else
printf("该线性表中没有此数值...");
getch();
system("cls");
displaymenu();
break;
case 4:
system("cls");
printf("\n该线性表的大小为:\t\t%d\n",size);
printf("请输入您要查询的节点数值:\t");
scanf("%d",&e);
if(search(e)!=-1)
{
printf("线性表中数值为e的节点数为:\t%d",search(e));
}
else
{
printf("线性表中没有此数值的节点...");
}
getch();
system("cls");
displaymenu();
break;
case 5:
system("cls");
printf("请输入您要删除的节点的序号:\t\t");
scanf("%d",&i);
if(i<=size)
{
delet(i);
indicate(q);
}
else
{
printf("删除的节点数大于线性表的长度!");
}
getch();
system("cls");
displaymenu();
break;
case 6:
system("cls");
printf("请输入您要插入的位置:\t\t");
scanf("%d",&i);
printf("请输入您要插入的数值:\t\t");
scanf("%d",&e);
insert(i,e);
indicate(q);
getch();
system("cls");
displaymenu();
case 7:
printf("按任意键退出...");
getch();
exit(0);
}
}
void main()
{
displaymenu();
}