Tuesday, May 19, 2009

Singly Link List

/* Title: Database creation using singly link list

Problem Statement: Write a program to create database using singly link list with options like create, insert, delete, modify, print reverse.
*/

#include
#include
#include

struct node
{
int data;
struct node *next;
}*head;

int create();
int insertloc(int a,int b);
int insertdata(int a,int b);
void del(int a);
void display();
void reverse();


void main()
{
int x,y,ch,s,ins;
head=NULL;
clrscr();
do
{
printf("\n\n-------Menu-------");
printf("\n1.Create database");
printf("\n2.Insert a node");
printf("\n3.Delete a node");
printf("\n4.Reverse");
printf("\n5.Exit");
printf("\nEnter your choice : ");
scanf("%d",&ch);
printf("\n");
switch(ch)
{
case 1:
s=create();
if(s != 0)
printf("\nError in creating list");
display();
break;

case 2:
printf("\n1:Insert by location");
printf("\n2:Insert by data");
printf("\nEnter your choice : ");
scanf("%d",&ins);
switch(ins)
{
case 1:
printf("\nEnter location at which node is to be inserted(from 1) : ");
scanf("%d",&x);
printf("\nEnter data to be inserted:");
scanf("%d",&y);
s=insertloc(x,y);
break;

case 2:
printf("\nEnter data after which node is to be inserted:");
scanf("%d",&x);
printf("\nEnter data to be inserted:");
scanf("%d",&y);
s=insertdata(x,y);
break;
}

if(s != 0)
{
printf("\nFailure to create or insert node");
display();
}
else
display();
break;

case 3:
printf("\nEnter data to be deleted:");
scanf("%d",&x);
del(x);
display();
break;

case 4:
reverse();
display();
break;
}
} while(ch != 5);
}

int create()
{
int i,size,r=0,ch;
struct node *p,*q;
p=(struct node*)malloc(sizeof(struct node));
if(!p)
r=1;
printf("\nEnter first data in link list:");
scanf("%d",&p->data);
head=p;
printf("\n\nDo you want to insert more data?(1:yes/0:no):");
scanf("%d",&ch);
p=head;
while(ch != 0)
{
q=(struct node*)malloc(sizeof(struct node));
if(!q)
r=1;
printf("\n\nEnter the data to be entered : ");
scanf("%d",&q->data);
p->next=q;
q->next=NULL;
p=q;
printf("\n\nDo you want to insert more data?(1:yes/0:no):");
scanf("%d",&ch);
}
return r;
}

int insertloc(int l,int d)
{
struct node *p,*q;
int r=0,i,n;
if(head==NULL)
return 1;
p=(struct node*)malloc(sizeof(struct node));
if(!p)
return 1;
p->data=d;
if(l==1)
{
p->next=head;
head=p;
return r;
}
q=head;
n=l-2;
while(q && n>0)
{
q=q->next;
n--;
}
p->next=q->next;
q->next=p;
return r;
}

int insertdata(int data,int d)
{
struct node *p,*q;
int r=0,i,n;
if(head==NULL)
return 1;
p=(struct node*)malloc(sizeof(struct node));
if(!p)
return 1;
p->data=d;
q=head;
while(q && q->data != data)
{
q=q->next;
}
if(q == NULL)
return 1;
p->next=q->next;
q->next=p;
return r;
}

void del(int d)
{
struct node *p,*q;
if(head->data== d)
{
p=head;
head=head->next;
free(p);
return ;
}
p=head;
while(p->next->data != d && p->next)
{
p=p->next;
}
if(p->next->data == d)
{
q=p->next;
p->next=p->next->next;
free(q);
}
}


void display()
{
struct node *p;
if(head== NULL)
return;
p=head;
printf("\nThe list is:");
while(p)
{
printf("\t%d",p->data);
p=p->next;
}
}

void reverse()
{
struct node *p,*q,*r;
if(head==NULL)
return;
if(head->next == NULL)
return;
p=head;
q=p->next;
if(q->next==NULL)
{
q->next=p;
p->next=NULL;
head=q;
return;
}
r=q->next;
while(r)
{
q->next=p;
p=q;
q=r;
r=q->next;
}
q->next=p;
head->next=NULL;
head=q;
}

No comments:

Post a Comment