#include<stdio.h>
#include<conio.h>
/*contains malloc & free functions */
#define NULL 0
struct node{
int info;
struct node *next;
};
typedef struct node nodeptr;
int menu(void);
void insertEnd(nodeptr **, int);/*insert to the end of the list*/
void deleteEnd(nodeptr **, int *);/*delete from the end " " "*/
void displayList(nodeptr *);
nodeptr *getnode(void);
void freenode(nodeptr *);
int main()
{
int choice,x;
nodeptr *List= NULL; /*List points the beginning of the list*/
do{
choice = menu();
switch(choice){
case 1:
printf("Enter new data item:");
scanf("%d",&x);
insertEnd(&List,x);
break;
case 2:
deleteEnd(&List,&x);
printf("The deleted data item is: %d",x);
break;
case 3:
displayList(List);
break;
default:
if(choice == 4)
printf("\nEnd of the Program");
else
printf("\nEnter the choice again");
break;
}
}while(choice !=4);
return 0;
}
/*
Use double pointer if you are changing the
content of the List which is a pointer to the
beginning of the Linked List.
Use single pointer if you are not changing the
content of the List which is a pointer to the
beginning of the Linked List.*/
int menu(void)
{
int c;
printf("\nMain Menu:\n");
printf("1 - Insert an item\n");
printf("2 - Delete an item\n");
printf("3 - Display the Linked List items\n");
printf("4 - Quit\n\n");
scanf("%d",&c);
return c;
}
void insertEnd(nodeptr **plist, int x)
{
nodeptr *p, *q;
p=getnode();
p->info = x;
p->next = NULL;
if(*plist == NULL)
*plist = p;
else{
q = *plist;
while(q->next != NULL)
q = q->next;
q->next = p;
}
}
void deleteEnd(nodeptr **plist, int *px)
{
nodeptr *p,*q;
if(*plist == NULL){
printf("void deletion\n");
return;
}
p = *plist; /*current node*/
q = p->next; /*the node after the current node*/
if(q == NULL){
*px = p->info; /*the data of the deleted element*/
freenode(p);
*plist = NULL;
}
else{
while(q->next != NULL){
p = p->next; /*go to the next node*/
q = p->next;
}
p->next = NULL;
*px = q->info; /*the data of the deleted element*/
freenode(q);
}
}
/*
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
*/
void displayList(nodeptr *plist)
{
nodeptr *p;
p = plist; /*current node*/
if(p == NULL){
printf("no node/data is available in the list\n");
return;
}
while(p!= NULL){
printf("%5d", p->info); /*prints the current data*/
p = p->next;
}
}
nodeptr *getnode(void)
{
nodeptr *p;
p = (nodeptr *) malloc(sizeof(nodeptr));
return p;
}
void freenode(nodeptr *p)
{
free(p);
}
#include<conio.h>
/*contains malloc & free functions */
#define NULL 0
struct node{
int info;
struct node *next;
};
typedef struct node nodeptr;
int menu(void);
void insertEnd(nodeptr **, int);/*insert to the end of the list*/
void deleteEnd(nodeptr **, int *);/*delete from the end " " "*/
void displayList(nodeptr *);
nodeptr *getnode(void);
void freenode(nodeptr *);
int main()
{
int choice,x;
nodeptr *List= NULL; /*List points the beginning of the list*/
do{
choice = menu();
switch(choice){
case 1:
printf("Enter new data item:");
scanf("%d",&x);
insertEnd(&List,x);
break;
case 2:
deleteEnd(&List,&x);
printf("The deleted data item is: %d",x);
break;
case 3:
displayList(List);
break;
default:
if(choice == 4)
printf("\nEnd of the Program");
else
printf("\nEnter the choice again");
break;
}
}while(choice !=4);
return 0;
}
/*
Use double pointer if you are changing the
content of the List which is a pointer to the
beginning of the Linked List.
Use single pointer if you are not changing the
content of the List which is a pointer to the
beginning of the Linked List.*/
int menu(void)
{
int c;
printf("\nMain Menu:\n");
printf("1 - Insert an item\n");
printf("2 - Delete an item\n");
printf("3 - Display the Linked List items\n");
printf("4 - Quit\n\n");
scanf("%d",&c);
return c;
}
void insertEnd(nodeptr **plist, int x)
{
nodeptr *p, *q;
p=getnode();
p->info = x;
p->next = NULL;
if(*plist == NULL)
*plist = p;
else{
q = *plist;
while(q->next != NULL)
q = q->next;
q->next = p;
}
}
void deleteEnd(nodeptr **plist, int *px)
{
nodeptr *p,*q;
if(*plist == NULL){
printf("void deletion\n");
return;
}
p = *plist; /*current node*/
q = p->next; /*the node after the current node*/
if(q == NULL){
*px = p->info; /*the data of the deleted element*/
freenode(p);
*plist = NULL;
}
else{
while(q->next != NULL){
p = p->next; /*go to the next node*/
q = p->next;
}
p->next = NULL;
*px = q->info; /*the data of the deleted element*/
freenode(q);
}
}
/*
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
*/
void displayList(nodeptr *plist)
{
nodeptr *p;
p = plist; /*current node*/
if(p == NULL){
printf("no node/data is available in the list\n");
return;
}
while(p!= NULL){
printf("%5d", p->info); /*prints the current data*/
p = p->next;
}
}
nodeptr *getnode(void)
{
nodeptr *p;
p = (nodeptr *) malloc(sizeof(nodeptr));
return p;
}
void freenode(nodeptr *p)
{
free(p);
}
No comments:
Post a Comment