0% found this document useful (0 votes)
14 views22 pages

Dsa Pratcial

The document outlines practical assignments for a B.Tech CE program, focusing on data structures, including pointers, dynamic memory allocation, stacks, queues, linked lists, and sorting algorithms. Each assignment includes specific programming tasks and examples in C++. The document serves as a practical guide for students to implement various data structures and algorithms.

Uploaded by

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

Dsa Pratcial

The document outlines practical assignments for a B.Tech CE program, focusing on data structures, including pointers, dynamic memory allocation, stacks, queues, linked lists, and sorting algorithms. Each assignment includes specific programming tasks and examples in C++. The document serves as a practical guide for students to implement various data structures and algorithms.

Uploaded by

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

NAME:DEEP Jain

Enroll/GR No:15594

PROGRAM NAME:- B.TECH CE SEM-III

COURSE CODE: - CE341P

COURSE:- DATA STRUCTURE PRACTICAL

FACULTY NAME:- MR. BHAVESH CHAVDA

Sr.N
Date Practical Name Sign
o
22.07.202
1 Introduction To Pointers. Call By Value And Call By Reference
4
29.07.202
2 Introduction To Dynamic Memory Allocation
4
Implement A Program For Stack That Performs Following
05.08.202
3 Operations Using Array.
4
(A) Push (B) Pop (C) Peep (D) Change (E) Display
12.08.202 Implement A Program To Convert Infix Notation To Postfix
4
4 Notation Using Stack.
Write A Program To Implement QUEUE Using Arrays That
19.08.202
5 Performs Following Operations
4
(A)INSERT (B) DELETE (C) DISPLAY
Write A Program To Implement Circular Queue Using Arrays
26.08.202
6 That Performs Following Operations.
4
(A) INSERT (B) DELETE (C) DISPLAY
Write A Menu Driven Program To Implement Following
Operations On The Singly Linked List.
(A) Insert A Node At The Front Of The Linked List.
02.09.202
7 (B) Insert A Node At The End Of The Linked List.
4
(C) Delete A First Node Of The Linked List.
(D) Delete A Node Before Specified Position.
(E) Delete A Node After Specified Position
09.09.202
8 Write A Program To Implement Stack Using Linked List.
4
16.09.202
9 Write A Program To Implement Queue Using Linked List.
4
Write A Program To Implement Following Operations On The
Doubly Linked List.
23.09.202 (A) Insert A Node At The Front Of The Linked List.
10
4 (B) Insert A Node At The End Of The Linked List.
(C) Delete A Last Node Of The Linked List.
(D) Delete A Node Before Specified Position.
Write A Program To Implement Following Operations On The
Circular Linked List.
30.09.202 (A) Insert A Node At The End Of The Linked List.
11
4 (B) Insert A Node Before Specified Position.
(C) Delete A First Node Of The Linked List.
(D) Delete A Node After Specified Position.
07.10.202
12 Write A Program Which Create Binary Search Tree.
4
14.10.202 Implement Tree Traversing Methods Inorder, Preorder And
13
4 Postorder Traversal.
21.10.201
14 Write A Program To Implement Quick Sort
4
15 04.11.202 Write A Program To Implement Merge Sort
4
08.11.202
16 Write A Program To Implement Bubble Sort
4
17 08.11.202 Write A Program To Implement Binary Search
4

1.Introduction to pointers. Call by Value and Call by Reference.


Introduction to pointers: -
A pointer is a variable that stores the memory address of another variable. In C, C++,
and other languages, pointers are a powerful feature that allows you to directly access and
manipulate memory.

Syntax of C Pointers:
The syntax of pointers is similar to the variable declaration in C, but
we use the ( * ) dereferencing operator in the pointer declaration.

datatype * ptr;

Why Use Pointers?


 Efficient memory management.
 Passing large data structures to functions without copying them.
 Dynamic memory allocation.
 To implement data structures like linked lists, trees, etc.
