#include<stdio.h>
#include<stdlib.h>
typedef struct Node *List;
struct Node {
char data;
List next;
};
//头插法的建立
/*void CreatList(List *L) {
List s;
char e;
(*L) =(List)malloc(sizeof(struct Node));
(*L)->next = NULL;
printf("开始输入数据\n");
scanf("%c", &e);
while (e != '\n') {
s = (List)malloc(sizeof(struct Node));
s->data = e;
s->next = (*L)->next;
(*L)->next = s;
scanf("%c", &e);
}
}*/
//尾插法的建立
void CreatListRear(List *L) {
List s,Rear;
char c;
(*L) = (List)malloc(sizeof(struct Node));//建立头指针
(*L)->next = NULL;
Rear = (*L);//Rear记录最后一个元素
printf("开始输入数据尾插法的\n");
scanf("%c", &c);
while (c != '\n') {
s = (List)malloc(sizeof(struct Node));//新建结点
s->data = c;
s->next = NULL;
Rear->next = s; //新建结点连在前面结点后边
Rear = s; //Rear记录最后一个元素
scanf("%c", &c); //输入回车则停止插入元素
}
}
//查找数据位置返回地址
List Poisition(List L, int i) {
int j = 0;
while (L != NULL&&j < i) {
L = L->next;
j++;
}
return L;
}
//插入
void InsertList(List L) {
int i;
char e;
List p,s;
printf("在第几处插入\n");
scanf("%d", &i);
getchar();//吸收回车符
printf("插入的数据是\n");
scanf("%c", &e);
p = Poisition(L, i-1);
s = (List)malloc(sizeof(struct Node));
s->data = e;
s->next = p->next;
p->next = s;
}
void PrintList(List L) {
L = L->next;
while (L != NULL) {
printf("%c", L->data);
L = L->next;
}
}
void DeleteList(List L) {
int i;
List s,p;
printf("删除第几处的数据");
scanf("%d", &i);
p = Poisition(L, i - 1);
s = p->next;
p->next = s->next;
free(s);
}
int main() {
List L,s;
//CreatList(&L);
CreatListRear(&s);
PrintList(s);
InsertList(s);
PrintList(s);
DeleteList(s);
PrintList(s);
}