BASIC FUNCTIONS OF SINGLY LINK LIST:
#include<stdio.h>#include<stdlib.h>
void add_begining();
void add_after();
void count();
void display();
void search();
void add_end();
void reverse();
void del_begining();
void del_end();
void del_by_position();
void sort();
struct node
{
int data;
struct node *next;
}*start=NULL;
void add_begining()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("\n enter data");
scanf("%d",&temp->data);
if(start==NULL)
temp->next=NULL;
else
temp->next=start;
start=temp;
}
void add_after()
{
struct node *q,*temp;
int i,n;
q=start;
printf("\n enter the node after which you want to add");
scanf("%d",&n);
for(i=0;i<n-1;i++)
{
q=q->next;
if(q==NULL)
{
printf("enough node not present");
return;
}
}
temp=(struct node*)malloc(sizeof(struct node));
temp->next=q->next;
q->next=temp;
printf("\n enter data");
scanf("%d",&temp->data);
}
void count()
{
struct node *q;
int c=0;
q=start;
while(q!=NULL)
{
c++;
q=q->next;
}
printf("\n total number of node present %d \n",c);
}
void search()
{
struct node *q;
int s,f=0,p=0;
q=start;
printf("\n enter the data you want to search");
scanf("%d",&s);
while(q!=NULL)
{
p++;
if(q->data!=s)
{
q=q->next;
}
else
{
f=1;
break;
}
}
if(f==1)
{
printf("\ndata found at node %d \n",p);
}
else
{
printf("data not found\n");
}
}
void add_end()
{
struct node *temp,*q;
if(start==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&temp->data);
temp->next==NULL;
start=temp;
return;
}
q=start;
while(q->next!=NULL)
{
q=q->next;
if(q->next==NULL)
{
break;
}
}
temp=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&temp->data);
q->next=temp;
temp->next=NULL;
}
void reverse()
{
struct node *p1,*p2,*p3,*q;
p1=start;
p2=p1->next;
p3=p2->next;
p1->next=NULL;
p2->next=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->next;
p2->next=p1;
}
start=p2;
q=start;
if(start==NULL)
{
printf("no node present \n");
}
while(q!=NULL)
{
printf("\n %d ",q->data);
q=q->next;
}
}
void del_begining()
{
struct node *q;
q=start;
if(start==NULL)
{
printf("no node to delete");
return;
}
start=start->next;
printf("deleted item is %d",q->data);
free(q);
printf("after deletion the list looks like \n ");
display();
}
void del_end()
{
struct node *p,*q;
if(start==NULL)
{
printf("no node to delete");
return;
}
q=start;
while(q->next!=NULL)
{
p=q;
q=q->next;
}
printf("deleted data is %d",q->data);
p->next=NULL;
free(q);
printf("after deletion the list looks like");
display();
}
void del_by_position()
{
struct node *p,*q;
int pos,i;
printf("enter the position of node that you want to delete");
scanf("%d",&pos);
p=start;
q=start->next;
if(start==NULL)
{
printf("no node to delete");
return;
}
if(start->next==NULL)
{
del_begining();
return;
}
for(i=1;i<pos-1;i++)
{
p=p->next;
q=q->next;
if(q==NULL)
{
printf("desired node is not present");
return;
}
}
printf("deleted data is %d",q->data);
p->next=q->next;
free(q);
printf("after deletion the list looks like");
display();
}
void display()
{
struct node *q;
q=start;
if(start==NULL)
{
printf("no node present \n");
}
while(q!=NULL)
{
printf("\n %d ",q->data);
q=q->next;
}
}
int main()
{
int ch;
while(1)
{
printf("\n enter 1 to add node at the begining: \n");
printf("enter 2 to add node after a node:\n");
printf("enter 3 to count the number of nodes:\n");
printf("enter 4 to search a data:\n");
printf("enter 5 to display:\n");
printf("enter 6 to add node at end:\n");
printf("enter 7 to reverse:\n");
printf("enter 8 to delete the begining node:\n");
printf("enter 9 to delete the end node:\n");
printf("enter 10 to delete the node by position:\n");
printf("enter 12 to exit:");
printf("enter your choice \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
add_begining();
break;
case 2:
add_after();
break;
case 3:
count();
break;
case 4:
search();
break;
case 5:
display();
break;
case 6:
add_end();
break;
case 7:
reverse();
break;
case 8:
del_begining();
break;
case 9:
del_end();
break;
case 10:
del_by_position();
break;
case 12:
exit(0);
}
}
return 0;
}
OUTPUT:
No comments:
Post a Comment