0% found this document useful (0 votes)
28 views40 pages

QB Ref1 Updated

Uploaded by

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

QB Ref1 Updated

Uploaded by

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

1)Develop a C program for linked list implementation of stack.

Implementation of stack using linked list


Node structure for stack
struct node
{
int data;
struct node *next;
};
To point the last inserted node
struct node *head = NULL;
head = NULL indicates the empty stack.
Push function
We should create the linked in reverse order. So that the head node will always point the last
inserted data.
Using this method, we can access the last inserted element in constant time.
Like,
push(10) = head->10->NULL
push(20) = head->20->10->NULL
push(30) = head->30->20->10->NULL
Push function Algorithm
2. Create a new node with given data.
3. Make the new node points to the head node.
4. Now make the new node as the head node.
Visual representation of the above algorithm
insert 10
After insertion of 20 and 30

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;
};

struct node *head = NULL;

void push(int val)


{
//create new node
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;

//make the new node points to the head node


newNode->next = head;

//make the new node as head node


//so that head will always point the last inserted data
head = newNode;
}
void pop()
{
//temp is used to free the head node
struct node *temp;

if(head == NULL)
printf("Stack is Empty\n");
else

{
printf("Poped element = %d\n", head->data);

//backup the head node


temp = head;

//make the head node points to the next node.


//logically removing the node
head = head->next;

//free the poped element's memory


free(temp);
}
}

//print the linked list


void printList()
{
struct node *temp = head;

//iterate the entire linked list and print the data


while(temp != NULL)
{
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

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".

What is Single Linked List?


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...

Operations on Single Linked List


The following operations are performed on a Single Linked List

 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

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the single linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, set newNode → next = NULL and head = newNode.
 Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5 - Keep moving the temp to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → data is equal to location, here location is
the node value after which we want to insert the newNode).
 Step 6 - Every time check whether temp is reached to last node or not. If it is reached to
last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp to next node.
 Step 7 - Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'

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;

void insertBetween(int value, int loc1, int loc2)


{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
struct Node *temp = head;
while(temp->data != loc1 && temp->data != loc2)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
}
printf("\nOne node inserted!!!\n");
}
void main()
{
int value,choice1,loc1,loc2;
printf("Enter the two values where you wanto insert: ");
scanf("%d%d",&loc1,&loc2);
insertBetween(value,loc1,loc2);
}
Write output with node diagram explanation
3) What is a linked list? Describe the suitable routine segments for any four operations.

What is Single Linked List?


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...
Operations on Single Linked List
The following operations are performed on a Single Linked List

 Insertion
 Deletion
 Count
 Display

1.Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as
follows...

 Inserting At Beginning of the list


 Inserting At End of the list
 Inserting At Specific location in the list
void insertBetween(int value, int loc1, int loc2)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
struct Node *temp = head;
while(temp->data != loc1 && temp->data != loc2)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
}
printf("\nOne node inserted!!!\n");
}

2.Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as
follows...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node
Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the single linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4 - Check whether list is having only one node (temp → next == NULL)
 Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list
conditions)
 Step 6 - If it is FALSE then set head = temp → next, and delete temp.
void removeBeginning()
{
if(head == NULL)
printf("\n\nList is Empty!!!");
else
{
struct Node *temp = head;
if(head->next == NULL)
{
head = NULL;
free(temp);
}
else
{
head = temp->next;
free(temp);
printf("\nOne node deleted!!!\n\n");
}
}
}

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. Push (To insert an element on to the stack)


2. Pop (To delete an element from the stack)
3. Display (To display elements of the stack)
Stack data structure can be implemented in two ways. They are as follows...

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

4.Develop a C program for linked list implementation of list.


What is Single Linked List?
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...

Operations on Single Linked List


The following operations are performed on a Single Linked List

 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:

A list can be implemented in two ways

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...

Operations on Single Linked List


The following operations are performed on a Single Linked List

 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

6.Briefly describe the operations of queue with example.


Program
#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}

Copy

OUTPUT:
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1

Enter no 1:10

Enter the Choice:1

Enter no 2:54

Enter the Choice:1

