0% found this document useful (0 votes)
12 views15 pages

Geeta16 18dmpweek9

The document contains multiple C programs implementing data structures using linked lists, including singly linked lists, queues, and stacks. Each program provides functionalities such as creating the structure, inserting and deleting elements, checking if the structure is empty, and determining its size. The document also includes sample outputs demonstrating the usage of these data structures.

Uploaded by

divyanegi2208
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)
12 views15 pages

Geeta16 18dmpweek9

The document contains multiple C programs implementing data structures using linked lists, including singly linked lists, queues, and stacks. Each program provides functionalities such as creating the structure, inserting and deleting elements, checking if the structure is empty, and determining its size. The document also includes sample outputs demonstrating the usage of these data structures.

Uploaded by

divyanegi2208
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/ 15

*/. Write an algorithm and a program to implement singly linked list.

The program should


implement following operations: a) Create(k) - create a linked list with single node of value k
b) InsertFront(k) - insert node of value k at the front of the linked list c) InsertEnd(k) - insert
a node of value k at the end of the linked list d) InsertAnywhere(p) - insert a node at specific
position p e) DeleteFront() - delete a node from the front of the linked list f) DeleteEnd() -
delete a node from the end of the linked list g) DeleteAnywhere(p) - delete a node from a
specific position p h) Size() - find the size of the linked list i) IsEmpty() - checks if the linked
list is empty or not j) FindMiddle() - finds the middle element of the linked list

Name – Geeta Negi


Roll no. - 08
Section -G2
Course - Btech
Sem - 3 */

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

struct node {
int data;
struct node *next;
};

struct node *head = NULL;

void create(int k) {
struct node *newnode = malloc(sizeof(struct node));
newnode->data = k;
newnode->next = NULL;
head = newnode;
}

void insertfront(int i) {
struct node *newnode = malloc(sizeof(struct node));
newnode->data = i;
newnode->next = head;
head = newnode;
}

void insertend(int i) {
struct node *newnode = malloc(sizeof(struct node));
newnode->data = i;
newnode->next = NULL;

if (head == NULL) {
head = newnode;
} else {
struct node *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newnode;
}
}

void insertpos(int k) {
struct node *newnode = malloc(sizeof(struct node));
newnode->data = k;
newnode->next = NULL;

int pos;
printf("Enter position: ");
scanf("%d", &pos);

if (pos == 1) {
newnode->next = head;
head = newnode;
} else {
struct node *ptr = head;
for (int i = 1; i < pos - 1 && ptr != NULL; i++) {
ptr = ptr->next;
}
if (ptr != NULL) {
newnode->next = ptr->next;
ptr->next = newnode;
} else {
printf("Position out of bounds\n");
free(newnode);
}
}
}

void deletefront() {
if (head != NULL) {
struct node *temp = head;
head = head->next;
free(temp);
} else {
printf("List is empty\n");
}
}

