You are on page 1of 11

Programming Lab – III - Data Structures

Ex.No: 1

Second largest element in a given list

Aim:

To write a C program that finds the second largest element in a given list
of N numbers.

Algorithm:

1. Include necessary header files(limits.h, stdio.h, stdlib.h)


2. Get the size of the list from the user and assign the value to integer
variable ‘N’
3. Create two integer variables as “max” and “smax” and assign values of
INT_MIN+1 & INT_MIN respectively.
4. Get the ‘N’ integers from the user. For each integer ‘x’ in the list check if
‘x’ is greater than ‘max’ and update smax = max and max = x
5. else check if ‘x’ is greater than ‘smax’ and ‘x’ is not equal to ‘max’ and
update as smax = x
6. Print the value of ‘smax’ which holds the second maximum value from
the given list of ‘N’ numbers
Ex. No: 2

Matrix Operations

Aim:

To write a C program to perform Matrix Operations

Algorithm:

1. Create two 2-Dimensional integer arrays to assign the values of two


matrices respectively. Let’s name first matrix as ‘A[][]’ and second
matrix as ‘B[][]’
2. Get the size of rows and cols of first matrix ‘A’ followed by values of
first matrix
3. Get the size of rows and cols of second matrix ‘B’ followed by values of
second matrix
4. To get the sum of both the matrices, use loop within another loop of
which outer loop iterates through the index of the rows (using ‘i’ as
iterator) & inner loop iterates through the index of the columns (using ‘j’
as iterator).
5. Inside the inner loop print the sum of each value of the resultant matrix
which can be obtained by the following expression:
A[i][j] + B[i][j]
6. To get the difference of both the matrices, use loop within another loop of
which outer loop iterates through the index of the rows (using ‘i’ as
iterator) & inner loop iterates through the index of the columns (using ‘j’
as iterator).
7. Inside the inner loop print the difference of each value of the resultant
matrix which can be obtained by the following expression:
A[i][j] - B[i][j]
Ex. No: 3

Linked List Insertion

Aim:

To write a C program for creating a linked list and perform insertion of a


node into the list

Algorithm:

1. Define a structure for each node in the linked list as:


struct node
{
int data;
struct node *next;
};
2. Declare a head pointer to hold the address of the head of the linked list
and initialize it to a value of NULL
3. Define a function called InsertAtHead() which accepts two arguments,
one is head pointer and another is data about to be inserted.
4. Define a function called display() which displays the contents of the List.
5. Inside Insert function, do the following:
a. Create a new node and store the data passed as an argument in it.
b. Change the next value of new node as head
c. Change the head pointer as new node
6. Inside display function, display the contents of the List
Ex. No: 4

Linked List Deletion

Aim:

To write a C program for creating a linked list and perform deletion of a


node from the list.

Algorithm:

1. Define a structure for each node in the linked list as:


struct node
{
int data;
struct node *next;
};
2. Declare a head pointer to hold the address of the head of the linked list and
initialize it to a value of NULL
3. Define a function called DeleteAtHead() which accepts only argument which
is head pointer.
4. Inside DeleteAtHead(), do the following:
 Copy the value of head to a temporary pointer called temp
 Move the head to point to next node (head = head->next)
 Now, delete the head node using free(temp)
Ex. No: 5

Remove every Kth node in a list

Aim:

To write a C program to remove every Kth node in a given singly linked


list.

Algorithm:

1. The deleteK() function takes two argument , head of linked list and an
integer k.
2. To find the ‘k’ the element to delete that node from the linked list find the
size of the linked list (i.e.,number of nodes of linked list) and store the
result in ‘size’
3. copy the head pointer to another pointer called ‘temp’
4. If ‘k’ is greater than size, don’t allow deletion, stop the function. else,
while ‘k’ is lesser than ‘size’, move the temp pointer to the next node and
decrement k.
5. Copy the temp->next to pointer called ‘del’ and update the temp->next to
del->next and remove the del pointer using free(del)
Ex. No: 6

Stack using Array

Aim:

To implement the concept of stack using array method.

Algorithm:

1. Define a global array variable called ‘stack’ of size between


1<=size<=100000
2. Define a global variable called ‘top’ to store the index of the top most
element of the stack.
3. Initialize the top variable to -1 indicating the state of stack as empty
4. Define the following functions Push(), Pop() and display() to carry over
operations of stack.
5. Inside Push() function, check whether the stack is full else increment the
index of top index and store the data in stack[top]
6. Inside Pop() function, check whether the stack is empty else decrement
the value of top
7. Inside display function, display the elements in the stack.
Ex. No: 7

Stack using Linked List

Aim:

To write a C program to implement the concept of Stack using Linked


list.

Algorithm:

1. Define a structure of node with two elements in it namely data and next to
store the data of each node and address of next node respectively.
2. Create a top pointer to store the address of the top of the stack
3. Initialize the top pointer as NULL to show that the stack is empty
4. Define Push() function with “top” of the list as first argument and data to
be pushed to stack as the second argument.
5. The Push() function creates or dynamically allocates memory and store
the data about to be pushed in that node and make the newly allocated
address as top of the stack
6. The Pop() function copy the top pointer address to “temp” pointer and
move the top pointer to point to next node and free the temp pointer
7. The display() function display the elements present in the stack
Ex. No: 8

Next greater element in a stack

Aim:

To write a C program to find the next greater element in a stack.

Algorithm:

1. Get the ‘N’ – size of the list followed by ‘N’ elements from the user.
2. Create a stack to hold ‘N’ elements maximum.
3. Define Push(), Pop(), Display() functions
4. Push the first element to stack
5. Pick rest of the elements one by one from the list and follow the
following steps:
a. Mark the current element as next
b. If stack is not empty, then pop an element from stack and compare
it with next
c. If next is greater than the popped element, then next is the next
greater element for the popped element.
d. Keep popping from the stack while the popped element is smaller
than next. Next becomes the next greater element for all such
popped elements.
e. If next is smaller than the popped element, then push the popped
element back.
6. After the loop in step 5 is over, pop all the elements from the stack and
print -1 as next element for them.
Ex. No: 9

Queue using Array

Aim:

To write a C program to implement the concept of Queue using Array.

Algorithm:

1. Define an array named queue to store the elements of the queue.


2. Define integer variables front and rear to hold the indexes of the front
element of the queue & rear element of the queue respectively.
3. The insertQ() function check whether the queue is full else increment the
rear index and store the data in the queue[rear]
4. The deleteQ() function check whether the queue is empty else increment
the front index
5. The displayQ() function check whether the queue is full else print the
values of the queue by keeping front as the first index of the queue and
rear as the last index of the queue, print all the elements one by one.
Ex. No: 10

Queue using Linked List

Aim:

To write a C program to implement the concept of Queue using Linked


list.

Algorithm:

1. Define a structure of node with two elements in it namely data and next to
store the data of each node and address of next node respectively.
2. Create a front pointer to store the address of the first node of the list.
3. Create a rear pointer to store the address of the last node of the list.
4. Initialize the front pointer and rear pointer as NULL to show that the
queue is empty
5. The InsertQ() function creates or dynamically allocates memory and store
the data about to be pushed in that node and make the newly allocated
address as rear of the queue
6. The DeleteQ() function copy the front pointer address to “temp” pointer
and move the front pointer to point to next node and free the temp pointer
7. The DisplayQ() function display the elements present in the Queue.
Ex. No: 11

Circular Queue using Array

Aim:

To write a C program to implement the concept of Circular Queue using


array.

Algorithm:

1. Define the array named as cqueue[]


2. Define integer variables front and rear to point to the front index and rear
index of the queue respectively.
3. Initialize the front and rear pointers to point to -1 value
4. Define size variable to store the maximum available size of the queue
5. Inside the InsertCQ() function check whether the queue is full. If it is full
then display Queue is full. else increment the rear and insert the element
6. Inside the DeleteCQ() function check whether the queue is empty and
display queue is empty. Else increment the front value and return the
element
7. The DisplayCQ() function display the elements present in the Circular
Queue.

You might also like