Data Structures Assignment Complete
Data Structures Assignment Complete
1. Explain basic operations on stack. Write algorithms on different operations on stack using
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. Basic operations include:
#include <stdio.h>
if (top == MAX - 1)
printf("Stack Overflow\n");
else
stack[++top] = value;
void pop() {
if (top == -1)
printf("Stack Underflow\n");
else
}
void peek() {
if (top == -1)
printf("Stack is empty\n");
else
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:
Circular Queue wraps around when reaching the end of the array, forming a circle.
#include <stdio.h>
#define SIZE 5
printf("Queue Overflow\n");
else {
queue[rear] = value;
void dequeue() {
if (front == -1)
printf("Queue Underflow\n");
else {
int main() {
enqueue(10);
enqueue(20);
dequeue();
dequeue();
return 0;
}
4. Pop operators from the stack to the output until the precedence is less.
#include <stdio.h>
#include <ctype.h>
char stack[MAX];
void push(char c) {
stack[++top] = c;
char pop() {
return stack[top--];
int precedence(char c) {
if (c == '^') return 3;
return 0;
char output[MAX];
int k = 0;
if (isalnum(exp[i]))
output[k++] = exp[i];
push(exp[i]);
output[k++] = pop();
pop();
} else {
output[k++] = pop();
push(exp[i]);
output[k++] = pop();
output[k] = '';
int main() {
infixToPostfix(exp);
return 0;
4. Give the differences between array and linked list. Explain different operations on singly
linked list.
- Insertion and deletion are costly in arrays, while efficient in linked lists.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
temp = temp->next;
temp->next = newNode;
if (*head == NULL)
printf("List is empty\n");
else {
*head = (*head)->next;
free(temp);
}
void displayList(struct Node* head) {
head = head->next;
printf("NULL\n");
int main() {
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
displayList(head);
deleteAtBegin(&head);
displayList(head);
return 0;
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
if (*head == NULL) {
*head = newNode;
newNode->next = newNode;
} else {
temp = temp->next;
temp->next = newNode;
newNode->next = *head;
if (*head == NULL)
printf("List is empty\n");
else {
tail = tail->next;
*head = (*head)->next;
tail->next = *head;
free(temp);
do {
temp = temp->next;
printf("\n");
int main() {
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
displayList(head);
deleteAtBegin(&head);
displayList(head);
return 0;
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
next = current->next;
current->next = prev;
prev = current;
current = next;
*head = prev;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
temp = temp->next;
temp->next = newNode;
head = head->next;
printf("NULL\n");
int main() {
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
displayList(head);
reverseList(&head);
displayList(head);
return 0;