Call by Value and Call by Reference: -
Call by Value
In Call by Value, a copy of the actual value is passed to the function.
Changes made inside the function do not affect the original variable.
Example:
#include <stdio.h>
void modify(int x) {
x = 20; // Modifies only the copy of the variable.
}
int main() {
int a = 10;
modify(a);
printf("Value of a: %d\n", a); // Output: 10
return 0;
}
Call by Reference
In Call by Reference, a reference (memory address) to the actual variable is
passed. Changes made in the function affect the original variable.
Example:
#include <stdio.h>
void modify(int *x) {
*x = 20; // Modifies the actual variable via the pointer.
}
int main() {
int a = 10;
modify(&a);
printf("Value of a: %d\n", a); // Output: 20
return 0;
}
2. Introduction to Dynamic Memory Allocation.
The concept of dynamic memory allocation in c language enables the C
programmer to allocate memory at runtime. Dynamic memory allocation in
c language is possible by 4 functions of stdlib.h header file.
1. malloc()
2. calloc()
3. realloc()
4. free()
malloc() function
The malloc() function allocates single block of requested memory.
calloc() function
The calloc() function allocates multiple block of requested memory.
realloc() function
If memory is not sufficient for malloc() or calloc(), you can reallocate the
memory by realloc() function. In short, it changes the memory size.
free() function
The memory occupied by malloc() or calloc() functions must be released by
calling free() function. Otherwise, it will consume memory until program
exit.
3. Implement a program for stack that performs following
operations using array.
(a) PUSH (b) POP (c) PEEP (d) CHANGE (e) DISPLAY
Code: -
#include <iostream>

#define MAX_SIZE 100 // Maximum size of the stack

using namespace std;

