0% found this document useful (0 votes)
19 views23 pages

Ds Practical Qs

The document provides implementations of various data structures and algorithms in C, including Stack, Queue, Linked List, Circular Queue, and Doubly Linked List. It includes functions for basic operations such as push, pop, enqueue, dequeue, and insertion at both ends of linked lists. Additionally, it covers finding square roots using binary search and evaluating postfix expressions.
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)
19 views23 pages

Ds Practical Qs

The document provides implementations of various data structures and algorithms in C, including Stack, Queue, Linked List, Circular Queue, and Doubly Linked List. It includes functions for basic operations such as push, pop, enqueue, dequeue, and insertion at both ends of linked lists. Additionally, it covers finding square roots using binary search and evaluating postfix expressions.
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/ 23

DATA STRUCTURE

1) Implement Stack ADT using an array. Include push and display opera on
2) Implement Stack ADT using an array. Include push and pop opera on

#include <stdio.h>
#define MAX 5

typedef struct{
int arr[MAX];
int top;
}Stack;

void initStack(Stack* stack){


stack-> top = -1;
}
void push(Stack* stack , int value){
if(stack->top == MAX-1){
prin ("Overflow condi on is reached");
return;
}
stack->top++;
stack->arr[stack->top]=value;
prin ("pushed value in the stack is : %d \n",value);
}

void display(Stack* stack){


if(stack->top == -1){
prin ("Stack is empty");
return;
}
prin ("Elements in stack are: \n");
for(int i = stack->top; i >= 0; i--){
prin ("%d\n", stack->arr[i]);
}
}

void pop(Stack* stack){


if(stack->top == -1){
prin ("Stack is already empty");
return;
}
int popped_value = stack->arr[stack->top];
stack->top--;
prin ("The popped value is : %d \n",popped_value);
}

int main(){
Stack stack;
initStack (&stack);
int choice , value;
while(1){
prin ("choosen an opera on \n");
prin ("Select 1 for push \n");
prin ("Select 2 for pop \n");
prin ("Select 3 for display \n");
prin ("Select 4 for exit \n");
scanf("%d", &choice);
switch(choice){
case 1:
prin ("Enter the value to push: ");
scanf("%d", &value);
push(&stack, value); // Call push func on
break;
case 2:
pop(&stack);
case 3:
display(&stack);
break;
case 4:
prin ("Exi ng Program..");
return 0;
default:
prin ("Please select correct choice of code:");
}
}

return 0;
}
3) Find the square root of the number using binary search technique.

