| Re: Linked list ..help //Program for linked list
#include
#define TRUE 1
struct node{
int info;
struct node *next;
};
typedef struct node *nodeptr;
nodeptr p,tp;
void startlist() {
int x;
p= getnode();
tp = p;
printf("Enter an item to insert:");
scanf("%d",&x);
p->info = x;
p->next = NULL;
}
void showlist() {
p = tp;
printf("node(p)\t Info\t Next");
while(p!= NULL) {
printf("\n %d \t%d \t%d ",p,p->info,p->next);
p=p->next;
}
putchar('\n');
}
void insafter(noteptr p,int x) {
nodeptr a;
if(p==NULL) {
printf("void insertion");
exit(1);
}
q= getnode();
q->info = x;
q->next = p->next;
p->next = q;
}
nodeptr getnode() {
nodeptr p;
p =(nodeptr) malloc(sizeof(struct node));
return(p);
}
void freenode(nodeptr p) {
free(p);
}
void deleteafter(nodeptr p) {
nodeptr q;
if(p==NULL ::q->next ==NULL) {
printf("void deletion");
exit(1);
}
q=q->next;
p->next = q->next;
freenode(q);
}
void main() {
int choice,i,x;
nodeptr n;
nodeptr getnode();
clrscr();
printf("\n Enter Ur choice 1-> start 2->showlist 3->insert 4->delete 5->end \n");
while (TRUE) {
printf("Enter ur choice");
scanf("%d",&choice);
switch(choice) {
case 1: startlist(); break;
case 2: showlist(); break;
case 3: printf ("Node Insertion: after which node?:");
scanf("%d",&n);
p=n;
printf("Enter the item for insertion:");
scanf("%d",&x);
insafter(p,x);
break;
case 4: printf("Node deletion:After which node?:");
scanf("%d",&n); p=n;
delafter(p);break;
case 5: exit(5);
}
}
} |