DS - SLL
Singly Linked List:
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_at_begin();
void insert_at_end();
void insert_at_pos();
void delete_at_begin();
void delete_at_end();
void delete_at_pos();
void search();
void sort();
void count();
void reverse();
void merge();
int i,value,p;
char ch;
struct node
{
int data;
struct node *next;
}*new,*head,*temp,*d,*new1,*temp1;
void main()
{
int n;
printf("\n 1.create\n 2.display\n 3.insert-at-begin\n 4.insert-at-end\n 5.insert-at-pos\n 6.delete-at-begin\n 7.delete-at-end\n 8.delete-at-pos\n 9.search\n 10.sort\n 11.count\n 12.reverse\n 13.merge");
while(1)
{
printf("\n enter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert_at_begin();
break;
case 4: insert_at_end();
break;
case 5: insert_at_pos();
break;
case 6: delete_at_begin();
break;
case 7: delete_at_end();
break;
case 8: delete_at_pos();
break;
case 9: search();
break;
case 10: sort();
break;
case 11: count();
break;
case 12: reverse();
break;
case 13: merge();
break;
default:exit(1);
}
}
}
void create()
{
do
{
new=(struct node *)malloc(sizeof(struct node));
printf("enter a value");
scanf("%d",&value);
new->data=value;
new->next=NULL;
if(head==NULL)
{
head=new;
temp=new;
}
else
{
temp->next=new;
temp=temp->next;
}
printf("\n do u want to add one more node to the list(y/n)? ");
scanf("%c",&ch);
}while(ch=='y');
}
void display()
{
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
void insert_at_begin()
{
new=(struct node *)malloc(sizeof(struct node));
printf("enter a value");
scanf("%d",&value);
new->data=value;
if(head == NULL)
{
new->next = NULL;
head = new;
}
else
{
new->next = head;
head = new;
}
printf("\nOne node inserted!!!\n");
}
void insert_at_end()
{
new=(struct node *)malloc(sizeof(struct node));
printf("enter a value");
scanf("%d",&value);
new->data=value;
new->next=NULL;
if(head == NULL)
head = new;
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
}
printf("\nOne node inserted!!!\n");
}
void insert_at_pos()
{
temp=head;
new=(struct node *)malloc(sizeof(struct node));
printf("enter a value");
scanf("%d",&value);
printf("enter a position");
scanf("%d",&p);
new->data=value;
if(head == NULL)
{
new->next = NULL;
head = new;
}
else
{
temp = head;
for(i=1;i<p-1;i++)
{
temp=temp->next;
}
new->next=temp->next;
temp->next=new;
}
printf("\nOne node inserted!!!\n");
}
void delete_at_begin()
{
if(head == NULL)
printf("\n\nList is Empty!!!");
else
{
temp = head;
if(head->next == NULL)
{
head = NULL;
free(temp);
}
else
{
temp = head;
printf("%d-> is deleted",head->data);
head=head->next;
temp->next=NULL;
free(temp);
}
}
}
void delete_at_end()
{
if(head == NULL)
{
printf("\nList is Empty!!!\n");
}
else
{
temp = head;
if(head->next == NULL)
head = NULL;
else
{
while(temp->next->next!=NULL)
{
temp=temp->next;
}
d=temp->next;
temp->next=NULL;
printf("End node is deleted");
free(d);
}
}
}
void delete_at_pos()
{
temp=head;
printf("enter a position");
scanf("%d",&p);
for(i=1;i<p-1;i++)
{
temp=temp->next;
}
d=temp->next;
temp->next=d->next;
printf("Node deleted Sucessfully");
d->next=NULL;
free(d);
}
void search()
{
int key,flag=0;
printf("enter the value to search");
scanf("%d",&key);
temp=head;
while(temp!=NULL)
{
if(key==temp->data)
{
flag+=1;
}
else
{
temp=temp->next;
}
}
if(flag==1)
{
printf(" found");
}
else
{
printf("not found");
}
}
void sort()
{
int temp;
struct node *temp1,*temp2;
for(temp1=head;temp1->next!=NULL;temp1=temp1->next)
{
for(temp2=head;temp2->next!=NULL;temp2=temp2->next)
{
if(temp2->data>temp1->data)
{
temp=temp2->data;
temp2->data=temp2->next->data;
temp2->next->data=temp;
}
}
}
}
void count()
{
int count=0;
temp=head;
while(temp!=NULL)
{
count=count+1;
temp=temp->next;
}
printf("\n number of nodes=%d",count);
}
void reverse()
{
struct node *prev=NULL,*current,*next;
current=head;
while(current!=NULL)
{
next=current->next;
current->next=prev;
prev=current;
current=next;
}
head=prev;
}
void merge()
{
int value;
struct node *head1=NULL;
do
{
new1=(struct node *)malloc(sizeof(struct node));
printf("enter a value");
scanf("%d",&value);
new1->data=value;
new1->next=NULL;
if(head1==NULL)
{
head1=new1;
temp1=new1;
}
else
{
temp1->next=new1;
temp1=temp1->next;
}
printf("\n do u want add one more node(y/n)");
scanf("%c",&ch);
}while(ch=='y');
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head1;
}
This comment has been removed by the author.
ReplyDelete