class Stack {

private:

int arr[MAX_SIZE]; // Array to store stack elements

int top; // Points to the top of the stack

public:

Stack() {

top = -1; // Initialize the stack as empty

// Function to push an element onto the stack

void push(int value) {

if (top >= MAX_SIZE - 1) {

cout << "Stack Overflow! Cannot push " << value << ".\n";

return;

arr[++top] = value;

cout << "Pushed " << value << " onto the stack.\n";
}

// Function to pop an element from the stack

void pop() {

if (top == -1) {

cout << "Stack Underflow! Cannot pop.\n";

return;

cout << "Popped " << arr[top--] << " from the stack.\n";

void peep(int pos) {

if (pos <= 0 || pos > top + 1) {

cout << "Invalid position! Position must be between 1 and " << top + 1 << ".\n";

return;

cout << "Element at position " << pos << " is " << arr[top - pos + 1] << ".\n";

void change(int pos, int value) {

if (pos <= 0 || pos > top + 1) {

cout << "Invalid position! Position must be between 1 and " << top + 1 << ".\n";

return;

arr[top - pos + 1] = value;

cout << "Changed element at position " << pos << " to " << value << ".\n";

void display() {

if (top == -1) {

cout << "Stack is empty!\n";

return;

cout << "Stack elements: ";

for (int i = top; i >= 0; i--) {

cout << arr[i] << " ";

}
cout << "\n";

};

int main() {

Stack stack;

int choice, value, pos;

while (true) {

cout << "\nStack Operations:\n";

cout << "1. PUSH\n";

cout << "2. POP\n";

cout << "3. PEEP\n";

cout << "4. CHANGE\n";

cout << "5. DISPLAY\n";

cout << "6. EXIT\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter the value to push: ";

cin >> value;

stack.push(value);

break;

case 2:

stack.pop();

break

case 3:

cout << "Enter the position to peep: ";

cin >> pos;

stack.peep(pos);

break;

case 4:

cout << "Enter the position to change: ";

cin >> pos;


cout << "Enter the new value: ";

cin >> value;

stack.change(pos, value);

break;

case 5:

stack.display();

break;

case 6:

cout << "Exiting program.\n";

return 0;

default:

cout << "Invalid choice! Please try again.\n";

Output: -

4.Implement a program to convert infix notation to postfix notation


using stack.
Code: -
#include <stdio.h>

using namespace std;

int prec(char c) {

if (c == '^')
return 3;

else if (c == '/' || c == '*')

return 2;

else if (c == '+' || c == '-')

return 1;

else

return -1;

void infixToPostfix(string s) {

stack<char> st;

string result;

for (int i = 0; i < s.length(); i++) {

char c = s[i];

if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))

result += c;

else if (c == '(')

st.push('(');

else if (c == ')') {

while (st.top() != '(') {

result += st.top();

st.pop();

st.pop();

else {

while (!st.empty() && prec(c) < prec(st.top()) ||

!st.empty() && prec(c) == prec(st.top())) {

result += st.top();

st.pop();

st.push(c);

while (!st.empty()) {

result += st.top();
st.pop();

cout << result << endl;

int main () {

string exp = "a+b*(c^d-e)^(f+g*h)-i";

infixToPostfix(exp);

return 0;

Output: -

5. Write a program to implement QUEUE using arrays that


performs following operations
(a)INSERT (b) DELETE (c) DISPLAY
Code: -
// C++ Program to implement a queue using array

#include <iostream>

using namespace std;

#define MAX_SIZE 100

class Queue {

public:

int front;

int rear;

int arr[MAX_SIZE];

Queue(): front(-1), rear(-1) {}

bool isEmpty() { return front == -1 || front > rear; }

bool isFull() { return rear == MAX_SIZE - 1; }

int getFront()

{
if (isEmpty()) {

cout << "Queue is empty" << endl;

return -1;

return arr[front];

int getRear()

if (isEmpty()) {

cout << "Queue is empty" << endl;

return -1;

return arr[rear];

void enqueue(int val)

if (isFull()) {

cout << "Queue is full" << endl;

return;

if (isEmpty())

front = 0;

rear++;

arr[rear] = val;

int dequeue()

if (isEmpty()) {

cout << "Queue is empty" << endl;

return -1;

int ans = arr[front];

front++;

if (isEmpty())

front = rear = -1;


return ans;

void display()

if (isEmpty()) {

cout << "Queue is empty" << endl;

return;

cout << "Queue: ";

for (int i = front; i <= rear; i++) {

cout << arr[i] << " ";

cout << endl;

};

int main()

Queue q;

q.enqueue(1);

q.enqueue(2);

q.enqueue(3);

cout << "\nAfter Enqueueing:" << endl;

cout << "Front element: " << q.getFront() << endl;

cout << "Rear element: " << q.getRear() << endl;

q.display();

q.enqueue(4);

q.enqueue(5);

q.display();

q.enqueue(6);

cout << "\nDequeueing elements:" << endl;

cout << "Dequeued element: " << q.dequeue() << endl;

cout << "Dequeued element: " << q.dequeue() << endl

cout << "\nAfter Dequeueing:" << endl;

cout << "Front element: " << q.getFront() << endl;


cout << "Rear element: " << q.getRear() << endl;

q.display();

return 0;

Output: -

6.Write a program to implement Circular Queue using arrays that


performs following Operations.
(a) INSERT (b) DELETE (c) DISPLAY
Code: -
#include <bits/stdc++.h>

#define MAX_SIZE 5

using namespace std;

class Queue {

public:

int front, rear;

int arr[MAX_SIZE];
Queue() { front = rear = 0; }

bool isEmpty()

if (front == rear)

return true;

return false;

bool isFull()

if ((rear + 1) % MAX_SIZE == front)

return true;

return false;

void enqueue(int val)

if (this->isFull()) {

printf("Queue Overflow!\n");

return;

rear = (rear + 1) % MAX_SIZE;

arr[rear] = val;

void dequeue()

if (this->isEmpty()) {

printf("Queue Underflow!\n");

return;

front = (front + 1) % MAX_SIZE;

int peek()

if (this->isEmpty()) {

printf("Queue is Empty!\n");

return -1;

}
return arr[(front + 1) % MAX_SIZE];

void print()

if (this->isEmpty())

return;

for (int i = (front + 1) % MAX_SIZE; i < rear;

i = (i + 1) % MAX_SIZE) {

printf("%d ", arr[i]);

cout << arr[rear];

};

int main()

Queue q;

q.enqueue(11);

q.enqueue(11);

q.enqueue(11);

q.enqueue(11);

q.enqueue(11);

q.enqueue(11);

q.dequeue();

q.dequeue();

q.enqueue(123);

q.print();

return 0;

Output: -
7. Write a menu driven program to implement following operations
on the singly linked list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
c) Delete a first node of the linked list.
(d) Delete a node before specified position.
(e) Delete a node after specified position.
Code: -
#include <iostream>

using namespace std;

struct Node {

int data;

Node* next;

};

Node* createNode(int data) {

Node* newNode = new Node();

newNode->data = data;

newNode->next = nullptr;

return newNode;

void insertAtFront(Node*& head, int data) {

Node* newNode = createNode(data);

newNode->next = head;

head = newNode;

cout << "Node with data " << data << " inserted at the front.\n";

void insertAtEnd(Node*& head, int data) {

Node* newNode = createNode(data);

if (head == nullptr) {

head = newNode;

} else {

Node* temp = head;

while (temp->next != nullptr) {


temp = temp->next;

temp->next = newNode;

cout << "Node with data " << data << " inserted at the end.\n";

void deleteFirstNode(Node*& head) {

if (head == nullptr) {

cout << "The list is empty. No node to delete.\n";

return;

Node* temp = head;

head = head->next;

cout << "Node with data " << temp->data << " deleted from the front.\n";

delete temp;

void deleteBeforePosition(Node*& head, int pos) {

if (head == nullptr || pos <= 1) {

cout << "Invalid position or list is too short.\n";

return;

Node* temp = head;

Node* prev = nullptr;

Node* prevPrev = nullptr;

int count = 1;

while (temp != nullptr && count < pos) {

prevPrev = prev;

prev = temp;

temp = temp->next;

count++;

if (prevPrev == nullptr || prev == nullptr) {

cout << "No node exists before position " << pos << ".\n";

return;

prevPrev->next = temp;
cout << "Node with data " << prev->data << " deleted before position " << pos << ".\n";

delete prev;

void deleteAfterPosition(Node*& head, int pos) {

if (head == nullptr) {

cout << "The list is empty. No node to delete.\n";

return;

Node* temp = head;

int count = 1;

while (temp != nullptr && count < pos) {

temp = temp->next;

count++;

if (temp == nullptr || temp->next == nullptr) {

cout << "No node exists after position " << pos << ".\n";

return;

Node* delNode = temp->next;

temp->next = delNode->next;

cout << "Node with data " << delNode->data << " deleted after position " << pos << ".\n";

delete delNode;

void displayList(Node* head) {

if (head == nullptr) {

cout << "The list is empty.\n";

return;

cout << "Linked list: ";

while (head != nullptr) {

cout << head->data << " -> ";

head = head->next;

cout << "NULL\n";

int main() {
Node* head = nullptr;

int choice, data, pos;

while (true) {

cout << "\nSingly Linked List Operations:\n";

cout << "1. Insert at front\n";

cout << "2. Insert at end\n";

cout << "3. Delete first node\n";

cout << "4. Delete node before specified position\n";

cout << "5. Delete node after specified position\n";

cout << "6. Display list\n";

cout << "7. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter the data to insert at the front: ";

cin >> data;

insertAtFront(head, data);

break;

case 2:

cout << "Enter the data to insert at the end: ";

cin >> data;

insertAtEnd(head, data);

break;

case 3:

deleteFirstNode(head);

break;

case 4:

cout << "Enter the position before which to delete: ";

cin >> pos;

deleteBeforePosition(head, pos);

break;

case 5:

cout << "Enter the position after which to delete: ";

cin >> pos;

deleteAfterPosition(head, pos);
break;

case 6:

displayList(head);

break;

case 7:

cout << "Exiting program.\n";

return 0;

default:

cout << "Invalid choice! Please try again.\n";

Output: -

8. Write a program to implement stack using linked list.


Code: -

You might also like