0% found this document useful (0 votes)
6 views18 pages

Kaagaz 20221104 110730560610

The document outlines the design, development, and implementation of various data structure programs in C/C++, including a menu-driven program for a linked list with operations like insertion, deletion, and searching, as well as sorting algorithms (Selection Sort, Bubble Sort, Insertion Sort) with their time complexities. It also describes stack operations and applications, such as converting infix expressions to postfix, evaluating postfix expressions, and solving the Tower of Hanoi problem. Additionally, it includes a program for implementing queue operations using arrays and linked lists.

Uploaded by

yogeshinfo1245
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)
6 views18 pages

Kaagaz 20221104 110730560610

The document outlines the design, development, and implementation of various data structure programs in C/C++, including a menu-driven program for a linked list with operations like insertion, deletion, and searching, as well as sorting algorithms (Selection Sort, Bubble Sort, Insertion Sort) with their time complexities. It also describes stack operations and applications, such as converting infix expressions to postfix, evaluating postfix expressions, and solving the Tower of Hanoi problem. Additionally, it includes a program for implementing queue operations using arrays and linked lists.

Uploaded by

yogeshinfo1245
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/ 18

I.

Design, Develop and Implement a menu driven Program for list data structure in C/C++ for
the following operations.
a. Creating an Empty list of N Char Items
b. Inserting an Item at a given valid Position (i)
c. Deleting an Item at a given valid Position (i)
d. Searching an Item and show its Position in List.
e. Exit.
Support the program with functions for each of the above operations. Discuss their best
case, worst case and average case time complexity.

#include <stdio.h>
#include <stdlib.h>
// Linked List Node
Struct node {
Int info;
Struct node* link
}
Struct node* start = NULL
// Function to create list with n nodes initially
Void createList()
{
If (start == NULL) {
Int n;
Printf(“\nEnter the number of nodes: “);
Scanf(“%d”, &n);
If (n != 0)
Int data;
Struct node* newnode;
Struct node* temp;
Newnode = malloc(sizeof(struct node));
Start = newnode;
Temp = start;
Printf(“\nEnter number to”
“ be inserted : “);
Scanf(“%d”, &data);
Start->info = data
For (int I = 2; I <= n; i++) {
Newnode = malloc(sizeof(struct node));
Temp->link = newnode;
Printf(“\nEnter number to”
“ be inserted : “);
Scanf(“%d”, &data);
Newnode->info = data;
Temp = temp->link;
}
}
Printf(“\nThe list is created\n”);
}
Else
Printf(“\nThe list is already created\n”);
}
Void insertAtPosition()
{
Struct node *temp, *newnode;
Int pos, data, I = 1;
Newnode = malloc(sizeof(struct node))
// Enter the position and data
Printf(“\nEnter position and data :”);
Scanf(“%d %d”, &pos, &data)
// Change Links
Temp = start;
Newnode->info = data;
Newnode->link = 0;
While (I < pos – 1) {
Temp = temp->link;
I++;
}
Newnode->link = temp->link;
Temp->link = newnode;
}
// Function to delete from any specified
// position from the linked list
Void deletePosition()
{
Struct node *temp, *position;
Int I = 1, pos;
// If LL is empty
If (start == NULL)
Printf(“\nList is empty\n”);
// Otherwise
Else {
Printf(“\nEnter index : “);
// Position to be deleted
Scanf(“%d”, &pos);
Position = malloc(sizeof(struct node));
Temp = start;
// Traverse till position
While (I < pos – 1) {
Temp = temp->link;
I++;
}
// Change Links
Position = temp->link;
Temp->link = position->link;
// Free memory
Free(position)
}
}
// Function to search an element in linked list
Void search()
{
Int found = -1;
// creating node to traverse
Struct node* tr = start;
// first checking if the list is empty or not
If (start == NULL) {
Printf(“Linked list is empty\n”);
}
Else {
Printf(“\nEnter the element you want to search: “);
Int key;
Scanf(“%d”, &key);
// checking by traversing
While (tr != NULL) {
// checking for key
If (tr->info == key) {
Found = 1;
Break;
}
// moving forward if not at this position
Else {
Tr = tr->link;
}
}
// printing found or not
If (found == 1) {
Printf(
“Yes, %d is present in the linked list.\n”,
Key);
}
Else {
Printf(“No, %d is not present in the linked “
“list.\n”,
Key);
}
}
}
// Driver Code
Int main()
{ int choice;
While (1) ;
Scanf(“%d”, &choice)
Switch (choice) {
Case 5:
deletePosition();
break;
case 2:search()
Break;
Case 3:exit(1);
Break;
Case 4: insertAtPosition();
Break;
Printf(“Incorrect Choice\n”);
}
}
Return 0;
}
}
Insertion at specific position:
II. Design, Develop and Implement an algorithm in C/C++ for the following sorting techniques.
a. Selection Sort b. Bubble Sort c. Insertion Sort
Discuss their best case, worst case and average case time complexity.
Ans:
(a) Selection Sort is an easy sorting algorithm that uses comparisons. It functions by repeatedly
locating the first element that is the least in the array's unsorted section and setting it there. The
array is divided into sorted and unsorted halves by this. The smallest element from the unsorted
portion is chosen and switched out with the element in the current position throughout each
iteration.
Time Complexity:
Best Case: O(n2)
Worst Case: O(n2)
Average Case: O(n2)
Selection Sort has the same O(n2) time complexity in all cases, including the best case, worst case,
and average case. This indicates that the array sorting method needs quadratic time to complete. In
order to determine the smallest element and carry out the required swaps, it must iterate through
the array for each element.
Algorithm:
1. Start with an unsorted array.
2. Set the first element as current min.
3. Iterate through the array starting from second element.
4. Compare each element with current minimum.
5. If current element < current min., update current min. to current element.
6. Continue iterating through array repeating the above.
7. At the end we have our sorted array.

