0% found this document useful (0 votes)
5 views

DS-Module2

This document covers various types of queues, including circular queues, priority queues, and double-ended queues, along with their implementations and applications. It explains the fundamental operations of queues such as enqueue and dequeue, and provides C programming examples for array implementation. Additionally, it introduces linked lists, their types, and operations, emphasizing how nodes are interconnected and manipulated.

Uploaded by

chinnu0707777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DS-Module2

This document covers various types of queues, including circular queues, priority queues, and double-ended queues, along with their implementations and applications. It explains the fundamental operations of queues such as enqueue and dequeue, and provides C programming examples for array implementation. Additionally, it introduces linked lists, their types, and operations, emphasizing how nodes are interconnected and manipulated.

Uploaded by

chinnu0707777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

DSA(BCS304)

MODULE II
Structure
Introduction
2.0 Queues
2.1 Queue Variants: Circular Queue
2.2 Queue Variants: Priority Queue
2.3 Queue Variants: Double Ended (De-Queue)
2.4 Assignment Questions

2.1 Queues
Queue is an abstract data structure, somewhat similar to Stack. In contrast to Stack, queue is
opened at both end. One end is always used to insert data, that end is referred to as REAR and
the operation is called enqueue and the other end referred to as FRONT, is used to remove data
and the operation is called dequeue. Queue follows First-In-First-Out methodology, i.e., the data
item stored first will be accessed first.

A real world example of queue can be a single-lane one-way road, where the vehicle enters first,
exits first. More real-world example can be seen as queues at ticket windows & bus-stops.

Logical representation of Queue data structure in computer’s memory

Queue [6]: 10 20 30 40 50

REAR
FRONT
The representation of queue is using arrays. Initially, FRONT & REAR indices are initialized to
-1. As elements are inserted, rear is incremented by 1 and as elements are deleted, front is
incremented by 1. Shaded cells in the above figure are empty. If an element is added, it sits in the
immediate next cell to rear and rear shifts right by one cell (i.e., rear++). Similarly if an element
is deleted, front shifts right by one cell (i.e. front++).

Smt. L N Shylaja, Asso. Prof.& HOD,AI & ML Page 1


ISE,BCE
DSA(BCS304)

Applications of Queue

Queue, as the name suggests is used whenever we need to have any group of objects in an order
in which the first one coming in, also gets out first while the others wait for their turn, like in the
following scenarios:
1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life, Call Center phone systems will use Queues, to hold people calling them in an
order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the same order as
they arrive, First come first served.
Array Implementation of a Queue
#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;

Smt. L N Shylaja, Asso. Prof.& HOD,AI & ML Page 2


ISE,BCE
DSA(BCS304)

main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /*End of switch*/
} /*End of while*/
} /*End of main()*/
insert()
{

Smt. L N Shylaja, Asso. Prof.& HOD,AI & ML Page 3


ISE,BCE
DSA(BCS304)

int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /*End of insert()*/

delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /*End of delete() */
display()
{
int i;

Smt. L N Shylaja, Asso. Prof.& HOD,AI & ML Page 4


ISE,BCE
DSA(BCS304)

if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}

2.4 Queue Variants: Circular Queue


A circular queue is an abstract data type that contains a collection of data which allows addition
of data at the end of the queue and removal of data at the beginning of the queue. Circular queues
have a fixed size. Circular queue follows FIFO principle. Queue items are added at the rear end
and the items are deleted at front end of the circular queue.

In a standard queue data structure re-buffering problem occurs for each de queue operation. To
solve this problem by joining the front and rear ends of a queue to make the queue as a circular
queue.

1. Circular queue is a linear data structure. It follows FIFO principle.


2. In circular queue the last node is connected back to the first node to make a circle.
3. Circular linked list fallow the First In First Out principle
4. Elements are added at the rear end and the elements are deleted at front end of the
queue
5. Both the front and the rear pointers points to the beginning of the array.
6. It is also called as “Ring buffer”.

Smt. L N Shylaja, Asso. Prof.& HOD,AI & ML Page 5


ISE,BCE
DSA(BCS304)

2.5 Queue Variants: Priority Queue


Priority Queue is more specialized data structure than Queue. Like ordinary queue, priority
queue has same method but with a major difference. In Priority queue items are ordered by key
value so that item with the lowest value of key is at front and item with the highest value of key
is at rear or vice versa. So we're assigned priority to item based on its key value. Lower the
value, higher the priority. Following are the principal methods of a Priority Queue.

Basic Operations
1. Insert / enqueue − add an item to the rear of the queue.

2. Remove / dequeue − remove an item from the front of the queue.

3. Peek − get the element at front of the queue.

4. isFull − check if queue is full.

5. isEmpty − check if queue is empty.

Priority Queue Representation

C program to demonstrate working of priority queue


#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int rear=-1,ch,front=-1,i,j,item,queue[SIZE],choice;
int Q_full();
int Q_Empty();
void insert();
void delet();
void display();

Smt. L N Shylaja, Asso. Prof.& HOD,AI & ML Page 6


ISE,BCE
DSA(BCS304)

int main()
{
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
while(1)
{
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: if(Q_full())
printf("Priority Queue is Full\n");
else
insert();
break;
case 2: if(Q_Empty())
printf("Priority Queue Empty\n");
else
delet();
break;
case 3: if(Q_Empty())
printf("Priority Queue Empty\n");
else
display();
break;
case 4: exit(0);
}
}
}
void insert()
{
int ele;

Smt. L N Shylaja, Asso. Prof.& HOD,AI & ML Page 7


ISE,BCE
DSA(BCS304)

printf("Enter the element\n");


scanf("%d",&item);
if(front==-1)
front++;
j=rear;
while(j>=0 && item<queue[j])
{
queue[j+1]=queue[j];
j--;
}
queue[j+1]=item;
rear++;
}
int Q_full()
{
if(rear==SIZE-1)
return 1;
else
return 0;
}
void delet()
{
printf("The item deleted is %d",queue[front]);
front++;
}
int Q_Empty()
{
if((front==-1)||(front>rear))
return 1;
else
return 0;

Smt. L N Shylaja, Asso. Prof.& HOD,AI & ML Page 8


ISE,BCE
DSA(BCS304)

}
void display()
{
printf("Elements of Priority Queue...");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}

2.6 Queue Variants: Double Ended (De-Queue)


A double-ended queue is an abstract data type similar to an simple queue, it allows you to insert
and delete from both sides means items can be added or deleted from the front or rear end.
Following reasons may help you understand why DE-Queue?
1. A nice application of the deque is storing a web browser's history. Recently visited URLs are
added to the front of the deque, and the URL at the back of the deque is removed after some
specified number of insertions at the front.
2. Another common application of the deque is storing a software application's list of undo
operations.
3. One example where a deque can be used is the A-Steal job scheduling algorithm.[5] This
algorithm implements task scheduling for several processors. A separate deque with threads
to be executed is maintained for each processor. To execute the next thread, the processor
gets the first element from the deque (using the "remove first element" deque operation). If
the current thread forks, it is put back to the front of the deque ("insert element at front") and
a new thread is executed. When one of the processors finishes execution of its own threads
(i.e. its deque is empty), it can "steal" a thread from another processor: it gets the last element
from the deque of another processor ("remove last element") and executes it
4. One of the best application of DE-Queue is in verifying whether given string is a
palindrome?

Smt. L N Shylaja, Asso. Prof.& HOD,AI & ML Page 9


ISE,BCE
DSA(BCS304)

Rear Front

RADAR inserted R A D A R

Rear Front
Verification R A D A R

R (remove from rear) R (remove from front)

Smt. L N Shylaja, Asso. Prof.& HOD,AI & ML Page 10


ISE,BCE
DSA(BCS304)

