#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct LinkNode
{
char data;
struct LinkNode *next;
}LinkNode;
void InitList(LinkNode *&L)
{
L=(LinkNode*)malloc(sizeof(LinkNode));
L->next=NULL;
}
void CreatList(LinkNode *&L,char a[],int n)
{
LinkNode *s,*r;
L=(LinkNode*)malloc(sizeof(LinkNode));
r=L;
for(int i=0;i<n;i++)
{
s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void EmptyList(LinkNode *L)
{
if(L->next==NULL)
{
printf("empty\n");
}
else
{
printf("not empty\n");
}
}
void DisList(LinkNode *L)
{
LinkNode *p=L->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
void LengthList(LinkNode *L)
{
int n;
LinkNode *p=L;
while(p->next!=NULL)
{
n++;
p=p->next;
}
printf("Length:%d\n",n);
}
void GetElem(LinkNode *L,int n)
{
int j=0;
LinkNode *p=L;
if(n<=0)
{
printf("ERROR input\n");
}
else
{
while(j<n&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
{
printf("ERROR input\n");
}
else
{
printf("%c\n",p->data);
}
}
}
void LocateList(LinkNode *L,char e)
{
int i=1;
LinkNode *p=L->next;
while(p!=NULL&&p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)
{
printf("ERROR input\n");
}
else
{
printf("%d\n",i);
}
}
void ListDelete(LinkNode *&L,int n,char &e)
{
int j=0;
LinkNode *p=L;
LinkNode *q;
if(n<=0)
{
printf("ERROR input\n");
}
while(p!=NULL&&j<n-1)
{
j++;
p=p->next;
}
if(p==NULL)
{
printf("ERROR input\n");
}
else
{
q=p->next;
if(q==NULL)
{
printf("ERROR input\n");
}
e=q->data;
p->next=q->next;
free(q);
}
}
void ListInsert(LinkNode *&L,int n,char &e)
{
int j=0;
LinkNode *p=L,*q;
if(n<=0)
{
printf("ERROR input\n");
}
while(j<n-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
{
printf("ERROR input\n");
}
else
{
q=(LinkNode*)malloc(sizeof(LinkNode));
q->data=e;
q->next=p->next;
p->next=q;
}
}
int main()
{
char s[100];
char ch,sh;
char e;
int n,i,j,flag;
LinkNode *L;
int count;
scanf("%s",s);
count=strlen(s);
scanf("%d\n",&n);
scanf("%c\n",&ch);
scanf("%d\n",&i);
scanf("%c\n",&sh);
scanf("%d",&j);
InitList(L);
EmptyList(L);
CreatList(L,s,count);
DisList(L);
LengthList(L);
EmptyList(L);
GetElem(L,n);
LocateList(L,ch);
ListInsert(L,i,sh);
DisList(L);
ListDelete(L,j,e);
DisList(L);
}
不知道为什么有一组数据一直通过不了5555会变成下面的结果:
还请各路神仙帮帮我,想好久不知道为什么55