Exp-2 & 3
Exp-2 & 3
i) Implement a singly linked list and perform insertion and deletion operations.
ii) Develop a program to reverse a linked list iteratively and recursively.
iii) Solve problems involving linked list traversal and manipulation.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct linked_list
{
int data;
struct linked_list *next;
};
typedef struct linked_list node;
node *head;
void create(node *list);
void display(node *list1);
void insert(node *list);
int count(node *list);
void main()
{
clrscr();
head=(node *)malloc(sizeof(node));
create(head);
printf("The Node are ...\n");
display(head);
printf("\n\n");
insert(head);
}
//
void create(node *list)
{
printf("Enter the data to node at end type-999 :");
scanf("%d",&list->data);
if(list->data==-999)
list->next=NULL;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
//
void display(node *list1)
{
do
{
printf ("%d -->",list1->data);
list1=list1->next;
}
while (list1->next!=NULL);
printf("\b\b\b");
}
void insert(node *list)
{
int p;
node *newnode,*temp;
newnode=(node *)malloc(sizeof(node));
printf("\n Enter the to newnode :");
scanf("%d",&newnode->data);
newnode->next=NULL;
printf("Enter the node position you want insert the node:");
scanf("%d",&p);
///Inserting new node at first position
if(p==1)
{
newnode->next=list;
list=newnode;
display(list);
}
//inserting the new node at end or middle
else
{
temp=list->next;
while(p>2)
{
list=list->next;
temp=temp->next;
p--;
}
list->next=newnode;
newnode->next=temp;
display(head);
}
}
Output:
i) b. Implement a singly linked list and perform deletion operations
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct linked_list
{
int data;
struct linked_list *next;
};
typedef struct linked_list node;
node *head;
void create(node *list);
void display(node *list1);
void del(node *list);
void main()
{
clrscr();
head=(node *)malloc(sizeof(node));
create(head);
printf("The Node are ...\n");
display(head);
printf("\n\n");
printf("Deleting the head node :\n");
printf("\n\n");
printf("The Node are ...\n");
del(head);
}
//
void create(node *list)
{
printf("Enter the data to node at end type-999 :");
scanf("%d",&list->data);
if(list->data==-999)
list->next=NULL;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
//
void display(node *list1)
{
do
{
printf ("%d -->",list1->data);
list1=list1->next;
}
while (list1->next!=NULL);
}
void del(node *list)
{
node *temp1,*temp2;
int n;
printf("Enter the node you want to delete :");
scanf("%d",&n);
if(n==list->data)
{
temp1=list->next;
free(list);
display(temp1);
}
else
{
temp1=list;
while (n!=temp1->data)
{
temp2=temp1;
temp1=temp1->next;
}
temp2->next=temp1->next;
free(temp1);
display(head);
}
Output:-
ii) Develop a program to reverse a linked list iteratively and recursively.
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct linked_list
{
int data;
struct linked_list *next;
};
typedef struct linked_list node;
void create(node *list);
void display(node *list);
void main()
{
node *head;
clrscr();
head=(node *)malloc(sizeof(node));
create(head);
printf("The Nodes in reverse are ...\n");
display(head);
}
//
void create(node *list)
{
printf("Enter the data to node at end type -999:");
scanf("%d",&list->data);
if(list->data==-999)
list->next=NULL;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
//
void display(node *list)
{
int a[100];
int i=0;
do
{
a[i++]=list->data;
list=list->next;
}
while (list->next!=NULL);
for(i=i-1;i>=0;i--)
printf("%d\n",a[i]);
}
Output:-
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct linked_list
{
int data;
struct linked_list *next;
};
typedef struct linked_list node;
void create(node *list);
void display(node *list);
void main()
{
node *head;
clrscr();
head=(node *)malloc(sizeof(node));
create(head);
printf("The Node are ...\n");
display(head);
}
//
void create(node *list)
{
printf("Enter the data to node at end type -999:");
scanf("%d",&list->data);
if(list->data==-999)
list->next=NULL;
else
{
list->next=(node *) malloc(sizeof(node));
create(list->next);
}
}
//
void display(node *list)
{
do
{
printf ("%d -->",list->data);
list=list->next;
}
while (list->next!=NULL);
Output:-
. Exercise 3: Linked List Applications
i) Create a program to detect and remove duplicates from a linked list.
ii) Implement a linked list to represent polynomials and perform addition.
iii) Implement a double-ended queue (deque) with essential operations.
#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void addNode(int data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL)
{
head = newNode;
tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
}
void removeDuplicate()
{
struct node *current = head, *index = NULL, *temp = NULL;
if(head == NULL)
{
return;
}
else
{
while(current != NULL)
{
temp = current;
index = current->next;
while(index != NULL)
{
if(current->data == index->data)
{
temp->next = index->next;
}
else
{
temp = index;
}
index = index->next;
}
current = current->next;
}
}
}
void display()
{
struct node *current = head;
if(head == NULL)
{
printf("List is empty \n");
return;
}
while(current != NULL)
{
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
void main()
{
clrscr();
addNode(10);
addNode(20);
addNode(35);
addNode(20);
addNode(25);
addNode(42);
addNode(19);
}
ii) Implement a linked list to represent polynomials and perform addition.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int coeff;
int pow;
struct Node* next;
};
void create_node(int x, int y, struct Node** temp)
{
struct Node *r, *z;
z = *temp;
if (z == NULL) {
r = (struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
else {
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
}
void polyadd(struct Node* poly1, struct Node* poly2, struct Node* poly)
{
while (poly1->next && poly2->next)
{
if (poly1->pow > poly2->pow)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
else if (poly1->pow < poly2->pow)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
else
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff + poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
poly->next = (struct Node*)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
while (poly1->next || poly2->next)
{
if (poly1->next)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
if (poly2->next)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
poly->next= (struct Node*)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
}
}
iii) Implement a double-ended queue (deque) with essential operations.
# include<stdio.h>
# define Size 5
int deque_arr[Size];
int front = -1;
int rear = -1;
void insert_rear()
{
int added_item;
if((front == 0 && rear == Size-1) || (front == rear+1))
{
printf("Queue Overflow\n");
}
if (front == -1)
{ front = 0;
rear = 0;
}
else
if(rear == Size-1)
rear = 0;
else
rear = rear+1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[rear] = added_item ;
}
void insert_front()
{
int added_item;
if((front == 0 && rear == Size-1) || (front == rear+1))
{ printf("Queue Overflow \n");
return; }
if (front == -1)/*If queue is initially empty*/
{ front = 0;
rear = 0;
}
else
if(front== 0)
front=Size-1;
else
front=front-1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[front] = added_item ; }
void delete_front()
{ if (front == -1)
{
printf("Queue Underflow\n");
}
printf("Element deleted from queue is : %d\n",deque_arr[front]);
if(front == rear)
{ front = -1;
rear=-1;
}
else
if(front == Size-1)
front = 0;
else
front = front+1;
}
void delete_rear()
{
if (front == -1)
{
printf("Queue Underflow\n");
}
printf("Element deleted from queue is : %d\n",deque_arr[rear]);
if(front == rear)
{
front = -1;
rear=-1;
}
else
if(rear == 0)
rear=Size-1;
else
rear=rear-1; }
void display_queue()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{ printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
{
while(front_pos <= rear_pos)
{
printf("%d ",deque_arr[front_pos]);
front_pos++;
}
}
else
{
while(front_pos <= Size-1)
{ printf("%d ",deque_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",deque_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}
void input_que()
{ int ch;
do
{ printf("1.Insert at rear\n");
printf("2.Delete from front\n");
printf("3.Delete from rear\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{ case 1:
insert_rear();
break;
case 2:
delete_front();
break;
case 3:
delete_rear();
break;
case 4:
display_queue();
break;
case 5:
break;
default:
printf("Wrong choice\n");
}
}
while(ch!=5);
}
void output_que()
{ int choice;
do
{ printf("1.Insert at rear\n");
printf("2.Insert at front\n");
printf("3.Delete from front\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_rear();
break;
case 2:
insert_front();
break;
case 3:
delete_front();
break;
case 4:
display_queue();
break;
case 5:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=5);
}
void main()
{
int choice;
printf("1.Input restricted dequeue\n");
printf("2.Output restricted dequeue\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
input_que();
break;
case 2:
output_que();
break;
default:
printf("Wrong choice\n");
}
}