STRUCTURE
3.0 Introduction
3.1 Linked List
3.2 Linked list types
3.3 Linked List Representation
3.4 Operations on Linked Lists
3.4.1 Inserting a node
3.4.2 Deleting a node
3.5 Types of Linked Lists
3.6 Header Linked List
3.7 Doubly Linked list operations
3.8 Assignment Questions

3.0 Introduction
This module discusses the linked list data structures, operations on linked list. Different
types of liked list are explained in detail. The header linked list and its usage is highlighted. T he
concept of doubly linked list is discussed.

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 1


DSA(BCS304)

3.1 Linked List

Linked list is the collection of inter connected nodes with a head node representing the first node
and a tail node representing the last node of it. A node can be viewed as a block consisting of
two major fields : a data field and a pointer field. The pointer field is used to interconnect nodes
of a list. Below diagram depicts the linked list setup.

Tail

3.2 Linked list types


NULL

There are few variants of linked lists. Below diagram shows a brief classification of Linked
Lists.

Linked List

Singly LL Circular LL Doubly LL

Singly Circular LL Doubly Circular LL

Regarding these variants of LL, explanations and examples are given in subsequent sections of
the notes.

3.3 Linked List Representation

A simple representation of singly linked list would look like the below:
struct node {
int data;
struct node *next; };

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 2


DSA(BCS304)

struct node *newnode;


With the above declaration we can create a node of the pattern below:
data *next

new node
Initially, the pointer field of the first node in singly linked list would contain NULL value.
Instruction that makes it is: newnode->next = NULL;
Note: Structure variables’ fields of self referential type are accessed through -> (arrow
operator)
Now, let us try to understand few conventions w.r.t linked lists. Consider below given list.

10 1008h 20 1016h 30 1024h 40 NULL

Head (1000h) (1008h) (1016h) tail (1024h)


In this list, head node contains data = 10 and tail node contains data = 40. This list has 4 nodes
that are interconnected. Head node is connected to the next node since the pointer value contains
the address of next node. Tail node is not linked to any node since its pointer value is NULL.
Struct node *head, *tail, *newnode; // variables declared of type struct node
head = (struct node *) malloc (1 * sizeof(struct node));
This instruction creates one node of type struct. Memory gets allocated using DMA function
malloc(). Created node in the above instruction is referred to as “head”. It can be any other name
as well.
Head->data = 10; // assigns head node’s data field with 10
head->next = NULL; //assigning pointer value to NULL

10 NULL
head; tail
tail = head; // assigning the reference of head to tail. This means both head and tail are referring
to the same node.
Now, let us add a new node to this list.
Newnode = (struct node *) malloc (1 * sizeof(struct
node)); newnode->data = 20; newnode->next =
NULL;

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 3


DS(21CS32)

10 NULL 10 NULL

Head; tail new node

The above nodes can be connected using the following instructions:


head->next = newnode; // assigning head node’s pointer value with address of newnode

10 1008h 20 NULL

head; tail (1000h) newnode (1008h)


Now, if we execute the instruction, tail = newnode; tail & newnode both refer to the node with
value 20.
In this way, linked lists can be created.
3.4 Operations on Linked Lists
Major operations performed on linked lists are inserting a node, deleting a node, searching a
node and traversing the list to display all of its nodes.
3.5.1 Inserting a node: Node can be inserted at the beginning, at the end or anywhere in the linked
list

10 1008h 20 1016h 30 1024h 40 NULL


head (1000h) (1008h) (1016h) tail (1024h)
Consider the above list. Suppose we wish to insert a node at the beginning, following set of
instruction are to be executed.
newnode = (struct node *) malloc (1 * sizeof(struct node));
newnode->data = 50; newnode->next = head; 50 1000h
head = newnode;

Smt. L N Shylaja, Asso. Prof HOD, AI&ML, Page 4


ISE,BCE
DS(21CS32)

10 1008h 20 1016h 30 1024h 40 NULL

(1000h) (1008h) (1016h) tail (1024h)

