Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Complete Practical Data Structures Assignment

The document provides a series of C programming assignments focused on implementing various data structures, including stacks (using both arrays and linked lists), singly linked lists, queues (using linked lists), circular queues (using arrays), and insertion methods in singly linked lists. Each section includes algorithms and complete code implementations for the respective data structure. The document serves as a practical guide for understanding and coding fundamental data structures in C.

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)
3 views

Complete Practical Data Structures Assignment

The document provides a series of C programming assignments focused on implementing various data structures, including stacks (using both arrays and linked lists), singly linked lists, queues (using linked lists), circular queues (using arrays), and insertion methods in singly linked lists. Each section includes algorithms and complete code implementations for the respective data structure. The document serves as a practical guide for understanding and coding fundamental data structures in C.

Uploaded by

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

Complete Practical Data Structures Assignment

1. Write a C program to implement a stack using an array. Create a menu-driven program for

that.

Algorithm for Stack Using Array:

1. Initialize the stack with a maximum size and set top = -1.

2. Push operation:

- If top == MAX - 1, display "Stack Overflow."

- Else, increment top and add the element to stack[top].

3. Pop operation:

- If top == -1, display "Stack Underflow."

- Else, remove the element from stack[top] and decrement top.

4. Peek operation:

- If top == -1, display "Stack is empty."

- Else, display stack[top].

5. Repeat menu-driven options for push, pop, peek, and exit.

#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() {

int choice, value;

while (1) {

printf("\nMenu: 1.Push 2.Pop 3.Peek 4.Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to push: ");

scanf("%d", &value);

push(value);

break;

case 2:
pop();

break;

case 3:

peek();

break;

case 4:

return 0;

default:

printf("Invalid choice\n");

return 0;

2. Write a C program to implement a stack using a linked list.

Algorithm for Stack Using Linked List:

1. Define a Node structure with data and a next pointer.

2. Push operation:

- Create a new node.

- Set the new node's next to the current top.

- Update top to the new node.

3. Pop operation:

- If top == NULL, display "Stack Underflow."

- Else, remove the top node and update top to the next node.

4. Peek operation:

- If top == NULL, display "Stack is empty."

- Else, display the data in the top node.

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

struct Node {

int data;

struct Node* next;

};

struct Node* top = NULL;

void push(int value) {

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

newNode->data = value;

newNode->next = top;

top = newNode;

void pop() {

if (top == NULL)

printf("Stack Underflow\n");

else {

struct Node* temp = top;

printf("Popped element: %d\n", top->data);

top = top->next;

free(temp);

}
void peek() {

if (top == NULL)

printf("Stack is empty\n");

else

printf("Top element: %d\n", top->data);

int main() {

int choice, value;

while (1) {

printf("\nMenu: 1.Push 2.Pop 3.Peek 4.Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to push: ");

scanf("%d", &value);

push(value);

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

return 0;
default:

printf("Invalid choice\n");

return 0;

3. Write a C program to implement a singly linked list.

Algorithm for Singly Linked List:

1. Define a Node structure with data and a next pointer.

2. Insertion:

- Create a new node and add it to the beginning, end, or a specific position.

3. Deletion:

- Remove a node from the beginning, end, or a specific position.

4. Traversal:

- Iterate through the list and display each node's data.

#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 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);

return 0;

4. Write a C program to implement a queue using a linked list.


Algorithm for Queue Using Linked List:

1. Define a Node structure with data and a next pointer.

2. Enqueue operation:

- Create a new node and add it to the rear of the queue.

3. Dequeue operation:

- Remove a node from the front of the queue and update the front pointer.

4. Traversal:

- Iterate through the queue and display each node's data.

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* front = NULL;

struct Node* rear = NULL;

void enqueue(int value) {

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

newNode->data = value;

newNode->next = NULL;

if (rear == NULL)

front = rear = newNode;

else {

rear->next = newNode;
rear = newNode;

void dequeue() {

if (front == NULL)

printf("Queue Underflow\n");

else {

struct Node* temp = front;

printf("Dequeued element: %d\n", front->data);

front = front->next;

if (front == NULL)

rear = NULL;

free(temp);

void displayQueue() {

struct Node* temp = front;

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

int main() {
enqueue(10);

enqueue(20);

enqueue(30);

displayQueue();

dequeue();

displayQueue();

return 0;

5. Write a C program to implement a circular queue using an array and a linked list.

Algorithm for Circular Queue Using Array:

1. Initialize front = rear = -1.

2. Enqueue operation:

- If (rear + 1) % MAX == front, display "Queue Overflow."

- Else, update rear and add the element.

3. Dequeue operation:

- If front == -1, display "Queue Underflow."

- Else, remove the element and update front.

4. Traversal:

- Iterate from front to rear circularly to display elements.

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

void displayQueue() {

if (front == -1)

printf("Queue is empty\n");

else {

int i = front;

printf("Queue: ");

while (1) {

printf("%d ", queue[i]);


if (i == rear) break;

i = (i + 1) % SIZE;

printf("\n");

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

displayQueue();

dequeue();

displayQueue();

return 0;

6. Write a C program to insert a node at the beginning, end, and any position in a singly

linked list.

Algorithm for Inserting Nodes in Singly Linked List:

1. Define a Node structure with data and a next pointer.

2. Insert at Beginning:

- Create a new node and point it to the head.

- Update head to the new node.

3. Insert at End:

- Traverse to the end and point the last node's next to the new node.

4. Insert at Position:

- Traverse to the desired position and insert the new node.


#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

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

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

newNode->data = value;

newNode->next = *head;

*head = newNode;

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 insertAtPosition(struct Node** head, int value, int position) {

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

newNode->data = value;

if (position == 1) {

newNode->next = *head;

*head = newNode;

return;

struct Node* temp = *head;

for (int i = 1; i < position - 1 && temp != NULL; i++)

temp = temp->next;

if (temp == NULL)

printf("Position out of range\n");

else {

newNode->next = 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;

insertAtBeginning(&head, 10);

insertAtEnd(&head, 20);

insertAtPosition(&head, 15, 2);

displayList(head);

return 0;

You might also like