# 单链表的创建和使用（数据结构）

#include<stdio.h>
#include<malloc.h>

typedef int Position;
typedef struct LNode * PtrToLNode;

struct LNode
{
char alp;
struct LNode *next;
};
typedef PtrToLNode List;

void show(  );
List MakeEmpty();
Position Find(char alp);
bool Insert(int X, char alp);
bool Delete(int X);
void DispList();
int Length();

int main()
{
int i, X, n, cnt, c=0;
char alp, k;
show();
while(1){
//显示菜单
scanf("%d", &n);
if(n<-1||n>6)
{
printf("\n输入无效，请重新输入：");
continue;
}
if(c==0&&(n>0&&n<7)&&n!=1)
{
printf("\n还未创建链表，请重新输入:");
continue;
}

switch(n)
{
case 1:
{
MakeEmpty();  //建立新的链表
printf("新的链表为:");
cnt = Length()-1;
for( i=0; i<cnt; i++ )
{
printf("%c", p1->alp);
p1 = p1->next;
}
printf("\n");
c=1;
}
break;

case 2:
{
printf("\n请输入你要插入的位置：");
scanf("%d", &X);
printf("请输入你要插入的字母：");
scanf("\n%c", &alp);
Insert(X, alp);
}
break;

case 3:
{
printf("输入你要删除的位序号");
scanf("%d", &X);
Delete(X); //删除元素
}
break;

case 4: //显示全部元素
{
printf("新的链表为:");
cnt = Length()-1;
for( i=0; i<cnt; i++ )
{
printf("%c", p2->alp);
p2=p2->next;
}
}
break;

case 5:
{
printf("输入你要查找的字母");
k = getchar();
scanf("%c", &alp);
if( i!=0 && i<=Length() )
printf("你查找的字母在第%d个位置上！\n", Find(alp)+1);//查找元素函数
else
printf("抱歉，找不到该字母\n");
}
break;

case 6:
{
printf("表长长度为%d\n", Length()-1 ); //求得链表长度
}
break;
}
show();
}
return 0;
}

int Length() //查找元素长度
{
Position cnt = 0;
while(p)
{
//printf("*cnt = %d*%c*\n",cnt, p);
p = p->next;
cnt++;
}
return cnt;
}
List MakeEmpty()
{
head = (List)malloc(sizeof(struct LNode));   //在这里申请大小为struct LNode 的结构空间
struct LNode *p=(struct LNode *)malloc(sizeof(struct LNode));
struct LNode *t=(struct LNode *)malloc(sizeof(struct LNode));
char alp, k;
printf("请输入字母，输入0为结束按回车键：");//输入连续的字母串，以0为结束
k = getchar();
while(true)
{
scanf("%c", &alp);
if(alp=='0')
{
p->next = NULL;
break;
}
p->alp = alp;
p->next = t;
p = t;
t = (struct LNode *)malloc(sizeof(struct LNode));
//printf("%c", p->alp);
}
}

Position Find(char alp)//元素查找
{
Position i=0, cnt = Length();
while(t->alp!=alp )
{
if( i>cnt )
break;
t = t->next;
i++;
}
printf("%d", i);
return i;
}

bool Insert(int X, char alp) //插入操作
{
Position j;
struct LNode *p=(struct LNode *)malloc(sizeof(struct LNode));
struct LNode *m=(struct LNode *)malloc(sizeof(struct LNode));
/*  for( j=L->Last; j>=X-1; j-- )
p->alp[j+1] = p->alp[j];
p->alp[i-1] = X;
p->Last++;*/
for( j = 0; j<=X-2; j++ )
p = p->next;
m->next = p->next;
p->next = m;
m->alp = alp;
}

bool Delete(int X) //删除操作
{
Position j, k;
struct LNode *u=(struct LNode *)malloc(sizeof(struct LNode));
struct LNode *o=(struct LNode *)malloc(sizeof(struct LNode));
/*for( j=i; j<=L->Last; j++ )
L->Date[j-1] = L->Date[j];
L->Last--;*/
if( X>=2 )
{
for( j=0; j<X-2; j++ )
u = u->next;
o = u->next;
u->next = o->next;
free(o);
k=1;
}
else
if( k=1 )
printf("删除成功！");
else
printf("删除失败！请重试。");
}

void show()
{
printf("\n\n");
printf("                    数字查询系统          \n");
printf("            ******************************\n");
printf("            *       1------建   表       *\n");
printf("            *       2------插   入       *\n");
printf("            *       3------删   除       *\n");
printf("            *       4------显   示       *\n");
printf("            *       5------查   找       *\n");
printf("            *       6------表   长       *\n");
printf("            *       0------退   出       *\n");
printf("            ******************************\n\n");
printf("请选择你需要的操作(0-6): ");
}