0% found this document useful (0 votes)
2 views

Data Structures Assignment Complete

The document covers fundamental data structures and algorithms, including stacks, queues, linked lists, and their operations. It provides implementations for various operations using arrays and linked lists, as well as algorithms for converting infix expressions to postfix and reversing linked lists. Additionally, it highlights the differences between arrays and linked lists, along with circular linked list operations.

Uploaded by

ipas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Structures Assignment Complete

The document covers fundamental data structures and algorithms, including stacks, queues, linked lists, and their operations. It provides implementations for various operations using arrays and linked lists, as well as algorithms for converting infix expressions to postfix and reversing linked lists. Additionally, it highlights the differences between arrays and linked lists, along with circular linked list operations.

Uploaded by

ipas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Data Structures and Algorithms Assignment

1. Explain basic operations on stack. Write algorithms on different operations on stack using

array and linked list.

A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. Basic operations include:

- Push: Add an element to the top of the stack.

- Pop: Remove the top element from the stack.

- Peek: View the top element without removing it.

// Stack Implementation using Array

#include <stdio.h>

#define MAX 100

int stack[MAX], top = -1;

void push(int value) {

if (top == MAX - 1)

printf("Stack Overflow\n");

else

stack[++top] = value;

void pop() {

if (top == -1)

printf("Stack Underflow\n");

else

printf("Popped element: %d\n", stack[top--]);

}
void peek() {

if (top == -1)

printf("Stack is empty\n");

else

printf("Top element: %d\n", stack[top]);

int main() {

push(10);

push(20);

peek();

pop();

peek();

return 0;

2. Write different operations on queue and circular queue and write algorithms for that.

Queue is a linear data structure following FIFO (First In, First Out). Basic operations include:

- Enqueue: Add an element to the rear.

- Dequeue: Remove an element from the front.

Circular Queue wraps around when reaching the end of the array, forming a circle.

// Circular Queue Implementation

#include <stdio.h>

#define SIZE 5

int queue[SIZE], front = -1, rear = -1;


void enqueue(int value) {

if ((rear + 1) % SIZE == front)

printf("Queue Overflow\n");

else {

if (front == -1) front = 0;

rear = (rear + 1) % SIZE;

queue[rear] = value;

void dequeue() {

if (front == -1)

printf("Queue Underflow\n");

else {

printf("Dequeued element: %d\n", queue[front]);

if (front == rear) front = rear = -1;

else front = (front + 1) % SIZE;

int main() {

enqueue(10);

enqueue(20);

dequeue();

dequeue();

return 0;
}

3. Write an algorithm to convert infix to postfix expression.

Infix to Postfix Conversion:

1. Traverse the infix expression.

2. If the character is an operand, add it to the output.

3. If the character is an operator, push it onto the stack.

4. Pop operators from the stack to the output until the precedence is less.

5. Pop all operators to the output at the end.

// Infix to Postfix Conversion

#include <stdio.h>

#include <ctype.h>

#define MAX 100

char stack[MAX];

int top = -1;

void push(char c) {

stack[++top] = c;

char pop() {

return stack[top--];

int precedence(char c) {
if (c == '^') return 3;

if (c == '*' || c == '/') return 2;

if (c == '+' || c == '-') return 1;

return 0;

void infixToPostfix(char* exp) {

char output[MAX];

int k = 0;

for (int i = 0; exp[i] != ''; i++) {

if (isalnum(exp[i]))

output[k++] = exp[i];

else if (exp[i] == '(')

push(exp[i]);

else if (exp[i] == ')') {

while (top != -1 && stack[top] != '(')

output[k++] = pop();

pop();

} else {

while (top != -1 && precedence(stack[top]) >= precedence(exp[i]))

output[k++] = pop();

push(exp[i]);

while (top != -1)

output[k++] = pop();
output[k] = '';

printf("Postfix Expression: %s\n", output);

int main() {

char exp[] = "a+b*(c^d-e)^(f+g*h)-i";

infixToPostfix(exp);

return 0;

4. Give the differences between array and linked list. Explain different operations on singly

linked list.

Arrays vs Linked Lists:

- Arrays have fixed size, while linked lists are dynamic.

- Arrays allow random access, linked lists allow sequential access.

- Insertion and deletion are costly in arrays, while efficient in linked lists.

Operations on Singly Linked List:

1. Insertion: Add a node at the beginning, end, or middle.

2. Deletion: Remove a node from the beginning, end, or middle.

// Singly Linked List Implementation

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;


};

void insertAtEnd(struct Node** head, int value) {

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

newNode->data = value;

newNode->next = NULL;

if (*head == NULL)

*head = newNode;

else {

struct Node* temp = *head;

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

void deleteAtBegin(struct Node** head) {

if (*head == NULL)

printf("List is empty\n");

else {

struct Node* temp = *head;

*head = (*head)->next;

free(temp);

}
void displayList(struct Node* head) {

while (head != NULL) {

printf("%d -> ", head->data);

head = head->next;

printf("NULL\n");

int main() {

struct Node* head = NULL;

insertAtEnd(&head, 10);

insertAtEnd(&head, 20);

displayList(head);

deleteAtBegin(&head);

displayList(head);

return 0;

5. Write algorithm to insert and delete a node in circular linked list.

Circular Linked List:

1. Insertion: Add a node such that it points back to the head.

2. Deletion: Remove a node and adjust the circular link appropriately.

// Circular Linked List

#include <stdio.h>

#include <stdlib.h>

struct Node {
int data;

struct Node* next;

};

void insertAtEnd(struct Node** head, int value) {

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

newNode->data = value;

if (*head == NULL) {

*head = newNode;

newNode->next = newNode;

} else {

struct Node* temp = *head;

while (temp->next != *head)

temp = temp->next;

temp->next = newNode;

newNode->next = *head;

void deleteAtBegin(struct Node** head) {

if (*head == NULL)

printf("List is empty\n");

else {

struct Node* temp = *head;

struct Node* tail = *head;

while (tail->next != *head)

tail = tail->next;
*head = (*head)->next;

tail->next = *head;

free(temp);

void displayList(struct Node* head) {

if (head == NULL) return;

struct Node* temp = head;

do {

printf("%d -> ", temp->data);

temp = temp->next;

} while (temp != head);

printf("\n");

int main() {

struct Node* head = NULL;

insertAtEnd(&head, 10);

insertAtEnd(&head, 20);

displayList(head);

deleteAtBegin(&head);

displayList(head);

return 0;

6. Write an algorithm to reverse a singly linked list.

Reversing a Linked List:


1. Initialize three pointers: previous, current, and next.

2. Traverse the list, reversing the links.

3. Update the head pointer to the last node.

// Reverse a Linked List

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void reverseList(struct Node** head) {

struct Node *prev = NULL, *current = *head, *next = NULL;

while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;

current = next;

*head = prev;

void insertAtEnd(struct Node** head, int value) {

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

newNode->data = value;
newNode->next = NULL;

if (*head == NULL)

*head = newNode;

else {

struct Node* temp = *head;

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

void displayList(struct Node* head) {

while (head != NULL) {

printf("%d -> ", head->data);

head = head->next;

printf("NULL\n");

int main() {

struct Node* head = NULL;

insertAtEnd(&head, 10);

insertAtEnd(&head, 20);

insertAtEnd(&head, 30);

displayList(head);

reverseList(&head);

displayList(head);
return 0;

You might also like