50 1000h
head; newnode
Suppose we wish to insert a node at the end, following set of instruction are to be executed.

10 1008h 20 1016h 30 1024h 40 1032h

head (1000h) (1008h) (1016h) tail (1024h)

newnode = (struct node *) malloc (1 * sizeof(struct node)); 80 NULL


newnode->data = 80; newnode->next = NULL; (1032h)
80 NULL

Smt. L N Shylaja, Asso. Prof HOD, AI&ML, Page 5


ISE,BCE
DS(21CS32)

Take a temporary variable of type struct node, traverse the list until you reach the last node (i.e
tail )
while(temp->next !=NULL)
{
temp=temp->next;
}
temp->next = newnode;
tail = newnode;
Now let us see how to insert a node at a particular position in the list. For this, we need to ask
user to specify after which node in the list he would want to insert. Call it as “key” node. This
can be realized as below:
temp
10 1008h 20 1016h 30 1040h 40 1032h
head (1000h) (1008h) (1016h) tail (1024h)

Key = 30 80 NULL
100 1032h
Newnode (1040h) (1032h)
Take a temporary variable of type struct node, traverse the list until you reach the desired node
(i.e 30)
while(temp->data !=key)
{ 100 NULL

temp=temp->next; newnode (1040h)


}
newnode->next = temp->next;
temp->next = newnode;

Smt. L N Shylaja, Asso. Prof HOD, AI&ML, Page 6


ISE,BCE
DS(21CS32)

3.5.2 Deleting a node: A node in the list can be deleted using the standard function free ( ). Let us
see an example to understand delete operations on linked list.

10 1008h 20 1024h 30 NULL 40 NULL


head (1000h) temp(1016h) tail (1024h)
temp2 (1008h)
Let us try to delete node with 30 data value.
Take two temporary variables and position them to key node and the previous node of it.

Smt. L N Shylaja, Asso. Prof HOD, AI&ML, Page 7


ISE,BCE
DS(21CS32)

Now, key =30.


While(temp->data!=key)
{
temp=temp->next;
}
While(temp2->next!=key)
{
temp2=temp2->next;
}
temp2->next = temp->next;
temp->next = NULL;
free(temp);

Below C program creates a singly linked list and performs few operations like insert, delete,
display, search, insert and delete at specific positions in the list.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node{
int data;
struct node *ptr;
};
struct node *head, *last,*temp, *temp2;
void create();
void delet();
void dele_spec();
void display();
void spec_ins();
int search(int);
int num,con=1,ch,key,f;
void main()

Smt. L N Shylaja, Asso. Prof., ISE, BCE Page 7


DS(21CS32)

{
clrscr();
head=NULL;
printf("1.Create\n2.Delete\n3.Delete Specific\n4.Insert After Specific node\n5.Display\n6.
Search\n7.Exit\n");
while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter number\n");
scanf("%d",&num);
create();
break;
case 2: delet();
break;
case 3: dele_spec();
break;
case 4: spec_ins();
break;
case 5: display();
break;
case 6: printf("Enter the node data to be searched\n");
scanf("%d",&key);
f=search(key);
if(f==1) printf("NODE FOUND\n");
else printf("NODE NOT FOUND\n");
break;
case 7: exit(0);
}

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 8


DS(21CS32)

}
}
void create()
{
if(head==NULL)
{
head = (struct node*)malloc(1*sizeof(struct node));
head->data=num;
head->ptr=NULL;
last=head;
}
else
{
temp= (struct node*)malloc(1*sizeof(struct node));
temp->data=num;
last->ptr=temp;
temp->ptr=NULL;
last=temp;
}
}
void delet()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head->ptr;
free(head);
head=temp;

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 9