(b) A basic comparison-based sorting algorithm is the bubble sort. Until the entire array is sorted, it
continuously switches neighbouring elements that are in the wrong order. The biggest piece in each
pass "bubbles" up to the right place.
Time Complexity:
Best Case: O(n)
Worst Case: O(n2)
Average Case: O(n2)
When the array is already sorted, the temporal complexity is at its lowest. Since Bubble Sort only
needs to verify that the array is sorted once in this scenario, its linear time complexity is O(n). The
worst-case and average-case time complexity, however, is O(n2). This occurs because Bubble Sort
needs to make several swaps during each run when the array is arranged in reverse.
Algorithm:
1. Start with an unsorted array of elements.
2. Iterate through array repeatedly, comparing and swapping adjacent elements if in wrong
order.
3. Continue iterating until no more swaps are performed in a complete iteration.
4. In each iteration, the largest unsorted element “bubbles” up to end of the array.
5. Repeat the above until entire array is sorted.

(c) Insertion Sort is a quick comparison-based sorting algorithm that creates the final sorted array
one element at a time. It separates the array into a sorted and an unsorted section. As it loops over
the unsorted section, it continuously moves elements in the sorted section to the right until it finds
the ideal spot to insert the current element.
Time Complexity:
Best Case: O(n)
Worst Case: O(n2)
Average Case: O(n2)
When the array is already sorted, Insertion Sort has the lowest temporal complexity. This results in a
linear time complexity of O(n), where the algorithm can find the proper place for each element by
running a single comparison. The worst-case and average-case time complexity, however, is O(n2).
As each member in the unsorted portion needs to be compared and shifted many times in the sorted
portion, this occurs when the array is arranged in reverse.
Algorithm:
1. Start with an unsorted array of elements.
2. Iterate through the array from second element.
3. Compare each element with element before it, moving them to right if they are greater.
4. Insert the current element into its correct sorted position among the elements before it.
5. Repeat above steps until array is sorted.
III. Design, Develop and Implement a menu driven Program in C/C++ for the following
operations on STACK of Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate Overflow and Underflow situations on Stack
d. Display the status of Stack
e. e. Exit
Support the program with appropriate functions for each of the above operations
Ans:
#include<bits/stdc++.h>
using namespace std;

void push(stack<int> s)
{
int n, b;
cout<<"Enter the no. of elements: ";
cin>>n;
for (int i=0; i<n; i++)
{
cout<<"Enter the element being inserted: ";
cin>>b;
s.push(b);
}
cout<<"Size of stack currently: "<<s.size()<<endl;
}
void pop(stack<int> s)
{
int n,b;
cout<<"Create stack"<<endl;
cout<<"Enter the no. of elements: ";
cin>>n;
for (int i=0; i<n; i++)
{
cout<<"Enter the element being inserted: ";
cin>>b;
s.push(b);
}
cout<<"Enter the no. of elements being removed: ";
int k;
cin>>k;
for (int i=0; i<k; i++)
{
s.pop();
}
cout<<"Size of stack currently: "<<s.size()<<endl;
}
void flow(stack<int> s)
{
int n,b;
cout<<"Create stack"<<endl;
cout<<"Enter the no. of elements: ";
cin>>n;
for (int i=0; i<n; i++)
{
cout<<"Enter the element being inserted: ";
cin>>b;
s.push(b);
}
int top=-1,a;
cout<<"choice 1: Test for overflow"<<endl;
cout<<"choice 2: Test for underflow"<<endl;
int k;
cin>>k;
if(k==1)
{
cout<<"Enter the element being inserted: ";
cin>>a;
s.push(a);
if(top==n-1)
{
cout<<"Overflow"<<endl;
}
else
{
cout<<"Not Overflow"<<endl;
}
}
else if(k==2)
{
if(top==-1)
{
cout<<"Underflow"<<endl;
}
else
{
cout<<"Not Underflow"<<endl;
}

}
}
void status(stack<int> s)
{
int n,b;
cout<<"Create stack"<<endl;
cout<<"Enter the no. of elements: ";
cin>>n;
for (int i=0; i<n; i++)
{
cout<<"Enter the element being inserted: ";
cin>>b;
s.push(b);
}
for(int i=0; i<n; i++)
{
cout<<s.top()<<endl;
}
}
int main()
{
stack<int> s;
int c;
cout<<"choice 1: element insertion"<<endl;
cout<<"choice 2: element deletion"<<endl;
cout<<"choice 3: underflow and overflow in stack"<<endl;
cout<<"choice 4: status of stack"<<endl;
cout<<"Enter the choice: ";
cin>>c;
if (c==1)
{
push(s);
}
else if (c==2)
{
pop(s);
}
else if (c==3)
{
flow(s);
}
else if (c==4)
{
status(s);
}
else if (c==5)
{
cout<<"Thanks for using the program!"<<endl;
}
return 0;
}

