0% found this document useful (0 votes)
13 views6 pages

DS Assignment

Note

Uploaded by

joyn82017
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)
13 views6 pages

DS Assignment

Note

Uploaded by

joyn82017
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/ 6

Assignment No: 02

Assignment Name: Complex Engineering Problem.

Problem Statement: The task involves creating a linked list where the values are the result of the
pre-order traversal of CT-3 assignment. This linked list operates following the principles of a
queue, where insertion occurs at the rear and removal occurs from the front. The objective is to
implement a C program that efficiently handles the following queries:

 Pop
 Insert 24
 Pop
 Pop

Objective: In this assignment, we will learn how to simulate queue operations using a linked list.
We will perform various operations such as inserting and removing elements (enqueue and
dequeue) from the queue. The linked list's insertion and removal are identical to a queue,
following the FIFO (First In, First Out) principle. We will use a set of pre-order traversal results
as the initial values of the queue.

Solution:

We found the pre-order traversal result of the BST of CT-3 assignment is given by the sequence:
02, 00, 22, 05, 10, 10, 32, 92. Now we have to add them in a Singly Linked List.

I will match each operation (enqueue and dequeue) with the code and provide a brief
explanation:

1. Initial Queue Setup

We start with a pre-order traversal result {2, 0, 22, 5, 10, 10, 32, 92} and insert each value into
the queue.

The queue is now:

2 -> 0 -> 22 -> 5 -> 10 -> 10 -> 32 -> 92

2. Query 1: Pop (Dequeue)

We remove the first element (2) from the queue.

Queue after the pop:

0 -> 22 -> 5 -> 10 -> 10 -> 32 -> 92

3. Query 2: Insert 24 (Enqueue)


We insert the value 24 at the rear of the queue.

Queue after insertion:

0 -> 22 -> 5 -> 10 -> 10 -> 32 -> 92 -> 24

4. Query 3: Pop (Dequeue)

We remove the front element (0) from the queue.

Queue after the pop:

22 -> 5 -> 10 -> 10 -> 32 -> 92 -> 24

5. Query 4: Pop (Dequeue)

We remove the next front element (22) from the queue.

Queue after the final pop:

5 -> 10 -> 10 -> 32 -> 92 -> 24

This sequence of steps follows the logic of enqueue and dequeue operations on a queue
implemented with a singly linked list. The code successfully manages the queue, displaying the
updated state after each operation.

The code syntax is:

Code Discription
#include <stdio.h> In this code, we need to represent the linkeed list
#include <stdlib.h> as a quueue using a linkeed list data structure. The
quueue ollows First-In-First-Out (FIFO) behavior,
// Defne the structure or a node in the linkeed list and we must per orm the given operatons.
struct Node {
int data; This code implements a quueue using a linkeed list in
struct Node* next; C. Here’s a breakedown o its components:
};
Node Structure (struct Node):
// Defne the structure or a quueue This structure represents a node in the linkeed list.
struct Queue { data: Stores the integer value o the node.
struct Node * ront, *rear; next: A pointer that points to the next node in the
}; list (or NULL i it's the last node).

// Functon to create a new node Queue Structure (struct Queue):


struct Node* newNode(int value) { This structure represents a quueue.
struct Node* temp = (struct ront: Points to the frst element in the quueue
Node*)malloc(sizeo (struct Node)); (where elements are dequueued).
temp->data = value; rear: Points to the last element in the quueue
temp->next = NULL; (where elements are enquueued).
return temp;

/ Functon to create an empty quueue Functons:


struct Queue* createQueue() { newNode(int value) is a uncton that creates a
struct Queue* qu = (struct new node with the given value.
Queue*)malloc(sizeo (struct Queue)); malloc() allocates memory or the new node.
qu-> ront = qu->rear = NULL; The data feld o the new node is set to the value
return qu; passed as the argument.
} The next pointer is initalized to NULL because the
node will be the last in the quueue when it's
// Functon to add an element to the quueue created.
(enquueue) Finally, it returns the newly created node
void enquueue(struct Queue* qu, int value) {
struct Node* temp = newNode(value); main() Functon:
i (qu->rear == NULL) { createQueue() creates an empty quueue.
malloc() allocates memory or the quueue structure.
qu-> ront = qu->rear = temp; Both ront and rear pointers are set to NULL since
return; the quueue is initally empty.
} The uncton returns the newly created quueue.
qu->rear->next = temp; Enquue Operaton:
qu->rear = temp; enquueue(struct Queue* qu, int value) inserts a new
} element into the quueue.
First, it creates a new node using newNode(value).
// Functon to remove an element rom the quueue I the rear is NULL, it means the quueue is empty, so
(dequueue) both ront and rear are set to this new node( the
void dequueue(struct Queue* qu) { frst node in the quueue)
i (qu-> ront == NULL) Otherwise, it linkes the current rear node to the
return; new node by setng rear->next = temp and then
struct Node* temp = qu-> ront; moves the rear pointer to the new node.
return;
struct Node* temp = qu-> ront; Dequueue Operaton:
qu-> ront = qu-> ront->next; dequueue(struct Queue* qu) removes an element
i (qu-> ront == NULL) rom the ront o the quueue.
qu->rear = NULL; I the ront is NULL, the quueue is empty, so the
ree(temp); uncton returns without doing anything.
} Otherwise, it stores the current ront node in a
temporary pointer temp.
// Functon to display the elements o the quueue The ront pointer iS moved to the next node ( ront
void displayQueue(struct Queue* qu) { = ront->next).
struct Node* temp = qu-> ront; I the new ront is NULL, it means the quueue has
while (temp != NULL) { become empty afer the removal, so we set the
print(("d (, temp->data); rear to NULL as well.
temp = temp->next; Finally, the memory or the removed node is reed.
} Display Queue:
print((\n(); displayQueue(struct Queue* qu) prints all the
// Main program to execute the quueries elements o the quueue startng rom the ront to
int main() { the rear.
// Pre-order traversal result as a quueue It uses a temporary pointer temp to traverse the
int values[] = {2, 0, 22, 5, 10, 10, 32, 92}; quueue.
int n = sizeo (values) / sizeo (values[0]); The uncton prints the data o each node untl it
reaches the end (temp == NULL).
// Create a quueue and enquueue the elements
struct Queue* qu = createQueue();
or (int i = 0; i < n; i++) { Main program:
enquueue(qu, values[i]); int values[] = {2, 0, 22, 5, 10, 10, 32, 92}; is an array
} o integers that simulates the pre-order traversal
result. These values will be enquueued into the
// Inital quueue display quueue.
print((Inital quueue: (); int n = sizeo (values) / sizeo (values[0]); calculates
displayQueue(qu); the number o elements in the array.
A new quueue is created with createQueue().
// Per orm the quueries A or loop iterates over the the array and
print((Query 1: Pop\n(); enquueues each value into the quueue using
dequueue(qu); // Pop enquueue(qu, values[i]);.
displayQueue(qu);

print((Query 2: Insert 24\n();


enquueue(qu, 24); // Insert 24 Inital quueue display: This prints the current state
displayQueue(qu); o the quueue afer all the values rom the array are
enquueued.
print((Query 3: Pop\n();
dequueue(qu); // Pop POP(dequueue): The frst quuery is to pop (dequueue)
displayQueue(qu); an element rom the ront o the quueue. The
dequueue(qu) uncton is called, and the updated
quueue is displayed.
print((Query 4: Pop\n();
dequueue(qu); // Pop Insert 24(Enquueue):
displayQueue(qu); The second quuery is to insert the value 24 into the
quueue. The enquueue(qu, 24) uncton is called to
return 0; add 24 to the rear o the quueue, and the updated
} quueue is displayed.

Query 3. Pop(dequueue) : The third quuery is


another pop (dequueue) operaton. The dequueue(qu)
uncton is called again to remove an element rom
the ront, and the updated quueue is displayed.

Query 4.Pop(dequueue) : The fnal quuery is


another pop (dequueue) operaton. The dequueue(qu)
uncton is called once more, and the fnal state o
the quueue is displayed.

The program ends with a return 0 statement,


indicatng success ul executon.

Outpu

Discussion:

In this experiment, we used a queue data structure implemented with a linked list. The queue
follows the First-In-First-Out (FIFO) principle, where elements are added to the rear and
removed from the front. The integer 24 was inserted into the queue, expanding it. The enqueue
function adds 24 at the rear, while the dequeue function removes elements from the front. After
several operations, the queue efficiently manages the data, concluding with a well-ordered
sequence.
THE END

You might also like