#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define ERROR 0
#define OK 1
#define LEN sizeof(struct Node)
typedef char ElemType;
typedef struct Node{
ElemType data;
struct Node* next;
}Node,*LinkList;
LinkList Create()
{
LinkList head;
Node *p1,*p2;
char data;
head=(Node*)malloc(LEN);
p2=head;
while((data=getchar()) != '\n')
{
p1=(Node*)malloc(LEN);
p1->data=data;
p2->next=p1;
p2=p1;
}
p2->next=NULL;
return (head);
}
void print(LinkList head)
{
Node *p;
p=head->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
int ListInsert(LinkList head,int pos,char x)
{
Node *p,*s;
int i=0;
p=head;
while(p!=NULL && i<pos-1)
{
p=p->next;
i++;
}
if(p==NULL || i!=pos-1) return ERROR;
s=(Node*)malloc(LEN);
s->data=x;
s->next=p->next;
p->next=s;
return OK;
}
int ListDel(LinkList head,int pos)
{
Node *p,*r;
int j=0;
p=head;
while(p!=NULL && j<pos-1)
{
p=p->next;
j++;
}
if(p==NULL || j!=pos-1) return ERROR;
r=p->next;
p->next=p->next->next;
free(r);
return OK;
}
char FindList(LinkList head,int pos)
{
Node *p;
int k=0;
p=head;
while(p!=NULL && k<pos)
{
p=p->next;
k++;
}
if(p!=NULL && pos==k) return p->data;
}
int main()
{
LinkList head=NULL;
char p;
char x;
int del_num,in_num,ser_num;
printf("input node:\n");
head=Create();
print(head);
printf("input search node:\n");
scanf("%d",&ser_num);
p=FindList(head,ser_num);
printf("%c\n",p);
printf("input insert position and node:\n");
scanf("%d,%c",&in_num,&x);
ListInsert(head,in_num,x);
printf("after insert:\n");
print(head);
printf("input delete position:\n");
scanf("%d",&del_num);
ListDel(head,del_num);
printf("after delete:\n");
print(head);
}