IV. Design, Develop and Implement a Program in C for the following Stack Applications
a. Converting an Infix Expression to Postfix Expression. Program should support for
both parenthesized and free parenthesized expressions with the operators: +, -, *, /,
% (Remainder), ^ (Power) and alphanumeric operands.
b. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /,
%,^
c. Solving Tower of Hanoi problem with n disks
Ans: (a)
#include<bits/stdc++.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;
char c;
for (int i = 0; i < s.length(); i++)
{
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(s[i]) <= 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;
cout<<"Enter String: ";
cin>>exp;
// Function call
infixToPostfix(exp);

return 0;
}

(b)
#include<bits/stdc++.h>
using namespace std;

int evaluatePostfixExpression(string expression) {


stack<int> operandStack;

for (char ch : expression) {


if (isdigit(ch)) {
operandStack.push(ch - '0');
} else {
int operand2 = operandStack.top();
operandStack.pop();
int operand1 = operandStack.top();
operandStack.pop();

switch(ch) {
case '+':
operandStack.push(operand1 + operand2);
break;
case '-':
operandStack.push(operand1 - operand2);
break;
case '*':
operandStack.push(operand1 * operand2);
break;
case '/':
operandStack.push(operand1 / operand2);
break;
case '%':
operandStack.push(operand1 % operand2);
break;
case '^':
operandStack.push(pow(operand1, operand2));
break;
default:
cout << "Invalid operator: " << ch << endl;
return 0;
}
}
}

return operandStack.top();
}

int main() {
string postfixExpression;
cout << "Enter a postfix expression: ";
getline(cin, postfixExpression);

int result = evaluatePostfixExpression(postfixExpression);


cout << "Result: " << result << endl;

return 0;
}
(c)
#include <bits/stdc++.h>
using namespace std;

void towerOfHanoi(int n, char source, char auxiliary, char destination)


{
if (n == 1) {
cout << "Move disk 1 from " << source << " to " << destination
<< endl;
return;
}

towerOfHanoi(n - 1, source, destination, auxiliary);


cout << "Move disk " << n << " from " << source << " to " <<
destination << endl;
towerOfHanoi(n - 1, auxiliary, source, destination);
}

int main() {
int numDisks;
cout << "Enter the number of disks: ";
cin >> numDisks;

cout << "Tower of Hanoi steps:" << endl;


towerOfHanoi(numDisks, 'A', 'B', 'C');

return 0;
}
V. Write a program that implements Queue (its operations) using i) Arrays ii) Linked
list(Pointers).
Ans:
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

int queue[MAX_SIZE];
int front = -1;
int rear = -1;
int size = -1;

void enqueue(int element) {


if (rear == MAX_SIZE - 1)
printf("Queue overflow.\n");
else {
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = element;
printf("Enqueued element: %d\n", element);
}
}

void dequeue() {
if (front == -1 || front > rear) {
printf("Queue is empty.\n");
} else {
printf("Dequeued element: %d\n", queue[front]);
front++;
if (front > rear) {
front = -1;
rear = -1;
}
}
}
void totalelements() {

if(front==-1 || front>rear)
size=0;
else
size=rear-front+1;
printf("Total elements in the queue: %d\n", size);
}

int main() {
int task, element;

while (1) {
printf("Queue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. total\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &task);

switch (task) {
case 1:
printf("Enter the element to enqueue: ");
scanf("%d", &element);
enqueue(element);
break;
case 2:
dequeue();
break;
case 3:
totalelements();
break;
case 4:
printf("Exiting...\n");
exit(0);
}}

return 0;
}
Vi). Write a program that implement Circular Queue (its operations) using Arrays

You might also like