Sunday, January 26, 2014

C Program to illustrate Linked List Implementation of List

with 0 Comment
C Program to illustrate Linked List Implementation of List:

Main Program:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<alloc.h>
#include "c:\tc\bin\sublll.c"
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.Create\n2.Insert\n3.Delete\n4.Transverse\n5.Search\n6.Exit\n");
printf("Enter your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
printf("After Creation");
display();
break;
case 2:insertion();
printf("After Insertion");
display(); break;
case 3:deletion();
printf("after deletion");
display();
break;
case 4:printf("Linked list is");
display();
break;
case 5:search();
break;
case 6:exit(0);
}
}
}

SubProgram: (Saved as 'sublll.c' )

struct list
{
int info;
struct list *next;
}*head,*p,*n;
typedef struct list node;
void create()
{
int no,x,i;
head=(node*)malloc(sizeof(node));
head->next=NULL;
p=head;
printf("\nEnter the No. of Elements");
scanf("%d",&no);
printf("\nEnter the Elements");
for(i=1;i<=no;i++)
{
n=(node*)malloc(sizeof(node));
scanf("%d",&x);
n->info=x;
p->next=n;
p=n;
}
p->next=NULL;
}
void insertion()
{
int x,pre;
node *n;
n=(node*)malloc(sizeof(node));
printf("\nEnter the element");
scanf("%d",&x);
printf("Enter the previous element");
scanf("%d",&pre);
if(pre==0)
{
n->info=x;
n->next=head->next;
head->next=n;
}
else
{
for(p=head->next;p!=NULL&&p->info!=pre;p=p->next);
n->info=x;
n->next=p->next;
p->next=n;
}
}
void deletion()
{
int x;
node *t;
printf("\nEnter the element to be deleted:");
scanf("%d",&x);
for(p=head->next;p!=NULL&&p->info!=x;p=p->next);
for(t=head;t->next!=NULL&&t->next!=p;t=t->next);
t->next=p->next;
free(p);
}
void display()
{
p=head->next;
while(p->next!=NULL)
{
printf("%d",p->info);
p=p->next;
}
printf("%d",p->info);
}
void search()
{
int x;
printf("\nEnter the element to be searched");
scanf("%d",&x);
for(p=head->next;p!=NULL&&p->info!=x;p=p->next);
if(p==NULL)
printf("\nData not present");
else
printf("\nData present");
}

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive