#include <stdio.h>
#include <stdlib.h>
struct node;
typedef struct node *Ptrtonode;// p struct node
typedef Ptrtonode list;//list ptrtonode ,
typedef Ptrtonode position;// position
list makelist(list L);// , list makelist , list
int isempty(list L);// , true
int islast(position P,list L);//P , position
position find (int x,list L);// L x
void delete(int x,list L);
void insert(int x,list L,position P);//P , x L P x
position findprevious(int x,list L);
position header(list L);//
position first(list L);//
position advance(position P);//
int retrieve(position P);//retrieve , ,
void printlist(list L);
struct node{
int data;
position next;
};
void main(){
list L;
int n;
L = makelist(list L);
printf("0 represent the element you want to look for in the list
");
printf("1 represent the element you want to delete in the list
");
printf("2 represent the element you want to insert in the list
");
// scanf("%d",&n);
while (n == 0 || n == 1|| n == 2){
prinf("please input choose:
");
scanf("%d",&n);
switch(n){
case '0':
printf("please input element you want to look for:
");
int c;
position P;
scanf("%d",&c);
P= find(c,L);
printf("%s",P);
case '1':
printf("please input element you want to delete:
");
int c;
scanf("%d",&c);
delete(c,L);
case '2':
printf("please input element you want to insert:
");
int c;
position P;
scanf("%d,%s",&c,P);
insert(c,L,P);
default:
return "choosing error";
}
}
}
void printlist(list L){
position P;
P = header(L);
printf("the list is :
");
if(P != NULL)
do {
printf("%d--",P -> data);
p = P -> next;
}while(P != NULL);
}
list makelist(list L){
position P;
P =L;
P -> next = NULL;
return P;
}
int isempty(list L){//return true if empty
return L-> next == NULL;
}
int islast(position P,list L){//ruturn true if P is the last position in list L.parametre( , ) L is unused in this implementation( , )
return P -> next == NULL;//judge if the list is last ,if the list is null ,the return's value is true(1)
}
position find (int x,list L){//look for the present node x
position P;
p = L -> next;
while(P != NULL && P -> data != x)
P = P -> next;
return P;
}
void delete (int x,list L){
position P,tmpcell;
p = findprevious(x,L);
if(!islast(P,L)){
tmpcell = P -> next;
p -> next = tmpcell -> next;
free(tmpcell);
printlist(L);
}
else
printf("the list is null
");
}
position findprevious(x,L){//look for the next node x
position P;
P = L;
while(P -> next != NULL && P -> next -> data != x)
P = P -> next;
return P;
}
void insert(int x,list L,position P){
position tmpcell;
tmpcell = malloc(sizeof(struct node));
if(tmp == NULL)
printf("out of space");
tmpcell -> data = x;
tmpcell -> next = P -> next;
P -> next = tmpcell;
print(L);
}