void deleteEnd() {
if (head == NULL) {
printf("List is empty\n");
return;
}

struct node *temp = head;


if (temp->next == NULL) {
free(head);
head = NULL;
} else {
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
}

void deletepos() {
int pos;
printf("Enter position: ");
scanf("%d", &pos);

if (pos == 1 && head != NULL) {


struct node *temp = head;
head = head->next;
free(temp);
} else {
struct node *ptr = head;
for (int i = 1; i < pos - 1 && ptr != NULL; i++) {
ptr = ptr->next;
}
if (ptr != NULL && ptr->next != NULL) {
struct node *q = ptr->next;
ptr->next = q->next;
free(q);
} else {
printf("Position out of bounds\n");
}
}
}
void findMiddle() {
if (head == NULL) {
printf("List is empty\n");
return;
}
struct node *slow = head;
struct node *fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
printf("Data at middle is %d\n", slow->data);
}
void display(void) {
struct node *temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
int choice, k;
printf("\nMenu:\n");
printf("1. Create List\n");
printf("2. Insert at Front\n");
printf("3. Insert at End\n");
printf("4. Insert at Position\n");
printf("5. Delete from Front\n");
printf("6. Delete from End\n");
printf("7. Delete from Position\n");
printf("8. find middle\n");
printf("9. Display List\n");
printf("10. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

while (choice!=11) {

switch (choice) {
case 1:
if (head == NULL) {
printf("Enter the number to create the list: ");
scanf("%d", &k);
create(k);
printf("The data is %d\n", head->data);
} else {
printf("List already exists. Use other options to modify.\n");
}
break;
case 2:
printf("Enter the number at front: ");
scanf("%d", &k);
insertfront(k);
display();
break;
case 3:
printf("Enter the number at end: ");
scanf("%d", &k);
insertend(k);
display();
break;
case 4:
printf("Enter the number to insert at a position: ");
scanf("%d", &k);
insertpos(k);
display();
break;
case 5:
printf("After deletion at front: ");
deletefront();
display();
break;
case 6:
printf("After deletion at end: ");
deleteEnd();
display();
break;
case 7:
printf("After deletion at position: ");
deletepos();
display();
break;
case 8:
findMiddle();
display();
break;
case 9:
display();
break;
case 10:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
break;
}
printf("Enter your choice: ");
scanf("%d", &choice);
}
return 0;
}

Path:
geeta@tuf:-/Desktop/code/dsa-lab $ gcc 16.c
geeta@tuf:-/Desktop/code/dsa-lab $ ./a.out

OUTPUT
Menu:
1. Create List
2. Insert at Front
3. Insert at End
4. Insert at Position
5. Delete from Front
6. Delete from End
7. Delete from Position
8. find middle
9. Display List
10. Exit

Enter your choice: 1


Enter the number to create the list: 10
The data is 10
Enter your choice: 2
Enter the number at front: 23
23 10
Enter your choice: 3
Enter the number at end: 45
23 10 45
Enter your choice: 4
Enter the number to insert at a position: 2
Enter position: 1
2 23 10 45
Enter your choice: 5
After deletion at front: 23 10 45
Enter your choice: 6
After deletion at end: 23 10
Enter your choice: 1
List already exists. Use other options to modify.
Enter your choice: 2
Enter the number at front: 23
23 23 10
Enter your choice: 2
Enter the number at front: 45
45 23 23 10
Enter your choice: 2
Enter the number at front: 67
67 45 23 23 10
Enter your choice: 9
67 45 23 23 10
Enter your choice: 8
Data at middle is 23
Enter your choice: 7
Enter position: 2
67 23 23 10
Enter your choice: 10
*/. Write an algorithm and a program to implement queue using linked list. The program
should implement following stack operations: a) Create() b) EnQueue() c) DeQueue()
d) IsEmpty() e) Size()

Name – Geeta Negi


Roll no. - 08
Section - G2
Course - Btech
Sem - 3rd */

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node *front, *rear;
int size;
};
struct Queue* Create() {
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->front = queue->rear = NULL;
queue->size = 0;
return queue;
}
void EnQueue(struct Queue* queue, int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if (queue->rear == NULL) {
queue->front = queue->rear = temp;
} else {
queue->rear->next = temp;
queue->rear = temp;
}
queue->size++;
printf("Enqueued: %d\n", data);
}
int DeQueue(struct Queue* queue) {
if (queue->front == NULL) {
printf("Queue is empty.\n");
return -1;
}
struct Node* temp = queue->front;
int dequeued_data = temp->data;
queue->front = queue->front->next;

if (queue->front == NULL) {
queue->rear = NULL;
}

free(temp);
queue->size--;
printf("Dequeued: %d\n", dequeued_data);
return dequeued_data;
}
int IsEmpty(struct Queue* queue) {
return queue->front == NULL;
}
int Size(struct Queue* queue) {
return queue->size;
}
int main() {
struct Queue* queue = NULL;
int choice, value;
printf("\nQueue Operations Menu:\n");
printf("1. Create Queue\n");
printf("2. EnQueue\n");
printf("3. DeQueue\n");
printf("4. Check if Queue is Empty\n");
printf("5. Display Queue Size\n");
printf("6. Exit\n");
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
queue = Create();
printf("Queue created successfully.\n");
break;
case 2:
if (queue == NULL) {
printf("Please create a queue first.\n");
break;
}
printf("Enter value to enqueue: ");
scanf("%d", &value);
EnQueue(queue, value);
break;
case 3:
if (queue == NULL) {
printf("Please create a queue first.\n");
break;
}
DeQueue(queue);
break;
case 4:
if (queue == NULL) {
printf("Please create a queue first.\n");
} else {
if (IsEmpty(queue)) {
printf("Queue is empty.\n");
} else {
printf("Queue is not empty.\n");
}
}
break;
case 5:
if (queue == NULL) {
printf("Please create a queue first.\n");
} else {
printf("Queue size: %d\n", Size(queue));
}
break;
case 6:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}