#include <stdio.h>
double squareroot(double num){
double low = 0 , high = num , mid;
double precision = 0.00001;
while((high-low)>precision){
mid = (high + low) /2;
if(mid*mid == num){
return mid;
}else if(mid * mid < num){
low = mid;
}else if(mid*mid > num){

high = mid;
}
}
return (low+high)/2;
}
int main(){
double num;
prin ("Enter the number to find it's square root: ");
scanf("%lf",&num);
if(num == 0 ){

prin ("Cannot find the sqaure root of 0");


}else if(num < 0){
prin ("Cannot find the square root of nega ve number");
}else{
double result = squareroot(num);
prin ("The square root of %.5lf is: %.5lf\n", num, result);
} }
4) Implement Linear Queue ADT using an array. Implement Enqueue and display
opera on
5) Implement Linear Queue ADT using an array. Implement Enqueue and Dequeue
opera on
->
#include <stdio.h>
#define MAX 5
typedef struct {
int arr[MAX];
int front;
int rear;
} Queue;
// Ini alize the queue
void initqueue(Queue* queue) {
queue->front = -1;
queue->rear = -1;
}
// Enqueue opera on
void enqueue(Queue* queue, int value) {
if (queue->rear == MAX - 1) {
prin ("Queue Overflow! Cannot enqueue more elements.\n");
return;
}
if (queue->front == -1) {
queue->front = 0; // Ini alize front if the queue was empty
}
queue->rear++;
queue->arr[queue->rear] = value;
prin ("Enqueued value is %d\n", value);
}
// Dequeue opera on
void dequeue(Queue* queue) {

if (queue->front == -1 || queue->front > queue->rear) {


prin ("Queue Underflow! The queue is already empty.\n");
return;
}
prin ("The dequeued value is: %d\n", queue->arr[queue->front]);
queue->front++;
// Reset queue if it becomes empty
if (queue->front > queue->rear) {
queue->front = -1;
queue->rear = -1;
}
}
// Display opera on
void display(Queue* queue) {
if (queue->front == -1 || queue->front > queue->rear) {
prin ("The queue is empty.\n");
return;
}
prin ("The displayed elements are:\n");
for (int i = queue->front; i <= queue->rear; i++) {
prin ("%d ", queue->arr[i]);
}
prin ("\n");
}
int main() {
Queue queue;
int choice, value;
initqueue(&queue);
while (1) {

prin ("\nChoose an opera on from below:");


prin ("\n1. Enqueue");
prin ("\n2. Dequeue");
prin ("\n3. Display");
prin ("\n4. Exit");
prin ("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
prin ("Enter the value to be inserted in the queue: ");
scanf("%d", &value);
enqueue(&queue, value);
break;
case 2:
dequeue(&queue);
break;
case 3:
display(&queue);
break;
case 4:
prin ("Exi ng the program.\n");
return 0;
default:
prin ("Select the correct choice.\n");
}
}
return 0; }
6) Implement a Singly Linked List. Perform inser on at the end and display the list
7) Implement a Singly Linked List. Perform inser on at the beginning and display the list

#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct Node {
int data;
struct Node* next;
} Node;
// Func on to create a new node
Node* createNode(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
prin ("Memory alloca on failed.\n");
exit(1);
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}

// Insert at the front of the linked list


void insertAtFront(Node** head, int value) {
Node* newNode = createNode(value);
newNode->next = *head;

*head = newNode;
prin ("Inserted %d at the front of the list.\n", value);
}
// Insert at the end of the linked list
void insertAtEnd(Node** head, int value) {

Node* newNode = createNode(value);


if (*head == NULL) {
*head = newNode;
prin ("Inserted %d at the end of the list.\n", value);
return;
}
Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
prin ("Inserted %d at the end of the list.\n", value);
}

// Display the linked list


void displayList(Node* head) {
if (head == NULL) {
prin ("The list is empty.\n");
return;
}
prin ("Linked list elements: ");
Node* temp = head;
while (temp != NULL) {
prin ("%d -> ", temp->data);
temp = temp->next;
}
prin ("NULL\n");
}

// Main func on
int main() {
Node* head = NULL; // Ini alize an empty list

int choice, value;


while (1) {
prin ("\nChoose an opera on:\n");
prin ("1. Insert at the front\n");
prin ("2. Insert at the end\n");
prin ("3. Display the list\n");
prin ("4. Exit\n");
prin ("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
prin ("Enter the value to insert at the front: ");
scanf("%d", &value);
insertAtFront(&head, value);
break;
case 2:
prin ("Enter the value to insert at the end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
displayList(head);
break;
case 4:

prin ("Exi ng the program.\n");


return 0;
default:
prin ("Invalid choice. Please try again.\n");
}
}
return 0;
}
8) Implement Circular Queue ADT using an array.

#include <stdio.h>
#define MAX 5
typedef struct {
int arr[MAX];
int front;
int rear;
} CircularQueue;
// Ini alize the queue
void initQueue(CircularQueue* queue) {
queue->front = -1;
queue->rear = -1;
}
// Enqueue opera on
void enqueue(CircularQueue* queue, int value) {
if ((queue->rear + 1) % MAX == queue->front) {
prin ("Queue Overflow! Cannot enqueue more elements.\n");
return;
}
if (queue->front == -1) {

queue->front = 0; // Ini alize front if queue was empty


}
queue->rear = (queue->rear + 1) % MAX;
queue->arr[queue->rear] = value;
prin ("Enqueued value: %d\n", value);
}

// Dequeue opera on
void dequeue(CircularQueue* queue) {
if (queue->front == -1) {
prin ("Queue Underflow! The queue is empty.\n");
return;
}
prin ("Dequeued value: %d\n", queue->arr[queue->front]);
if (queue->front == queue->rear) {
// If the queue becomes empty
queue->front = -1;
queue->rear = -1;
} else {
queue->front = (queue->front + 1) % MAX;
}
}

// Display opera on
void display(CircularQueue* queue) {
if (queue->front == -1) {
prin ("The queue is empty.\n");
return;
}

prin ("Queue elements: ");


int i = queue->front;
while (1) {
prin ("%d ", queue->arr[i]);
if (i == queue->rear) {
break;
}
i = (i + 1) % MAX;
}
prin ("\n");
}
// Main func on
int main() {
CircularQueue queue;
initQueue(&queue);
int choice, value;
while (1) {
prin ("\nChoose an opera on:\n");
prin ("1. Enqueue\n");
prin ("2. Dequeue\n");
prin ("3. Display\n");
prin ("4. Exit\n");
prin ("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
prin ("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(&queue, value);

break;
case 2:
dequeue(&queue);
break;
case 3:
display(&queue);
break;
case 4:
prin ("Exi ng the program.\n");
return 0;
default:
prin ("Invalid choice! Please try again.\n");
}
}
return 0;
}
9) Implement Doubly Linked List. Perform inser on at the end and display the list
10) Implement Doubly Linked List. Perform inser on at the begin and display the list

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

// Node structure
typedef struct Node {

int data;
struct Node* next;
struct Node* prev;
} Node;
// Func on to create a new node

Node* createNode(int value) {


Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
prin ("Memory alloca on failed.\n");
exit(1);
}
newNode->data = value;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}

// Insert at the beginning of the list


void insertAtBeginning(Node** head, int value) {
Node* newNode = createNode(value);
if (*head != NULL) {
newNode->next = *head;
(*head)->prev = newNode;
}
*head = newNode;
prin ("Inserted %d at the beginning of the list.\n", value);
}

// Insert at the end of the list


void insertAtEnd(Node** head, int value) {
Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
prin ("Inserted %d at the end of the list.\n", value);

return;
}
Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
prin ("Inserted %d at the end of the list.\n", value);
}

// Display the list


void displayList(Node* head) {
if (head == NULL) {
prin ("The list is empty.\n");
return;
}
prin ("Doubly Linked List elements: ");
Node* temp = head;
while (temp != NULL) {
prin ("%d <-> ", temp->data);
temp = temp->next;
}
prin ("NULL\n");
}

int main() {
Node* head = NULL; // Ini alize an empty list
int choice, value;

while (1) {
prin ("\nChoose an opera on:\n");
prin ("1. Insert at the beginning\n");
prin ("2. Insert at the end\n");
prin ("3. Display the list\n");
prin ("4. Exit\n");
prin ("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
prin ("Enter the value to insert at the beginning: ");
scanf("%d", &value);
insertAtBeginning(&head, value);
break;
case 2:
prin ("Enter the value to insert at the end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
displayList(head);
break;
case 4:
prin ("Exi ng the program.\n");
return 0;
default:
prin ("Invalid choice. Please try again.\n");

}
}
return 0;
}
11) Pos ix evalua on
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define MAX 100

// Stack structure
typedef struct {
int arr[MAX];
int top;
} Stack;

// Ini alize the stack


void initStack(Stack* stack) {
stack->top = -1;
}

// Check if the stack is empty


int isEmpty(Stack* stack) {
return stack->top == -1;
}
// Check if the stack is full
int isFull(Stack* stack) {

return stack->top == MAX - 1;


}

// Push an element into the stack


void push(Stack* stack, int value) {
if (isFull(stack)) {
prin ("Stack Overflow!\n");
return;
}
stack->arr[++stack->top] = value;
}

// Pop an element from the stack


int pop(Stack* stack) {
if (isEmpty(stack)) {
prin ("Stack Underflow!\n");
exit(1);
}
return stack->arr[stack->top--];
}

// Evaluate a pos ix expression


int evaluatePos ix(char* expression) {
Stack stack;
initStack(&stack);
char* token = expression;
while (*token != '\0') {
// If the token is a digit, push it onto the stack

if (isdigit(*token)) {
push(&stack, *token - '0');
}
// If the token is an operator, pop two elements, apply the operator, and push the result
else {
int operand2 = pop(&stack);
int operand1 = pop(&stack);
int result;

switch (*token) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
if (operand2 == 0) {
prin ("Division by zero error!\n");
exit(1);
}
result = operand1 / operand2;
break;
case '^':
result = pow(operand1, operand2);
break;

default:
prin ("Invalid operator: %c\n", *token);
exit(1);
}
push(&stack, result);
}
token++;
}
// Final result should be the only element in the stack
return pop(&stack);
}
// Main func on
int main() {
char expression[MAX];
prin ("Enter a pos ix expression: ");
scanf("%s", expression);
int result = evaluatePos ix(expression);
prin ("The result of the pos ix expression is: %d\n", result);

return 0;
}
12) Infix to pos ix
#include <stdio.h>
#include <ctype.h> // For isalpha() and isdigit()
#include <string.h> // For strlen()

#define MAX 100

typedef struct {
char arr[MAX];
int top;
} Stack;

// Ini alize the stack


void initStack(Stack* stack) {
stack->top = -1;
}

// Check if the stack is empty


int isEmpty(Stack* stack) {
return stack->top == -1;
}

// Check if the stack is full


int isFull(Stack* stack) {
return stack->top == MAX - 1;
}

// Push an element onto the stack


void push(Stack* stack, char value) {
if (isFull(stack)) {
prin ("Stack Overflow! Cannot push %c\n", value);
return;
}
stack->arr[++stack->top] = value;
}

// Pop an element from the stack


char pop(Stack* stack) {
if (isEmpty(stack)) {
prin ("Stack Underflow! Cannot pop\n");
return '\0';
}
return stack->arr[stack->top--];
}
// Peek at the top element of the stack
char peek(Stack* stack) {
if (isEmpty(stack)) {
return '\0';
}
return stack->arr[stack->top];
}

// Check operator precedence


int precedence(char op) {
if (op == '+' || op == '-') {
return 1; // Lowest precedence
} else if (op == '*' || op == '/') {
return 2; // Higher precedence
} else if (op == '^') {
return 3; // Highest precedence
}
return 0;
}

// Check if a character is an operator


int isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}

// Convert infix to pos ix


void infixToPos ix(char* infix, char* pos ix) {
Stack stack;
initStack(&stack);
int j = 0; // Index for pos ix

for (int i = 0; i < strlen(infix); i++) {


char ch = infix[i];

// If the character is an operand, add it to pos ix


if (isalnum(ch)) {
pos ix[j++] = ch;
}
// If the character is '(', push it to the stack
else if (ch == '(') {
push(&stack, ch);
}
// If the character is ')', pop and output un l '(' is found
else if (ch == ')') {
while (!isEmpty(&stack) && peek(&stack) != '(') {
pos ix[j++] = pop(&stack);
}
if (!isEmpty(&stack) && peek(&stack) == '(') {
pop(&stack); // Remove '('
}
}
// If the character is an operator
else if (isOperator(ch)) {
while (!isEmpty(&stack) && precedence(peek(&stack)) >= precedence(ch)) {
pos ix[j++] = pop(&stack);
}
push(&stack, ch);
}
}

// Pop all remaining operators from the stack


while (!isEmpty(&stack)) {
pos ix[j++] = pop(&stack);
}

pos ix[j] = '\0'; // Null terminate the pos ix expression


}

int main() {
char infix[MAX], pos ix[MAX];

prin ("Enter an infix expression: ");


scanf("%s", infix);

infixToPos ix(infix, pos ix);

prin ("The pos ix expression is: %s\n", pos ix);

return 0;
}

You might also like