DS(21CS32)

}
}
void dele_spec()
{
int key;
printf("Enter the node to be deleted\n");
scanf("%d",&key);
temp=head;
temp2=head;
while(temp->data!=key)
{
temp=temp->ptr;
}
while(temp2->ptr!=temp)
{
temp2=temp2->ptr;
}
if(temp->ptr==NULL)
{
temp2->ptr=NULL;
free(temp);
last=temp2;
}
else
{
temp2->ptr=temp->ptr;
free(temp);
}
}
void spec_ins()
{

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 10


DS(21CS32)

int key;
printf("Enter the node info next to which to be inserted\n");
scanf("%d",&key);
printf("Enter data for newnode\n");
scanf("%d",&num);
temp2= (struct node*)malloc(1*sizeof(struct node));
temp2->data=num;
temp=head;
while(temp->data!=key)
{
temp=temp->ptr;
}
temp2->ptr=temp->ptr;
temp->ptr=temp2;
}
void display()
{
if(head==NULL)
{
printf("List empty\n");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->ptr;
}
}
}

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 11


DS(21CS32)

int search(int key)


{
int flag=0;
temp=head;
while(temp!=NULL)
{
if(temp->data==key)
{
flag=1;
return flag;
}
else
temp=temp->ptr;
}
return flag;
}
Advantages of Linked Lists
 They are a dynamic in nature which allocates the memory when required.
 Insertion and deletion operations can be easily implemented.
 Stacks and queues can be easily executed.
 Linked List reduces the access time.
Disadvantages of Linked Lists
 The memory is wasted as pointers require extra memory for storage.
 No element can be accessed randomly; it has to access each node sequentially.
 Reverse Traversing is difficult in linked list.

Applications of Linked Lists


 Linked lists are used to implement stacks, queues, graphs, etc.
 Linked lists let you insert elements at the beginning and end of the list.
 In Linked Lists we don’t need to know the size in advance.

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 12


DS(21CS32)

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 13


DS(21CS32)

3.5 Types of Linked Lists


Singly Linked List: Singly linked lists contain nodes which have a data part as well as an address
part i.e. next, which points to the next node in sequence of nodes. The operations we can perform
on singly linked lists are insertion, deletion and traversal.

Doubly Linked List : In a doubly linked list, each node contains two links the first link points to
the previous node and the next link points to the next node in the sequence.

Circular Linked List : In the circular linked list the last node of the list contains the address of
the first node and forms a circular chain.

3.6 Header Linked List

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 14


DS(21CS32)

Header linked list is similar to Singly linked List but the difference is that in this type of list,
there exists a special node called “head”, which either contains information regarding total nodes
in the list or the address of the last node in the whole list.
C Program to implement header linked list is shown below
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node{
int data;
struct node *ptr;
};
struct node *head, *last,*temp, *newnode;
void create();
void delet();
void display();
int num,count=0,ch;
void main()
{
clrscr();
head=NULL;
printf("1.Create\n2.Delete\n3.Display\n4.Exit\n");
while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: if(head==NULL)
{
head = (struct node*)malloc(1*sizeof(struct node));
head->data=count;

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 15


DS(21CS32)

head->ptr=NULL;
last=head;
}
printf("Enter number\n");
scanf("%d",&num);
create();
break;
case 2: delet();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
void create()
{
newnode = (struct node*)malloc(1*sizeof(struct node));
newnode->data=num;
newnode->ptr=NULL;
last->ptr=newnode;
last=newnode;
count++;
head->data=count;
}
void delet()
{
if(head->data==0)
{
printf("List is empty\n");
}

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 16


DS(21CS32)

else
{
temp=head;
while(temp->ptr!=last)
{
temp=temp->ptr;
}
free(last);
last =temp;
last->ptr=NULL;
}
count--;
head->data=count;
}
void display()
{
if(head==NULL)
{
printf("List empty\n");
}
else
{
temp=head->ptr;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->ptr;
}
}
printf("\nHeader Information:%d\n",head->data);
}

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 17


DS(21CS32)

Linked implementation of Stack


#include <stdio.h>
#include <stdlib.h>
struct Node
{
int Data;
struct Node *next;
}*top;
void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 18


DS(21CS32)

temp->next=top;
top=temp;
}
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main(int argc, char *argv[])
{
int i=0;
top=NULL;
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 19


DS(21CS32)

scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a valueber to push into Stack: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
popStack();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
struct Node *temp;
while(top!=NULL)
{
temp = top->next;
free(top);
top=temp;

Smt. L N Shylaja, Asso. Prof., ISE,BCE Page 20


DSA(BCS304)

}
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
Merging of two lists
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert_list1();
void insert_list2();
void display();
void merge_list();
struct node *list1, *list2,*list3,*temp1,*temp2,*temp3,*temp4;
void main()
{
int ch;
clrscr();
printf("1 for insert in first linklist\n");
printf("2 for insert in second linklist\n");
printf("3 for display first & second linklist\n");

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 21


DSA(BCS304)

printf("4 for merge linklist\n");


printf("5 for exit\n");
while(1)
{
printf("\nEnter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_list1();
break;
case 2: insert_list2();
break;
case 3: display();
break;
case 4: merge_list();
break;
case 5: exit(0);
}
}
}

void merge_list()
{
temp1=list1;
temp2=list2;
while(temp1!=NULL || temp2!=NULL)
{
temp3=(struct node*)malloc(1*sizeof(struct node));
if(list3==NULL)
{
list3=temp3;

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 22


DSA(BCS304)

temp4=temp3;
}
if(temp1->data<temp2->data)
{
temp3->data=temp1->data;
temp4->next=temp3;
temp4=temp3;
temp1=temp1->next;
}
else
{
temp3->data=temp2->data;
temp4->next=temp3;
temp4=temp3;
temp2=temp2->next;
}
if(temp2==NULL)
{
while(temp1!=NULL)
{
temp3=(struct node*)malloc(1*sizeof(struct node));
temp3->data=temp1->data;
temp4->next=temp3;
temp4=temp3;
temp1=temp1->next;
}
break;
}
if(temp1==NULL)
{
while(temp2!=NULL)

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 23


DSA(BCS304)

{
temp3=(struct node*)malloc(1*sizeof(struct node));
temp3->data=temp2->data;
temp4->next=temp3;
temp4=temp3;
temp2=temp2->next;
}
break;
}
}
temp3->next=NULL;
}

void insert_list1()
{
struct node *new;
if(list1==NULL)
{
list1=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&list1->data);
list1->next=NULL;
temp1=list1;
}
else
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&new->data);
new->next=NULL;
temp1->next=new;

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 24


DSA(BCS304)

temp1=temp1->next;
}
}

void insert_list2()
{
struct node *new;
if(list2==NULL)
{
list2=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&list2->data);
list2->next=NULL;
temp2=list2;
}
else
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&new->data);
new->next=NULL;
temp2->next=new;
temp2=temp2->next;
}
}

void display()
{
temp1=list1;
temp2=list2;
printf("\nThe Information of First linklist:");

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 25


DSA(BCS304)

while(temp1!=NULL)
{
printf("%d ",temp1->data);
temp1=temp1->next;
}
printf("\nThe Information of Second linklist:");
while(temp2!=NULL)
{
printf("%d ",temp2->data);
temp2=temp2->next;
}
temp3=list3;
printf("\nMerged List:");
while(temp3!=NULL)
{
printf("%d ",temp3->data);
temp3=temp3->next;
}
}
Reversing a Singly Linked List
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert_list();
void display();
void revers();

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 26


DSA(BCS304)

struct node *head,*tail,*temp,*temp1;


void main()
{
int ch;
clrscr();
printf("1 insert in list\n");
printf("2 Reverse and display list\n");
printf("3.Exit\n");
while(1)
{
printf("\nEnter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_list();
break;
case 2: revers();
break;
case 3: exit(0);
}
}
}
void revers()
{
temp=tail;
temp1=head;
while(temp!=head)
{
temp1=head;
while(temp1->next!=temp)
{

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 27


DSA(BCS304)

temp1=temp1->next;
}
temp->next=temp1;
temp=temp1;
}
temp=tail;
head->next=NULL;
printf("Reversed list: ");
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
void insert_list()
{
struct node *new;
if(head==NULL)
{
head=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&head->data);
head->next=NULL;
tail=temp=head;

}
else
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&new->data);

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 28