Path:
geeta@tuf:-/Desktop/code/dsa-lab $ gcc 17.c
geeta@tuf:-/Desktop/code/dsa-lab $ ./a.out

Output
Queue Operations Menu:
1. Create Queue
2. EnQueue
3. DeQueue
4. Check if Queue is Empty
5. Display Queue Size
6. Exit
Enter your choice: 1
Queue created successfully.
Enter your choice: 2
Enter value to enqueue: 4
Enqueued: 4
Enter your choice: 2
Enter value to enqueue: 5
Enqueued: 5
Enter your choice: 2
Enter value to enqueue: 6
Enqueued: 6
Enter your choice: 5
Queue size: 3
Enter your choice: 4
Queue is not empty.
Enter your choice: 3
Dequeued: 4
Enter your choice: 5
Queue size: 2
*/ Write an algorithm and a program to implement stack using linked list. he program should
implement following queue operations: 1. a) Create() 2. b) Push() 3. c) Pop()
4. d) IsEmpty() 5. e) IsFull() 6. f) Size()

Name – Geeta Negi


Roll no. - 08
Section - G2
Course - Btech
Sem - 3rd */

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Stack {
struct Node* top;
int size;
};
struct Stack* Create() {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = NULL;
stack->size = 0;
return stack;
}
void Push(struct Stack* stack, int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = stack->top;
stack->top = new_node;
stack->size++;
printf("Pushed: %d\n", data);
}
int Pop(struct Stack* stack) {
if (stack->top == NULL) {
printf("Stack is empty.\n");
return -1;
}
struct Node* temp = stack->top;
int popped_data = temp->data;
stack->top = stack->top->next;
free(temp);
stack->size--;
printf("Popped: %d\n", popped_data);
return popped_data;
}
int IsEmpty(struct Stack* stack) {
return stack->top == NULL;
}
int IsFull(struct Stack* stack) {
return 0; // A linked list-based stack is never "full" unless memory is exhausted
}
int Size(struct Stack* stack) {
return stack->size;
}
printf("\nStack Operations Menu:\n");
printf("1. Create Stack\n");
printf("2. Push\n");
printf("3. Pop\n");
printf("4. Check if Stack is Empty\n");
printf("5. Check if Stack is Full\n");
printf("6. Display Stack Size\n");
printf("7. Exit\n");
}

int main() {
struct Stack* stack = NULL;
int choice,value;
while (1) {
displayMenu();
printf("Enter your choice: ");
scanf("%d", &ch);

switch (ch) {
case 1:
stack = Create();
printf("Stack created successfully.\n");
break;
case 2:
if (stack == NULL) {
printf("Please create a stack first.\n");
break;
}
printf("Enter value to push: ");
scanf("%d", &val);
Push(stack, val);
break;
case 3:
if (stack == NULL) {
printf("Please create a stack first.\n");
break;
}
Pop(stack);
break;
case 4:
if (stack == NULL) {
printf("Please create a stack first.\n");
} else {
if (IsEmpty(stack)) {
printf("Stack is empty.\n");
} else {
printf("Stack is not empty.\n");
}
}
break;
case 5:
if (stack == NULL) {
printf("Please create a stack first.\n");
} else {
printf("Stack is never full in a linked list implementation.\n");
}
break;
case 6:
if (stack == NULL) {
printf("Please create a stack first.\n");
} else {
printf("Stack size: %d\n", Size(stack));
}
break;
case 7:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}

return 0;
}
Path:
geeta@tuf:-/Desktop/code/dsa-lab $ gcc 18.c
geeta@tuf:-/Desktop/code/dsa-lab $ ./a.out

Output:
Stack Operations Menu:
1. Create Stack
2. Push
3. Pop
4. Check if Stack is Empty
5. Check if Stack is Full
6. Display Stack Size
7. Exit

Enter your choice: 1


Stack created successfully.
Enter your choice: 2
Enter value to push: 12
Pushed: 12
Enter your choice: 2
Enter value to push: 13
Pushed: 13
Enter your choice: 2
Enter value to push: 90
Pushed: 90
Enter your choice: 66
Invalid choice. Please try again.
Enter your choice: 6
Stack size: 3
Enter your choice: 3
Popped: 90
Enter your choice: 4
Stack is not empty.
Enter your choice: 5
Stack is never full in a linked list implementation.
Enter your choice: 7
Exiting...

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy