Monday, June 4, 2012

Menu based program on C linked list


In this a menu based C linked list program, all the operations, inserting new node in the linked list and deletion of node from the linked list are done in this program. Insertion and deletion can be performed on any location of the linked list.

#include< stdio.h>
#include< stdlib.h>
struct tag
{
int a;
struct tag *next;
};
struct tag *p,*node,*new1;
void addL(struct tag *);
void display(struct tag *);
struct tag *addF(struct tag *);
struct tag *delF(struct tag *);
void delL(struct tag *);
struct tag *delA(struct tag *);
struct tag *insA(struct tag *);
void main()
{
struct tag *start;
char ch='a';
clrscr();
start=(struct tag *)malloc(sizeof(struct tag));
printf("\n enter the value =");
scanf("%d",&start->a);
start->next=NULL;
while(ch!='e')
{
do
{
printf("\n press 'c' for add at last location,\n'a'for add at first location,\n'f'for delete from first location,\n 'l' for delete from last location ,\n 'i' fot insertion at any location,\n 'd' for delete at any location ,\n 'e' for exit");
ch=tolower(getche());
clrscr();
}while(strchr("caflide",ch)==NULL);
if(ch=='c')
{
addL(start);
display(start);
}
else if(ch=='a')
{
start=addF(start);
display(start);
}
else if(ch=='f')
{
start=delF(start);
display(start);
}
else if(ch=='l')
{
delL(start);
display(start);
}
else if(ch=='i')
{
start=insA(start);
display(start);
}
else if(ch=='d')
{
start=delA(start);
display(start);
}
else
break;
}
}
void addL(struct tag *n)
{
node=n;
while(node->next!=NULL)
{
node=node->next;
}
node->next=(struct tag *)malloc(sizeof(struct tag));
node=node->next;
printf("\n value=");
scanf("%d",&node->a);
node->next=NULL;
}
void display(struct tag *n)
{
node=n;
printf("\nPresently the list is\n");
while(node)
{
printf("%4d",node->a);
node=node->next;
}
}
struct tag *addF(struct tag *n)
{
node=n;
new1=(struct tag *)malloc(sizeof(struct tag *));
printf("\n value=");
scanf("%d",&new1->a);
new1->next=node;
return new1;
}
struct tag *delF(struct tag *n)
{
node=n;
p=node->next;
free(node);
return p;
}
void delL(struct tag *n)
{
node=n;
p=node->next;

while(p->next!=NULL)
{
p=p->next;
node=node->next;
}
node->next=p->next;
free(p);
}
struct tag *delA(struct tag *n)
{
int c=1,count;
node=n;
p=node->next;
printf("\n which location:");
scanf("%d",&count);
if(c==count)
{
free(node);
n=p;
}
else
{
while(p->next!=NULL)
{
c++;
if(c==count)
break;
p=p->next;
node=node->next;
}
node->next=p->next;
free(p);
}
return n;
}
struct tag *insA(struct tag *n)
{
int c=1,count;
node=n;
p=node->next;
printf("\n in which location:");
scanf("%d",&count);
new1=(struct tag *)malloc(sizeof(struct tag));
printf("\n value=");
scanf("%d",&new1->a);
if(c==count)
{
new1->next=node;
n=new1;
}
else
{
while(p)
{
c++;
if(c==count)
break;
p=p->next;
node=node->next;
}
node->next=new1;
new1->next=p;
}
return n;
}

 Technical analysis of the above menu based linked list program

Total number of functions defined in this linked list program is six. They are as follows
void addL(struct tag *);
This function is used for inserting new node at last location of the linked list.

void display(struct tag *);
This function is used to display the linked list after every operation.

struct tag *addF(struct tag *);
This function is used for inserting new node at first location of the linked list.

struct tag *delF(struct tag *);
This function is used for deleting node from the first location of the linked list.

void delL(struct tag *);
This function is used for deleting node from last location of the linked list.

struct tag *delA(struct tag *);
This function is used for deletion of any specified node of the linked list.

struct tag *insA(struct tag *);
This function is used for inserting new node at any location of the linked list.

Within main () function user choice is taken and accordingly functions are called to perform the necessary job in the linked list.

About the function calling statement ‘strchr("caflide",ch)==NUL’ – this is a predefined function in string.h header file which is used to search a specific character from a string. The first argument in the function is the string and second argument is the specific character. If the search is successful, it returns the address of the character in the string otherwise returns NULL.

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner