Adobe Scan 21 Nov 2023
Adobe Scan 21 Nov 2023
Aim
Create a stack and perform PUSH, POP, PEEK and TRAVERSE
operations on the stack using the linked list.
Language Used
C Programming Language
Theory
Linked List implementation of stack
Instead of using array, we can also use linked list to implement
stack. Linked list allocates the memory dynamically. However,
time complexity n both the scenario is same for all the operations
i.e. push, pop and peek.
In linked list implementation of stack, the nodes are maintained
non-contiguously in the memory. Each node contains a pointer to
its immediate successor node in the stack. Stack is said to be
overflown if the space left in the memory heap is not enough to
createa node.
Algorithm
Push Operation:
" Initialize a node.
Update the value of that node by data i.e. node->data=data
Now link this node to the top of the linked list.
" And update top pointer to the current node.
Pop Operation:
First check whether there is any node present in the linked
list or not, if not then return.
Otherwise make pointer let say temp to the top node and
move forward the top node by 1 step.
Now free this temp node.
Peek Operation:
Check if there is any node present or not, if not then return.
Otherwise return the value of top node of the linked list.
Traversing Operation:
Take a temp node and initialize it with top pointer.
Now start traversing temp till it encounters NULL.
" Simultaneously print the value of the temp node.
Programn Code
#include <stdio . h>
#include <stdlib.h>
|// Define the structure for a stack node
struct Node {
int data;
struct Node* next;
return top->data;
// Function to traverse and display the elements in the stack
void traverse() {
struct Node* current = top;
printf("Stack elements: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
printf("\n");
int main() {
int choice, data;
while (1) {
printf("\nStack Operations:\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. PEEK\n");
printf("4. TRAVERSE\n") ;
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to push: ");
scanf("%d", &data) ;
push(data);
break;
case 2:
data = pop();
if (data != -1) {
printf("Popped item: %d\n", data);
break;
case 3:
data = peek();
if (data != -1) {
printf("Top of the stack: %d\n", data);
break;
case 4:
traverse();
break;
case 5:
printf("Exiting the program. \n") ;
exit (0) ;
default:
printf("Invalid choice. Please try again. \n");
}
return 0;
Output
EXPERIMENT - 7
Aim
Create a linear queue using linked list and implement different
operations such as insert, delete and display the queue elements.
Language Used
C Programming Language
Theory
In linear queue, insertion is done from the rear and deletion is
done from the front end. In circular queue, the insertion and
deletion can take place from any end. The memory space occupied
by the linear queue is more than the circular queue. It requires
less memory as compared to linear queue.
Program Code
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the linked list
struct Node {
int data;
struct Node* next;
printf("\n");
int main() {
struct Queue* queue = createQueue() ;
int choice, item;
while (1) {
printf(""\nQueue Operations : \n");
printf("1. Enqueue (Insert)\n");
printf("2. Dequeue (Delete)\n");
printf("3. Display\n" ) ;
printf("4. Exit\n");
printf("Enter your choice: ");
scanf( "%d", &choice) ;
Switch (choice) {
case 1:
printf("Enter the element to enqueue:");
scanf("%d", &item);
enqueue(queue, item);
break;
case 2:
dequeue(queue);
break;
case 3:
display(queue);
break;
case 4:
exit(0) ;
default:
printf("Invalid choice. Please try again. \n");
return 0;
Output
ldt section Vie Go un
DeBLE CONSOu
2. Dege (elete)
Enter yo c 2
engd to the gu,
Queue perat ions:
bej (belete)
4. Eit
r choices 1
Etee the e l e t to eege:
m d te the gee
3. olsplay
. Oisplay
2. Dee (helete)
2. Orge (Delete)
return root;
int main() {
struct Node* root = NULL;
int choice, data;
Switch (choice) {
case 1:
Theory
Abinary search tree follows some order to arrange the elements.
In aBinary Search Tree, the value of left mode must be smaller
than the parent node and the value of right node must be greater
than the parent node. This rule is applied recursively tothe left
and right subtrees of the root.
Insertion and Deletion: Operations such as insertion and
deletion cause the BST representation to change dynamically. The
data structure must be modified in such a way that the properties
of BST continue to hold. New nodes are inserted as leaf nodes in
the BST.
Traversal: ABST can be traversed through three basic
algorithms: inorder, preorder and postorder tree walks.
Inorder tree walk: Nodes from the left subtree get visited
first, followed by the root node and right subtree. Such a
traversal visit all the nodes in the order of non-decreasing
key sequence.
" Preorder tree walk: The root node gets visited first,
followed by left and right subtrees.
Postorder tree walk: Nodes from the left subtree get
visited first, followed by the right subtree and finally the
root.
Programn Code
#include <stdio.h>
#include <stdlib.h>
// Define a structure to represent an automobile
struct Automobile {
char type[ 50];
char company[50] ;
int year_of_make;
};
|// Define the structure for a BST node
struct BSTNode {
struct Automobile data;
struct BSTNode* left;
struct BSTNode* right;
if (root == NULL) {
return createNode(data);
if (data.year_of_make < root->data.year_of_make) {
root->left = insert (root ->left, data);
} else if (data.year_of_make > root->data.year_of_make) {
root->right = insert (root->right, data);
return root;
return root;
}
|// Function to delete a node from the BST
struct BSTNode* deleteNode (struct BSTNode* root, int year) {
if (root == NULL) {
return root;
if (year < root- >data.year_of_make) {
root->left = deleteNode(root->left, year);
} else if (year > root->data.year _of_make) {
root->right = deleteNode (root->right, year) ;
} else {
1/ Node with only one child or no child
if (root->left == NULL) {
struct BSTNode* temp = root->right;
free (root) ;
return temp;
} else if (root->right == NULL) {
struct BSTNode* temp = root->left;
free(root);
return temp;
|int main() {
struct BSTNode* root = NULL;
struct Automobile car1 = { "Sedan", "Toyota", 2020 };
struct Automobile car2 = { "SUV", "Ford", 2019 };
struct Automobile car3 = { "Hatchback", "Honda", 2022 };
struct Automobile car4 = { "Convertible", "Chevrolet", 2018 };
root = insert (root, car1);
root = insert(root, car2) ;
root = insert (root, car3) ;
root = insert(root, car4);
printf("Inorder Traversal:\n");
inorderTraversal(root);
int yearToDelete = 2019;
root = deleteNode (root, yearToDelete);
printf("\nInorder Traversal after deleting a node: \n");
inorderTraversal(root) ;
return 0;
Output
e fdt Selection View Go Run
Ps C:es\poana\eatplc> cd
Inorder Iraversal:
sers\poonaesktep\C1" ;1f (9) ( e e 1: 1f S) (.ube)
y evrolet, ear: 21a
Theory
Graph: Graph is a non-linear data structure made up of finite
number of nodes or vertices and the edges that connect them.
Graphs in data structure are used to address real-world problems
in which it represents the problem area as a network like
telephone networks,circuit networks and social networks.
Depth First Search (DFS): Unlike trees, graphs may contain
cycles (a node may be visited twice). To avoid processing a node
more than once, use a boolean visited array. A graph can have
more than one DFS traversal.
Breadth First Search (BFS): BFS is used to search a graph data
structure for a node that meets a set of criteria. It starts at the
root of the graph and visits all nodes at the current depth level
before moving on to the nodes at the next depth level.
Program Code
#include <stdio.h>
#include <stdlib.h>
#include <stdbool . h>
#define MAX NODES 100
return graph;
visited[start] = true;
queue[rear++] = start;
}
printf("\n");
|int main() {
int numNodes, numEdges;
printf("Enter the number of nodes and edges: ");
scanf ("%d %d", &numNodes, &numEdges);
struct Graph* graph = createGraph(numNodes);
printf("Enter the edges (src dest):\n");
for (int i = 0; i< numEdges; i++) {
int src, dest;
scanf( "%d %d", &src, &dest) ;
addEdge (graph, src, dest);
int startNode;
printf("Enter the starting node for BFS: ");
scanf("%d", &startNode) ;
printf("Depth-First Traversal: ");
bool visited [MAX_NODES] = (false);
for (int i =0; i< numNodes; i++) {
if (!visited[i]) {
DFS(graph, i, visited);
printf("\n");
PS C:sers\poonaesktup\C>