Enter no 3:98

Enter the Choice:1

Enter no 4:234

Enter the Choice:3

Queue Elements are:


10
54
98
234
Enter the Choice:2

Deleted Element is 10
Enter the Choice:3

Queue Elements are:


54
98
234

Enter the Choice:4

Queue using linked list


Implementation of the queue using linked list
/*
* Program : Queue using linked list
* Language : C
*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};

struct node *front = NULL, *rear = NULL;

void enqueue(int val)


{
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;

//if it is the first node


if(front == NULL && rear == NULL)
//make both front and rear points to the new node
front = rear = newNode;
else
{
//add newnode in rear->next
rear->next = newNode;

//make the new node as the rear node


rear = newNode;
}
}

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;

//make the front node points to the next node


//logically removing the front element
front = front->next;

//if front == NULL, set rear = NULL


if(front == NULL)
rear = NULL;

//free the first node


free(temp);
}

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...

1. Inserting At Beginning of the list


2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting At Beginning of the list


We can use the following steps to insert a new node at beginning of the single linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, set newNode→next = NULL and head = newNode.
 Step 4 - If it is Not Empty then, set newNode→next = head and head = newNode.

void insertAtBeginning(int value)

struct Node *newNode;

newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

if(head == NULL)

newNode->next = NULL;

head = newNode;

}
else

newNode->next = head;

head = newNode;

printf("\nOne node inserted!!!\n");

Node Representation:

Insert 30 in the beginning of already existing list:

Write output for the code

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.

Operations On A Circular Queue

1. Enqueue- adding an element in the queue if there is space in the queue.


2. Dequeue- Removing elements from a queue if there are any elements in the queue
3. Front- get the first item from the queue.
4. Rear- get the last item from the queue.
5. isEmpty/isFull- checks if the queue is empty or full.

Applications Of A Circular Queue

 Memory management: circular queue is used in memory management.


 Process Scheduling: A CPU uses a queue to schedule processes.
 Traffic Systems: Queues are also used in traffic systems.

Source Code

#include<stdio.h>

# define MAX 5

int cqueue_arr[MAX];

int front = -1;

int rear = -1;

void insert(int item)

if((front == 0 && rear == MAX-1) || (front == rear+1))

printf("Queue Overflow n");

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 ;

printf("Element deleted from queue is : %dn",cqueue_arr[front]);

if(front == rear)

{
front = -1;

rear=-1;

else

if(front == MAX-1)

front = 0;

else

front = front+1;

void display()

int front_pos = front,rear_pos = rear;

if(front == -1)

printf("Queue is emptyn");

return;

printf("Queue elements :n");

if( front_pos <= rear_pos )

while(front_pos <= rear_pos)

{
printf("%d ",cqueue_arr[front_pos]);

front_pos++;

else

while(front_pos <= MAX-1)

printf("%d ",cqueue_arr[front_pos])

front_pos++;

front_pos = 0;

while(front_pos <= rear_pos)

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>

// recursive function to find gcd of two number


int gcd(int a, int b)
{
if(b!=0)
return gcd(b, a%b); // general case
else
return a; // base case
}
int main()
{
int n1, n2, result;

printf("Enter two numbers: ");


scanf("%d %d",&n1,&n2);

result = hcf(n1,n2);
printf("GCD of %d and %d = %d",n1,n2,result);

return 0;
}
Output for the different test-cases:-

Enter two numbers: 48 18


GCD of 48 and 18 = 6

(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.

Example Factorial of 4= 4! = 4*3*2*1 or 1*2*3*4


Program for factorial using recursion in C

#include<stdio.h>

// recursive function to find factorial of a number


int factorial(int n)
{
if(n!=0)
return n*factorial(n-1); // general case
else
return 1; // base case
}

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:-

Enter a positive number: 5


Result = 120

Enter a positive number: 7


Result = 5040

11.Explain about the different types of data structure? Discuss about the different data

structure in detail with an example.


Write any linear datastructure with program and output with node representation
List using array

List using Linked list

Stack suing array

Stack using Linked list

Queue using array

Queue using linked list

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy