Description:-
If you are a student of a computer science then u have to do the program of "Single Link List" in the "DATA STRUCTURE" subject. Then this program helps u a lot.
This program has a complete program of single link list with reversing the list(physically).
Process:-
1. Copy the entire source code(Bold in format).
2. Paste that in your c or c++ compiler.
3. Save that with a suitable name.
4. Compile that program.
5. Finally run that program.
Error handling:-
Due to inappropiate html coding done by this web page I can't give the header file
name like stdio.h, conio.h, stdlib.h, iostream.h, fstream.h, iomanip.h etc.
If u get an error report after compiling the program
"CALL TO UNDEFINED FUNCTION 'PRINTF(OR ANYTHING)' IN FUNCTION MAIN()"
please add the above mentioned header file after #include
like #include
Disclaimer:-
If you have any other problem regarding the program please mail me.
My mail address is showing on my blog page.
Source code:-
/* THIS IS THE COMPLETE PROGRAM OF "SINGLE LINKED LIST" */
#include
#include
#include
#include
struct node
{
int data;
struct node * link;
} *start;
void create_list(int);
void showlist();
void add_beg();
void insert(int);
void del();
void del1();
void reverse();
int count();
void main()
{
int ch,i,n,num,c;
clrscr();
start=NULL;
while(1)
{
printf("\n\n\n");
printf("\n1. Create list.");
printf("\n2. Show list.");
printf("\n3. Add at begining.");
printf("\n4. Count how many node?");
printf("\n5. Insert any specified position.");
printf("\n6. Delete.");
printf("\n7. Reverse the list.");
printf("\n8. Delete any node by value.");
printf("\n9. Exit.\n\n");
printf("\nEnter your choice=>");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nHow many number want to create=>");
scanf("%d",&n);
for(i=0;i
printf("Enter the number=>");
scanf("%d",&num);
create_list(num);
}
break;
case 2:
showlist();
break;
case 3:
add_beg();
break;
case 4:
c=count();
printf("\nNo of node=>%d",c);
break;
case 5:
printf("\nIn which position=>");
scanf("%d",&num);
c=count();
if(num>c)
printf("Specified position is not possible!");
else if(num==1)
add_beg();
else
insert(num);
break;
case 6:
del();
break;
case 7:
reverse();
break;
case 8:
del1();
break;
case 9:
exit(0);
}
}
}
void create_list(int val)
{
struct node *q,*tmp;
tmp=(node *)malloc(sizeof(struct node));
tmp->data=val;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}
void showlist()
{
struct node *q;
q=start;
if(start==NULL)
{
printf("List is empty");
return;
}
else
{
printf("\nCreated list=>\n");
while(q!=NULL)
{
printf("%d ",q->data);
q=q->link;
}
printf("\n");
}
}
void add_beg()
{
struct node *tmp;
int num;
printf("\nEnter the number=>");
scanf("%d",&num);
tmp=(node *)malloc(sizeof(struct node));
tmp->data=num;
tmp->link=start;
start=tmp;
}
void insert(int val)
{
struct node *q,*tmp;
int i=1,num;
printf("Enter the number=>");
scanf("%d",&num);
q=start;
while(i!=val)
{
q=q->link;
i++;
}
tmp=(node *)malloc(sizeof(struct node));
tmp->data=num;
tmp->link=q;
q=start;
i=1;
while(i!=val-1)
{
q=q->link;
i++;
}
q->link=tmp;
}
int count()
{
struct node *q;
int cnt=0;
q=start;
if (start==NULL)
printf("\nThere is no node exist");
else
{
while(q!=NULL)
{
cnt++;
q=q->link;
}
}
return cnt;
}
void del()
{
int pos,i=1;
struct node *q,*tmp;
q=start;
printf("\nWhich position=>");
scanf("%d",&pos);
if(pos>count())
printf("\nNode does not exist");
else if(pos==1)
{
while(i!=pos+1)
{
q=q->link;
i++;
}
start=q;
}
else if(pos==count())
{
while(i!=pos-1)
{
q=q->link;
i++;
}
q->link=NULL;
}
else
{
while(i!=pos+1)
{
q=q->link;
i++;
}
tmp=q;
q=start;
i=1;
while(i!=pos-1)
{
q=q->link;
i++;
}
q->link=tmp;
}
}
void reverse()
{
struct node *q,*x,*p;
printf("\nReversed the created list\n");
q=start;
x=NULL;
p=start;
while(x!=p)
{
while(q->link!=x)
q=q->link;
if(p->link!=q->link)
{
p->data=p->data*q->data;
q->data=p->data/q->data;
p->data=p->data/q->data;
x=q;
p=p->link;
q=start;
}
else
return;
}
}
void del1()
{
struct node *q,*r;
int c=1,val;
q=start;
r=start;
printf("\nEnter the value=>");
scanf("%d",&val);
while(q->link!=NULL&&q->data!=val)
{
q=q->link;
c++;
}
if(q->data==val)
{
if(c==1)
{
q=q->link;
start=q;
}
else if(c==count())
{
q=start;
c--;
while(c!=1)
{
q=q->link;
c--;
}
q->link=NULL;
}
else
{
q=q->link;
c--;
while(c!=1)
{
r=r->link;
c--;
}
r->link=q;
}
}
else
printf("\nGiven value does not exist!");
}
Sunday, 14 June 2009
Single Linked List
Posted by san_shinee at 1:16 am
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment