QB Ref1 Updated
QB Ref1 Updated
Pop function
1.Check whether the head node is NULL
2.if head == NULL
The stack is empty. we can't pop the element.
3.Otherwise,
Move to head node to the next node. head = head->next;
Free the head node's memory
C Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
if(head == NULL)
printf("Stack is Empty\n");
else
{
printf("Poped element = %d\n", head->data);
int main()
{
push(10);
push(20);
push(30);
printf("Linked List\n");
printList();
pop();
printf("After the pop, the new linked list\n");
printList();
pop();
printf("After the pop, the new linked list\n");
printList();
return 0;
}
2. Explain the steps involved in insertion into a singly linked list and how nodes are inserte
d after a specific node?
What is Linked List?
When we want to work with an unknown number of data values, we use a linked list data
structure to organize that data. The linked list is a linear data structure that contains a sequence of
elements such that each element links to its next element in the sequence. Each element in a
linked list is called "Node".
Insertion
Deletion
Display
Before we implement actual operations, first we need to set up an empty list. First, perform the
following steps before implementing actual operations.
Step 1 - Include all the header files which are used in the program.
Step 2 - Declare all the user defined functions.
Step 3 - Define a Node structure with two members data and next
Step 4 - Define a Node pointer 'head' and set it to NULL.
Step 5 - Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user selected operation.
Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as
follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Source code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insertBetween(int,int,int);
struct Node
{
int data;
struct Node *next;
}*head = NULL;
Insertion
Deletion
Count
Display
1.Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as
follows...
2.Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as
follows...
3.Count
Count the total number of nodes in linked list.
int count() {
int count = 0;
temp_node = head_node;
while (temp_node != 0) {
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}
4.Display – Used to display the linked list
void display()
{
if(head == NULL)
{
printf("\nList is Empty\n");
}
else
{
struct Node *temp = head;
printf("\n\nList elements are - \n");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
}
Write output with node diagram explanation
3) Describe about stack ADT using array in detail.
Stack is a linear data structure in which the insertion and deletion operations are performed at
only one end. In a stack, adding and removing of elements are performed at a single position
which is known as "top". That means, a new element is added at top of the stack and an element
is removed from the top of the stack. In stack, the insertion and deletion operations are
performed based on LIFO (Last In First Out) principle.
Example
If we want to create a stack by inserting 10,45,12,16,35 and 50. Then 10 becomes the bottom-
most element and 50 is the topmost element. The last inserted element 50 is at Top of the stack
as shown in the image below...
Operations on a Stack
The following operations are performed on the stack...
1. Using Array
2. Using Linked List
When a stack is implemented using an array, that stack can organize an only limited number of
elements. When a stack is implemented using a linked list, that stack can organize an unlimited
number of elements.
#include <stdio.h>
#include <conio.h>
#define max 5
static int stack[max];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return (stack[top--]);
}
void view()
{
int i;
if (top < 0)
printf("\n Stack Empty \n");
else
{
printf("\n Top-->");
for(i=top; i>=0; i--)
{
printf("%4d", stack[i]);
}
printf("\n");
}
}
main()
{
int ch=0, val;
clrscr();
while(ch != 4)
{
printf("\n STACK OPERATION \n");
printf("1.PUSH ");
printf("2.POP ");
printf("3.VIEW ");
printf("4.QUIT \n");
printf("Enter Choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(top < max-1)
{
printf("\nEnter Stack element : ");
scanf("%d", &val);
push(val);
}
else
printf("\n Stack Overflow \n");
break;
case 2:
if(top < 0)
printf("\n Stack Underflow \n");
else
{
val = pop();
printf("\n Popped element is %d\n", val);
}
break;
case 3:
view();
break;
case 4:
exit(0);
default:
printf("\n Invalid Choice \n");
}
}
}
Output
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 23
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 34
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 45
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
Top--> 45 34 23 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 2
Popped element is 45
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
Top--> 34 23 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 4
Insertion
Deletion
Count
Display
Singly Linked List:-
Singly linked list is the most common linked list among the others. The singly linked list can be
traversed only in one direction. It is a collection of ordered sets of elements. In singly linked list,
Each node has a data and a pointer to the next node.
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
typedef struct node DATA_NODE;
DATA_NODE *head_node=0, *current_node, *temp_node,
*prev_node, next_node;
int data;
void insert() {
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);
temp_node = (DATA_NODE *) malloc(sizeof(DATA_NODE));
temp_node->value = data;
if (head_node == 0) {
head_node = temp_node;
} else {
current_node->next = temp_node;
}
temp_node->next = 0;
current_node = temp_node;
}
void deleteatFirst() {
if(head_node == NULL)
printf("\n\nList is Empty!!!");
else
{
struct Node *temp_node = head_node;
if(head_node->next == NULL)
{
head_node = NULL;
}
else
{
head = temp->next;
printf("\nOne node deleted!!!\n\n");
}
}
void display() {
int count = 0;
temp_node = head_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
}
int count() {
int count = 0;
temp_node = head_node;
while (temp_node != 0) {
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}
int main() {
insert();
deleteatFirst() ;
display();
count();
}
}
return 0;
}
Write output with node explanation diagram
5.Explain the various operations of the list ADT with examples.
LIST:
A list is a sequential data structure, ie. a collection of items accessible one after another
beginning at the head and ending at the tail.
It is a widely used data structure for applications which do not need random access
Addition and removals can be made at any position in the list
lists are normally in the form of a1,a2,a3.....an. The size of this list is n.The first element of
the list is a1,and the last element is an.The position of element ai in a list is i.
List of size 0 is called as null list.
Basic Operations on a List
Creating a list
Traversing the list
Inserting an item in the list
Deleting an item from the list
Implementation of List:
1. Array list
2. Linked list
#include<stdio.h>
#define MAX 10
void create()
{
printf("\n Enter the number of nodes");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}
void deletion()
{
printf("\n Enter the position u want to delete::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
printf("\n The Elements after deletion");
for(i=0;i<n;i++)
{
printf("\t%d", b[i]);
}
}
void search()
{
printf("\n Enter the Element to be searched:");
scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
printf("Value is in the %d Position", i);
}
else
{
printf("Value %d is not in the list::", e);
continue;
}
}
}
void insert()
{
printf("\n Enter the position u need to insert::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n invalid Location::");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);
b[pos]=p;
n++;
}
printf("\n The list after insertion::\n");
display();
}
void display()
{
printf("\n The Elements of The list ADT are:");
for(i=0;i<n;i++)
{
printf("\n\n%d", b[i]);
}
}
void main()
{
Create();
Delete();
Insert();
Search();
}
Write output with diagram
Simply a list is a sequence of data, and the linked list is a sequence of data linked with each
other.
The formal definition of a single linked list is as follows...
Single linked list is a sequence of elements in which every element has link to its next
element in the sequence.
In any single linked list, the individual element is called as "Node". Every "Node" contains two
fields, data field, and the next field. The data field is used to store actual value of the node and
next field is used to store the address of next node in the sequence.
The graphical representation of a node in a single linked list is as follows...
Insertion
Deletion
Count
Display
Singly Linked List:-
Singly linked list is the most common linked list among the others. The singly linked list can be
traversed only in one direction. It is a collection of ordered sets of elements. In singly linked list,
Each node has a data and a pointer to the next node.
Source Code
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
typedef struct node DATA_NODE;
DATA_NODE *head_node=0, *current_node, *temp_node,
*prev_node, next_node;
int data;
void insert() {
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);
temp_node = (DATA_NODE *) malloc(sizeof(DATA_NODE));
temp_node->value = data;
if (head_node == 0) {
head_node = temp_node;
} else {
current_node->next = temp_node;
}
temp_node->next = 0;
current_node = temp_node;
}
void deleteatFirst() {
if(head_node == NULL)
printf("\n\nList is Empty!!!");
else
{
struct Node *temp_node = head_node;
if(head_node->next == NULL)
{
head_node = NULL;
}
else
{
head = temp->next;
printf("\nOne node deleted!!!\n\n");
}
}
void display() {
int count = 0;
temp_node = head_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
}
int count() {
int count = 0;
temp_node = head_node;
while (temp_node != 0) {
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}
int main() {
insert();
deleteatFirst() ;
display();
count();
}
}
return 0;
}
Write output with node diagram
Copy
OUTPUT:
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:10
Enter no 2:54
Enter no 3:98
Enter no 4:234
Deleted Element is 10
Enter the Choice:3
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void dequeue()
{
//used to free the first node after dequeue
struct node *temp;
if(front == NULL)
printf("Queue is Empty. Unable to perform dequeue\n");
else
{
//take backup
temp = front;
void printList()
{
struct node *temp = front;
while(temp)
{
printf("%d->",temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
printf("Queue :");
printList();
dequeue();
printf("After dequeue the new Queue :");
printList();
dequeue();
printf("After dequeue the new Queue :");
printList();
return 0;
}
Output:
7. Give an algorithm to convert an infix expression to a postfix expression using stack with
suitable example
Refer our class notes- algorithm and example problem
8. What are the ways to insert a node in linked list? Write an algorithm for inserting a node
before a given node in a linked list.
Single linked list is a sequence of elements in which every element has link to its next
element in the sequence.
In any single linked list, the individual element is called as "Node". Every "Node" contains two
fields, data field, and the next field. The data field is used to store actual value of the node and
next field is used to store the address of next node in the sequence.
The graphical representation of a node in a single linked list is as follows...
Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as
follows...
newNode->data = value;
if(head == NULL)
newNode->next = NULL;
head = newNode;
}
else
newNode->next = head;
head = newNode;
Node Representation:
9.What are circular queues. Explain the procedure to insert an element to circular queue
and delete an element from a circular queue using array implementation.
Circular Queue In C
A circular queue solved the limitations of the normal queue. Thus making it a better pick than the
normal queue. It also follows the first come first serve algorithm. Circular Queue is also
called ring Buffer.
Source Code
#include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
return;
if(front == -1)
{
front = 0;
rear = 0;
else
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
cqueue_arr[rear] = item ;
void deletion()
if(front == -1)
printf("Queue Underflown");
return ;
if(front == rear)
{
front = -1;
rear=-1;
else
if(front == MAX-1)
front = 0;
else
front = front+1;
void display()
if(front == -1)
printf("Queue is emptyn");
return;
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
else
printf("%d ",cqueue_arr[front_pos])
front_pos++;
front_pos = 0;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
printf("n");
int main()
int choice,item;
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
deletion();
display();
return 0;
Write Output
10. Demonstrate a program to calculate the factorial and finding a GCD of a given number
using recursion.
Recursion:
A function that contains a call to itself is called the recursive function. A technique of defining
the recursive function is called recursion. The recursive function allows us to divide the complex
problem into identical single simple cases that can handle easily.
(i)GCD
Greatest Common Measure(GCM) and Greatest Common Divisor(GCD) are the other terms
used to refer to HCF. Example: HCF of 60 and 75 = 15 because 15 is the highest number which
divides both 60 and 75 exactly.
C program to find gcd of two numbers using recursion
#include<stdio.h>
result = hcf(n1,n2);
printf("GCD of %d and %d = %d",n1,n2,result);
return 0;
}
Output for the different test-cases:-
(ii)Finding Factorial:
The C program given here is a solution for Finding the Factorial of a given number using
Recursion. A straight definition of recursion is, a function calls itself. Each recursive call will be
stored in Stack.
#include<stdio.h>
int main()
{
int num, result;
printf("Enter a positive number: ");
scanf("%d",&num);
result= factorial(num); //function call
printf("Result = %d\n",result);
return 0;
}
Output for the different test-cases:-
11.Explain about the different types of data structure? Discuss about the different data