New Member FAQ | Forums | Earn Revenue


Resources Entrance Ask Experts Exam Papers Jobs English Projects Universities Colleges Courses Schools Training My India



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



C program to perform different operations on SINGLE LINK LIST


Posted Date:     Total Responses: 0    Posted By: pankaj agarwala   Member Level: Gold   Points/Cash: 6   


C program to perform Append, Delete ,Traverse, Update, Insert, Split, Reverse,Sort of Single Link List





#include
#include
#include

struct node
{
int no;
struct node *next;
};
struct node * start=NULL,*start1;

void Append()
{
struct node *p=start,*n;
n=(struct node*)malloc(sizeof(struct node)) ;
printf("\n enter no.");
scanf("%d",&n->no);
n->next=NULL;
if(start==NULL)
{
start=n;
return;
}
while(p->next!=NULL)
{
p=p->next;
}
p->next=n;

}

void Traverse(struct node *p)
{
if(p==NULL)
{
printf("link list empty");
return;
}
while(p!=NULL)
{
printf("\t %d",p->no);
p=p->next;
}
}


void Search()
{
struct node*p=start;
int val;
if(start==NULL)
{
printf("empty link list");
return;
}
printf("enter the element to be search");
scanf("%d",&val);
while(p!=NULL)
{
if(val==p->no)
{
printf("\nFOUND");
return;
}
p=p->next;
}
printf("\n%d NOt found");
}



void Update()
{
struct node*p=start;
int val;
if(start==NULL)
{
printf("emptyy link list");
return;
}

printf("\enter element to be updated");
scanf("%d",&val);
while(p!=NULL)
{
if(val == p->no)
{
printf("enter new no");
scanf("%d",&p->no);
printf("\n updated");
return;


}
p=p->next;


}

printf("\n%d NOT found");

}



void del_all()
{
struct node *p;
while(start!=NULL)
{
p=start;
start=start->next;
free(p);
}
}

void Insertafter()
{
struct node *p=start,*n;
int val;
if(start==NULL)
{
printf("emty link list");
return;
}

printf("\n enter no after which you want to insert");
scanf("%d",&val);
while(p!=NULL)
{
if(val==p->no)
{
n=(struct node*)malloc(sizeof(struct node));
printf("\nEnter No");
scanf("%d",&n->no);
n->next=p->next;
p->next=n;
printf("\n Node inserted");
return;
}
p=p->next;
}
printf("\n%d Not found",val);
}




void insertbefore()
{
struct node *p=start,*n;
int val;
if(start==NULL)
{
printf("empty linklist");
return;
}
printf("\n enter no before whinch you want to insert");
scanf("%d",&val);
if(val==start->no)
{
n=(struct node*)malloc(sizeof(struct node));
printf("enter no");
scanf("%d",&n->no);
n->next=start;
start=n;
return;
}
while(p->next!=NULL)
{
if(val==p->next->no)
{
n=(struct node*)malloc(sizeof (struct node));
printf("\n E#nter no");
scanf("\d ",&n->no);
n->next=n;
printf("\n Node inserted");
return;
}
p=p->next;
}
printf("%d not found",val);
}

void Split_even_odd()
{
struct node *p=start,*q;
int non=0,i;
if(start==NULL)
{
printf("empty link list");
return;
}
else if(start->next==NULL)
{
printf("single node");
return;
}
while(p!=NULL)
{
non++;
p=p->next;
}
start1=start->next;
q=start1;
for(p=start,i=0;i {
p->next=q->next;
p=p->next;
q->next=p->next;
q=q->next;

}
printf("\nODD node");
Traverse(start);
printf("\nEVEN node");
Traverse(start1);
}

void reverse()
{
struct node *p = start,*q,*r;
q=p->next;
r=q->next;
start->next=NULL;

while(q!=NULL)
{
q->next=p;
p=q;
q=r;
r=r->next;
}

start=p;

printf("\nrreverse node are");
Traverse(start);
}


void Insert_by_pos()
{
struct node *p=start,*n;
int pos,non=0,i;
if(start==NULL)
{
printf("\nempty linklist");
return;
}
printf("enter the pos where node to be inserted");
scanf("%d",&pos);
while(p!=NULL)
{non++;
p=p->next;
}
if(pos<1||pos>non+1)
{
printf("invalid postion");
return;
}
if(pos==1)
{
n=(struct node*)malloc(sizeof(struct node));
printf("\nentet no");
scanf("%d",&n->no);
n->next=start;
start=n;
return;
}
else
{ for(p=start,i=0;i {
p=p->next;
}
n=(struct node*)malloc(sizeof(struct node));
printf("\nEnter no");
scanf("%d",&n->no);
n->next=p->next;
p->next=n;
}
}

void delByValue()
{
struct node *p=start,*d;
int val;
if(start==NULL)
{
printf("\n empty link list");
return;
}
printf("\n enter no to be deleted");
scanf("%d",&val);
if (val==start->no)
{
start=start->next;
free(p);
printf("Node deleted");
return;
}
while(p->next!=NULL)
{
if (val==p->next->no)
{
d=p->next;
p->next=d->next;
free(d);
printf("node deleted");
return;
}
p=p->next;
}
printf("\n %d Not found",val);
}





void sort()
{
struct node *p,*q;
int temp;
if(start==NULL)
{
printf("\n empty linklist");
return;
}
for(p=start;p->next!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(q->nono)
{
temp=p->no;
p->no=q->no;
q->no=temp;
}
}
}
}





void split()
{
struct node *p=start;
int i,non=0;
if(start==NULL)
{
printf("\nEmpty link list");
return;
}
while(p!=NULL)
{
non++;
p=p->next;
}
for(p=start,i=1;i {
p=p->next;
}
start1=p->next;
p->next=NULL;
printf("\n first link list");
Traverse(start);
printf("\n second lnklist");
Traverse(start1);
}





void sum()
{
struct node*p=start,*q;
int sum=0,i;

while(p!=NULL)
{
for(q=p->next,i=1;i<5&&q!=NULL ;i++)
{
q=q->next;
}
sum=sum+p->no*q->no;
p=p->next;


}
printf("SUM is %d " ,sum);
}




void main()
{
int ch;
clrscr();
do
{
printf("\n\n\n 1 Append");
printf("\n 2 Traverse");
printf("\n 3 Search");
printf("\n 4 update");
printf("\n 5 insertafter");
printf("\n 6 insertbefore");
printf("\n 7 Split Even Odd Node");
printf("\n 8 Reverse");
printf("\n 9 Insert_by_pos");
printf("\n 10 dell_all");
printf("\n 11 delete by value");
printf("\n 12 SOrt");

printf("\n 13 SPLit");
printf("\n 14 SUm");


printf("\n 0 exit");

printf("\nenter your choice");
scanf("%d",&ch);

switch(ch)
{
case 1: Append();

break ;
case 2: Traverse(start);
break;
case 3: Search();
break;
case 4: Update();
break;
case 5: Insertafter();
break;
case 6: insertbefore();
break;
case 7: Split_even_odd();
break;
case 8: reverse();
break;
case 9: Insert_by_pos();
break;
case 10: del_all();
break;

case 11: delByValue();
break;
case 12: sort();
break;

case 13: split();
break;
case 14: sum();
break;


case 0: del_all();

break;
default: printf("\n invalid choice");
}


}while(ch!=0);
getch();
}



Project Feedbacks


No feedbacks found. Be the first to respond and make money from revenue sharing program.

Post Feedback
You must Sign In to post a feedback.
Next Project: Type of Triangle
Previous Project: Intersection of two LINK LIST using C

Return to Project Index

Post New Project


Related Projects



Advertise Here





Contact Us   Advertise   Editors    Privacy Policy    Terms Of Use   

ISC Technologies.
2006 - 2009 All Rights Reserved.