DSA(BCS304)

new->next=NULL;
temp->next=new;
temp=temp->next;
tail=temp;
}
temp1=head;
printf("Created list: ");
while(temp1!=NULL)
{
printf("%d ",temp1->data);
temp1=temp1->next;
}
}
Singly Circular Linked List Operations
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node{
int data;
struct node *ptr;
};
struct node *newnode, *front,*rear, *temp;

void create();
void display();
void delet();
int num,ch;
void main()
{
front=rear=NULL;
while(1)

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 29


DSA(BCS304)

{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter number\n");
scanf("%d",&num);
create();
break;
case 2: delet();
break;
case 3: display();
break;
case 4: exit(0);

}
}
}
void create()
{
newnode = (struct node*)malloc(1*sizeof(struct node));
newnode->data=num;
if(rear==NULL)
front=rear=newnode;
else
{
rear->ptr=newnode;
rear=newnode;
}
rear->ptr=front;
}

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 30


DSA(BCS304)

void delet()
{
if(front==NULL)
printf("List is empty\n");
else if(front==rear)
{
printf("Element deleted is %d",front->data);
front=NULL;
}
else
{
temp=front;
printf("Deleted element is %d",temp->data);
temp=temp->ptr;
free(front);
front=temp;
rear->ptr=front;
}
}
void display()
{
if(front==NULL)
{
printf("List empty\n");
}
else
{
temp=front;
while(temp!=rear)
{

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 31


DSA(BCS304)

printf("%d\t",temp->data);
temp=temp->ptr;
}
}
printf("%d\n",temp->data);
}
Adding and evaluating Polynomials using Circular Linked List
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
struct node{
int coeff;
int expo;
struct node *ptr;
};
struct node *head1,*head2,*head3, *temp,*temp1,*temp2,*temp3,*list1,*list2,*list3;
struct node *dummy1,*dummy2;
void create_poly1(int , int);
void create_poly2(int , int);
void display();
void add_poly();
void eval_poly(int );
int n,ch;
int c,e,i;
void main()
{
int x;
list1=list2=NULL;
printf("1.Create first polynomial\n2.Create Second Polynomial\n3.Display both the
polynomials\n");

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 32


DSA(BCS304)

printf("4.Add Polynomials\n5.Evaluate a Polynomial\n6.Exit\n");


while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the number of terms\n");
scanf("%d",&n);
printf("Enter coefficient & power of each term\n");
for(i=0;i<n;i++)
{
scanf("%d%d",&c,&e);
create_poly1(c,e);
}
break;
case 2: printf("Enter the number of terms\n");
scanf("%d",&n);
printf("Enter coefficient & power of each term\n");
for(i=0;i<n;i++)
{
scanf("%d%d",&c,&e);
create_poly2(c,e);
}
break;
case 3: display();
break;
case 4: add_poly();
break;
case 5:printf("Enter the value for x\n");
scanf("%d",&x);

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 33


DSA(BCS304)

eval_poly(x);
break;
case 6:exit(0);
}
}
}
void create_poly1(int c, int e)
{
dummy1=(struct node*)malloc(1*sizeof(struct node));
dummy1->coeff=0;
dummy1->expo=0;
dummy1->ptr=list1;
if(list1==NULL)
{
list1=(struct node*)malloc(1*sizeof(struct node));
list1->coeff=c;
list1->expo=e;
list1->ptr=list1;
head1=list1;
head1->ptr=dummy1;
}
else
{
temp=(struct node*)malloc(1*sizeof(struct node));
temp->coeff=c;
temp->expo=e;
head1->ptr=temp;
temp->ptr=dummy1;
head1=temp;

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 34


DSA(BCS304)

void create_poly2(int c, int e)


{
dummy2=(struct node*)malloc(1*sizeof(struct node));
dummy2->coeff=0;
dummy2->expo=0;
dummy2->ptr=list2;
if(list2==NULL)
{
list2=(struct node*)malloc(1*sizeof(struct node));
list2->coeff=c;
list2->expo=e;
list2->ptr=list2;
head2=list2;
head2->ptr=dummy2;
}
else
{
temp=(struct node*)malloc(1*sizeof(struct node));
temp->coeff=c;
temp->expo=e;
head2->ptr=temp;
temp->ptr=dummy2;
head2=temp;
}
}
void add_poly()
{
temp1=list1;
temp2=list2;

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 35


DSA(BCS304)

while((temp1!=dummy1)&&(temp2!=dummy2))
{
temp=(struct node*)malloc(1*sizeof(struct node));
if(list3==NULL)
{
list3=temp;
head3=list3;
}
if(temp1->expo==temp2->expo)
{
temp->coeff=temp1->coeff+temp2->coeff;
temp->expo=temp1->expo;
temp->ptr=list3;
head3->ptr=temp;
head3=temp;
temp1=temp1->ptr;
temp2=temp2->ptr;
}
else if(temp1->expo>temp2->expo)
{
temp->coeff=temp1->coeff;
temp->expo=temp1->expo;
temp->ptr=list3;
head3->ptr=temp;
head3=temp;
temp1=temp1->ptr;
}
else
{
temp->coeff=temp2->coeff;

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 36


DSA(BCS304)

temp->expo=temp2->expo;
temp->ptr=list3;
head3->ptr=temp;
head3=temp;
temp2=temp2->ptr;
}
}
if(temp1==dummy1)
{
while(temp2!=dummy2)
{
temp=(struct node*)malloc(1*sizeof(struct node));
temp->coeff=temp2->coeff;
temp->expo=temp2->expo;
temp->ptr=list3;
head3->ptr=temp;
head3=temp;
temp2=temp2->ptr;
}
}
if(temp2==dummy2)
{
while(temp1!=dummy1)
{
temp=(struct node*)malloc(1*sizeof(struct node));
temp->coeff=temp1->coeff;
temp->expo=temp1->expo;
temp->ptr=list3;
head3->ptr=temp;
head3=temp;
temp1=temp1->ptr;

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 37


DSA(BCS304)

}
}
}

void display()
{
temp1=list1;
temp2=list2;
temp3=list3;
printf("\nPOLYNOMIAL 1:");
while(temp1!=dummy1)
{
printf("%dX^%d+",temp1->coeff,temp1->expo);
temp1=temp1->ptr;
}
printf("\b ");
printf("\nPOLYNOMIAL 2:");
while(temp2!=dummy2)
{
printf("%dX^%d+",temp2->coeff,temp2->expo);
temp2=temp2->ptr;
}
printf("\b ");
printf("\n\nSUM OF POLYNOMIALS:\n");
while(temp3->ptr!=list3)
{
printf("%dX^%d+",temp3->coeff,temp3->expo);
temp3=temp3->ptr;
}
printf("%dX^%d\n",temp3->coeff,temp3->expo);

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 38


DSA(BCS304)

void eval_poly(int x)
{
int result=0;
temp1=list1;
temp2=list2;
while(temp1!=dummy1)
{
result+=(temp1->coeff)*pow(x,temp1->expo);
temp1=temp1->ptr;
}
printf("Polynomial 1 Evaluation:%d\n",result);
result=0;
while(temp2!=dummy2)
{
result+=(temp2->coeff)*pow(x,temp2->expo);
temp2=temp2->ptr;
}
printf("Polynomial 2 Evaluation:%d\n",result);
}

Smt. L N Shylaja, Asso. Prof.& HOD, AI&ML,BCE Page 39

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy