(DS-2)
2A.Write a program to create a single linked list and display the node elements in reverse order.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node* create(struct node*);
struct node* print(struct node*);
struct node* reverse(struct node*);
int main()
{
int op;
struct node *start=NULL;
printf("--------Practical 2A [Menu Driven]-------\n");
do
{
printf("\n1->Create the linked list \n2->Print the linked list \n3->reverse the linked list \n4->Exit");
printf("\nEnter a choice : ");
scanf("%d",&op);
switch(op)
{
case 1:start=create(start);
break;
case 2:start=print(start);
break;
case 3:start=reverse(start);
break;
case 4:
break;
}
}while (op!=4);
return 0;
}
struct node *create(struct node *start)
{
int val;
struct node *newnode, *temp, *pre, *post;
printf("Enter value :");
scanf("%d",&val);
do
{
newnode=(struct node*)malloc(sizeof (struct node));
newnode->data=val;
if(start==NULL)
{
start=newnode;
newnode->next=NULL;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=NULL;
}
printf("\nEnter val or -1 to Exit :");
scanf("%d",&val);
}while(val!=-1);
return start;
}
//Reversing the linked list..
struct node *reverse(struct node *start)
{
struct node *pre=NULL, *temp=start, *post;
while(temp!=NULL)
{
post=temp->next;
temp->next=pre;
pre=temp;
temp=post;
}
post=NULL;
temp=NULL;
start=pre;
return start;
}
//Printing the linked list..
struct node *print(struct node *start)
{
struct node *temp=start;
printf("\nThe linked list is :");
while(temp!=NULL)
{
printf("\t %d",temp->data);
temp=temp->next;
}
return start;
}
2B.Write a program to search the elements in the linked list and display the same
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node* create(struct node*);
struct node* print(struct node*);
struct node* search(struct node*);
int main()
{
int op;
struct node* start=NULL;
printf("-----------Menu Driven---------\n");
do
{
printf("\n1.Create the linked list \n2.Display the linked list \n3.Search the element in linked list \n4.Exit");
printf("\nEnter a choice : ");
scanf("%d",&op);
switch(op)
{
case 1:start=create(start);
break;
case 2:start=print(start);
break;
case 3:start=search(start);
break;
case 4:
break;
}
}
while(op!=4);
return 0;
}
//creating the linked list
struct node *create(struct node *start)
{
int val;
struct node *newnode, *temp;
printf("\nEnter value :");
scanf("%d",&val);
do
{
newnode=(struct node*)malloc(sizeof (struct node));
newnode->data=val;
if(start==NULL)
{
start=newnode;
newnode->next=NULL;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=NULL;
}
printf("\nEnter val or -1 to Exit :");
scanf("%d",&val);
}while(val!=-1);
return start;
}
//printing created linked list..
struct node *print(struct node *start)
{
struct node *temp=start;
printf("\nThe linked list is :");
while(temp!=NULL)
{
printf("\t %d",temp->data);
temp=temp->next;
}
return start;
}
//searching the element in linked list...
struct node *search(struct node *start)
{
int val,count=1;
struct node *temp=start;
printf("\nEnter the element you want to search in linked list:");
scanf("%d",&val);
while(temp->data!=val)
{
if(temp->next==NULL)
{
break;
}
else
{
count++;
temp=temp->next;
}
}
if(temp->data==val)
{
printf("\nThe element %d is found at node %d ",val,count);
}
else
{
printf("\nThe element %d is not found in the linked list !!",val);
}
return start;
}
2C.Write a program to create double linked list and sort the elements in the linked list.
#include<stdlib.h>
#include<stdio.h>
struct node
{
int data;
struct node *next,*prev;
};
struct node* create(struct node*);
struct node* display(struct node*);
struct node* sort(struct node*);
int main()
{
int ch;
struct node *start=NULL;
do
{
printf("\n 1.create \n 2.display \n 3.sort \n 4.exit");
printf("\n enter an option:");
scanf("%d",&ch);
switch(ch)
{
case 1:start=create(start);
break;
case 2:start=display(start);
break;
case 3:start=sort(start);
break;
case 4:break;
}
}while(ch!=4);
return 0;
}
struct node* create(struct node*start)
{
int ch;
struct node *newnode,*temp;
printf("\n Enter value of node and -1 to exit:");
scanf("%d",&ch);
do
{
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=ch;
if(start==NULL)
{
start=newnode;
newnode->next=NULL;
newnode->prev=NULL;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=NULL;
newnode->prev=temp;
}
printf("\n Enter data or enter -1 to exit:");
scanf("%d",&ch);
}while(ch!=-1);
printf("\n Link list is created.");
return start;
};
struct node* display(struct node*start)
{
struct node *temp=NULL;
temp=start;
printf("The Link list is:");
while(temp!=NULL)
{
printf("\t %d",temp->data);
temp=temp->next;
}
return start;
};
struct node* sort(struct node*start)
{
struct node *temp1=start;
struct node *temp2, *temp;
int x;
while(temp1->next!=NULL)
{
temp2=start;
while(temp2->next!=NULL)
{
temp=temp2->next;
if(temp2->data > temp->data)
{
x=temp->data;
temp->data =temp2->data;
temp2->data=x;
}
temp2=temp2->next;
}
temp1=temp1->next;
}
return start;
}
Comments
Post a Comment