Lab Manual (1) DS
Lab Manual (1) DS
: CS107
LAB MANUAL
Sr. Practical Name Page Grade Faculty
No. No. Signature
1 Introduction to pointers. Call by Value and Call by Reference. 3
1
Student’s Roll No.: CS107
15 Implement hash table with a hash function H(x) maps the value x
at the index x%10 in an Array.
2
Student’s Roll No.: CS107
Practical-1
Aim: Introduction to pointers. Call by Value and Call by Reference.
Pointers
Definition:
A pointer is a variable that stores the memory address of another variable. Instead of holding a
direct value, it holds the address where the value is stored in memory. There are 2 important
operators that we will use in pointers concepts i.e. Dereferencing operator (*) used to declare
pointer variable and access the value stored in the address.
Address operator (&) used to returns the address of a variable or to access the address of a
variable to a pointer.
General form:
Datatype *pointer_name=&variable_name;
Example of Pointers:
Code:
3
Student’s Roll No.: CS107
Output:
Call by Value
Definition:
In call by value method of parameter passing, the values of actual parameters are copied to the
Function’s formal parameters.
• There are two copies of parameters stored in different memory locations.
• One is the original copy and the other is the function copy.
• Any changes made inside functions are not reflected in the actual parameters of the caller.
Example of Call by Value:
4
Student’s Roll No.: CS107
Output:
Call by reference:
Definition:
In call by reference method of parameter passing, the address of the actual parameters is passed
5
Student’s Roll No.: CS107
• Any changes made inside the function are actually reflected in the actual parameters of the
caller.
Example of Call by Reference:
Code:
Output:
6
Student’s Roll No.: CS107
Practical-2
Sometimes the size of the array we declared may be insufficient. To solve this issue, we can
allocate memory manually during run-time. This is known as dynamic memory allocation in C
programming. To allocate memory dynamically, library functions are malloc(), calloc(), realloc()
and free() are used. These functions are defined in the header file.
MALLOC()
The malloc() function allocates a single block of requested memory. It doesn't initialize memory at
execution time, so it has garbage value initially. It returns NULL if memory is not sufficient. The
syntax of malloc() function is given below: ptr=(cast-type*)malloc(byte-size)
Program :-
7
Student’s Roll No.: CS107
Output :-
CALLOC()
The calloc() function allocates multiple blocks of requested memory. It initially initializes all bytes
to zero . It returns NULL if memory is not sufficient. The syntax of calloc() function is given
below:
ptr=(cast-type*)calloc(number, byte-size)
Program :-
8
Student’s Roll No.: CS107
Output :-
9
Student’s Roll No.: CS107
REALLOC()
If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc()
function. In short, it changes the memory size. The syntax of realloc() function is:
ptr=realloc(ptr, new-size)
Program :-
10
Student’s Roll No.: CS107
Output :-
FREE()
The memory occupied by malloc() or calloc() functions must be released by calling free() function.
Otherwise, it will consume memory until the program exits. The syntax of free() function is :
free(ptr);
11
Student’s Roll No.: CS107
Practical No. :- 3
Insert at the end:
If you are inserting an element at the end, there’s no need for shifting elements. You can directly
add the element at the next available position and increase the size.
Program :
12
Student’s Roll No.: CS107
Output :-
13
Student’s Roll No.: CS107
Output :-
Delete Operation:
In the delete operation, the element to be deleted is searched using the linear search, and then the
delete operation is performed followed by shifting the elements.
Program :
14
Student’s Roll No.: CS107
Output :-
15
Student’s Roll No.: CS107
Practical No. :- 4
Aim : Implement a program to convert infix notation to postfix notation using stack.
Code :
#include <stdio.h>
#include <string.h>
#include <ctype.h>
16
Student’s Roll No.: CS107
17
Student’s Roll No.: CS107
18
Student’s Roll No.: CS107
19
Student’s Roll No.: CS107
infixToPostfix(infix);
return 0;
}
Output :
20
Student’s Roll No.: CS107
Practical No. :- 5
Aim : Write a program to implement Queue using arrays that performs following
operations
(a)INSERT (b)DELETE (c)DISPLAY
#include <stdio.h>
#define MAX 5
int queue[MAX], F = -1, R = -1;
void enqueue(int);
void dequeue();
int main()
{
int choice, value;
printf("Choose Operation: \n1) enqueue\n2) dequeue\n3) display\n4) Exit
Program");
while (1)
{
printf("\nEnter Choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
21
Student’s Roll No.: CS107
22
Student’s Roll No.: CS107
23
Student’s Roll No.: CS107
24
Student’s Roll No.: CS107
Practical No. :- 6
Aim : Write a program to implement Circular Queue using arrays that performs
following operations
(a)INSERT (b)DELETE (c)DISPLAY
Code :
#include <stdio.h>
#define MAX 5
void enqueue(int);
void dequeue();
void display();
int main()
{
int choice, value;
printf("Choose Operation: \n1) enqueue\n2) dequeue\n3) display\n4) Exit
Program");
while (1)
{
25
Student’s Roll No.: CS107
26
Student’s Roll No.: CS107
27
Student’s Roll No.: CS107
void dequeue()
{
if (F == -1 && R == -1)
{
printf("Queue Underflow , no elements to remove.");
}
else if (F == R && queue[(F + 1) % MAX] == NULL)
{
printf("Element Deleted: %d", queue[F]);
queue[F] = NULL;
F = -1;
R = -1;
}
else
{
printf("Element Deleted: %d", queue[F]);
queue[F] = NULL;
F = (F + 1) % MAX;
}
28
Student’s Roll No.: CS107
void display()
{
int i = F;
if (F == -1)
{
printf("Queue Underflow.");
}
while (i != R)
{
printf("%d ", queue[i]);
i = (i + 1) % MAX;
}
printf("%d", queue[i]);
}
29
Student’s Roll No.: CS107
Output :
30
Student’s Roll No.: CS107
Practical No. :- 7
Aim : Write a menu driven program to implement the following operations on the
singly linked list.
(a)Insert a node at front of the linked list.
(b)Insert a node at end of the linked list.
(c)Insert a node such that linked list is in ascending order.
(d)Delete first node of linked list.
(e)Delete a node before specified position.
(f)Delete a node after specified position.
Code :
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *head = NULL;
32
Student’s Roll No.: CS107
33
Student’s Roll No.: CS107
34
Student’s Roll No.: CS107
if (position == 2)
{
deleteFirst();
return;
}
35
Student’s Roll No.: CS107
36
Student’s Roll No.: CS107
// Main menu
int main()
{
int choice, value, pos;
while (1)
{
printf("\n***** MENU *****\n");
printf("1. Insert at Front\n");
printf("2. Insert at End\n");
printf("3. Insert in Ascending Order\n");
printf("4. Delete First Node\n");
printf("5. Delete Before Position\n");
printf("6. Delete After Position\n");
printf("7. Display\n");
38
Student’s Roll No.: CS107
switch (choice)
{
case 1:
printf("Enter value: ");
scanf("%d", &value);
insertAtFront(value);
break;
case 2:
printf("Enter value: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 3:
printf("Enter value: ");
scanf("%d", &value);
insertInAscendingOrder(value);
break;
case 4:
deleteFirst();
39
Student’s Roll No.: CS107
40
Student’s Roll No.: CS107
Output :
41
Student’s Roll No.: CS107
Practical No. :- 8
Aim : Write a program to implement stack using linked list.
Code :
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void pop()
{
if (top == NULL)
{
printf("List is empty.\n");
return;
}
struct Node *temp = top;
top = top->next;
printf("Deleted node with value %d from front.\n", temp->data);
free(temp);
}
void display()
{
struct Node *temp = top;
if (top == NULL)
{
printf("List is empty.\n");
43
Student’s Roll No.: CS107
int main()
{
int choice, value;
while (1)
{
printf("\n*** MENU ***\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n"); // Changed from 8 to 4
44
Student’s Roll No.: CS107
switch (choice)
{
case 1:
printf("Enter value: ");
scanf("%d", &value);
Push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice! Try again.\n");
45
Student’s Roll No.: CS107
46
Student’s Roll No.: CS107
Practical No. :- 9
Aim : Write a program to implement queue using linked list.
Code :
#include <stdio.h>
#include <stdlib.h>
// Define the node structure
struct node
{
int data;
struct node *link;
};
struct node *front = NULL;
struct node *rear = NULL;
// Enqueue operation in the queue
void enqueue(int value)
{
struct node *Newnode = (struct node *)malloc(sizeof(struct node));
if (Newnode == NULL)
{
printf("Memory allocation failed.\n");
return;
}
Newnode->data = value;
Newnode->link = NULL;
if (rear == NULL)
{
47
Student’s Roll No.: CS107
48
Student’s Roll No.: CS107
int main()
{
int choice, value;
while (1)
{
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
49
Student’s Roll No.: CS107
50
Student’s Roll No.: CS107
Output :
51
Student’s Roll No.: CS107
Practical No. :- 10
Aim : Write a program to perform the following operations on the doubly 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 the last node of the linked list.
(d) Delete a node before specified position.
Code :
#include <stdio.h>
#include <stdlib.h>
if (head != NULL) {
head->prev = newNode;
}
head = newNode;
52
Student’s Roll No.: CS107
if (head == NULL) {
newNode->prev = NULL;
head = newNode;
printf("%d inserted at end\n", value);
return;
}
temp->next = newNode;
newNode->prev = temp;
printf("%d inserted at end\n", value);
}
if (head->next == NULL) {
printf("Deleted %d from end\n", head->data);
53
Student’s Roll No.: CS107
temp->next = nodeToDelete->next;
if (nodeToDelete->next != NULL) {
nodeToDelete->next->prev = temp;
54
Student’s Roll No.: CS107
while (1) {
printf("1. Insert at Head\n");
printf("2. Insert at End\n");
printf("3. Delete at End\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at head: ");
scanf("%d", &value);
insertAtHead(value);
55
Student’s Roll No.: CS107
return 0;
}
56
Student’s Roll No.: CS107
Output :
57
Student’s Roll No.: CS107
Practical No. :- 11
Aim : Write a program to implement the following operations on the circular
linked list.
Code :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
if (head == NULL) {
head = p;
p->next = head;
} else {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = p;
p->next = head;
}
printf("%d inserted at end.\n", value);
}
58
Student’s Roll No.: CS107
int count = 1;
struct Node* temp = head;
struct Node* prev = NULL;
struct Node* del = NULL;
del = temp->next;
temp->next = del->next;
printf("Deleted %d before position %d.\n", del->data, pos);
free(del);
}
if (del == head) {
head = del->next;
}
void deleteFirstNode() {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}
60
Student’s Roll No.: CS107
void display() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
int main() {
61
Student’s Roll No.: CS107
while (1) {
printf("\n1. Insert at End\n");
printf("2. Delete Before Position\n");
printf("3. Delete After Position\n");
printf("4. Delete First Node\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 2:
printf("Enter position to delete before: ");
scanf("%d", &pos);
deleteBeforePosition(pos);
break;
case 3:
printf("Enter position to delete after: ");
scanf("%d", &pos);
deleteAfterPosition(pos);
break;
case 4:
deleteFirstNode();
break;
case 5:
display();
break;
case 6:
62
Student’s Roll No.: CS107
return 0;
}
Output :
63
Student’s Roll No.: CS107
64