Geeta16 18dmpweek9
Geeta16 18dmpweek9
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
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;
}
void deletepos() {
int pos;
printf("Enter position: ");
scanf("%d", &pos);
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
#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()
#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