Saturday 19 March, 2011

Write a program to create linear linked list interactively and print out the list and total number of items in the list.


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*head,*last,*temp,*n;
void menu(); // to select the choise
void create(); // insert at last
void display(); // to print the link list
int i;
void main()
{
i=0;
clrscr();
menu();
getch();
}
void menu()
{
int ch;
printf("1. Create\n");
printf("2. Display\n");
printf("3. Exit\n");
printf("Enter choise : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : create();
case 2 : display();
case 3 : printf("You have choose to exit\n");
getch(); exit(0);
default : printf("Invalid choise\n"); menu();
}
}
void create()
{
n= new node;
printf("Enter the information : ");
scanf("%d",&n->info);
if(i==0)
{
head=n;
i++;
}
else
{
last->next=n;
i++;
}
last=n;
last->next=NULL;
menu();
}
void display()
{
if(head==NULL)
{
printf("No information in list\n");
menu();
}
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->info);
temp=temp->next;
}
printf("\n");
printf("Total number of elements in the link list are : %d\n",i);
menu();
}
Output:
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 12
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 23
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 45
1. Create
2. Display
3. Exit
Enter choise : 2
12->23->45->
Total number of elements in the link list are : 3
1. Create
2. Display
3. Exit
Enter choise : 3
You have choose to exit

No comments:

Post a Comment