mirror of
https://github.com/Manoj-HV30/ds-lab-codes.git
synced 2026-05-16 19:35:22 +00:00
Add DS lab linked list, stack and queue implementations
This commit is contained in:
+126
@@ -0,0 +1,126 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
typedef struct node {
|
||||
int data;
|
||||
struct node *prev;
|
||||
struct node *next;
|
||||
} NODE;
|
||||
|
||||
|
||||
NODE *head = NULL;
|
||||
|
||||
|
||||
void insert_front(int value) {
|
||||
NODE *newNode = (NODE *)malloc(sizeof(NODE));
|
||||
|
||||
if (newNode == NULL) {
|
||||
printf("Memory allocation failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
newNode->data = value;
|
||||
newNode->prev = NULL;
|
||||
newNode->next = head;
|
||||
|
||||
if (head != NULL) {
|
||||
head->prev = newNode;
|
||||
}
|
||||
|
||||
head = newNode;
|
||||
printf("Inserted %d\n", value);
|
||||
}
|
||||
|
||||
|
||||
void delete_value(int value) {
|
||||
NODE *temp = head;
|
||||
|
||||
if (head == NULL) {
|
||||
printf("List is empty\n");
|
||||
return;
|
||||
}
|
||||
|
||||
while (temp != NULL) {
|
||||
if (temp->data == value) {
|
||||
|
||||
|
||||
if (temp->prev == NULL) {
|
||||
head = temp->next;
|
||||
if (head != NULL)
|
||||
head->prev = NULL;
|
||||
}
|
||||
|
||||
else {
|
||||
temp->prev->next = temp->next;
|
||||
if (temp->next != NULL)
|
||||
temp->next->prev = temp->prev;
|
||||
}
|
||||
|
||||
free(temp);
|
||||
printf("Deleted %d\n", value);
|
||||
return;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
printf("Value not found\n");
|
||||
}
|
||||
|
||||
void display() {
|
||||
NODE *temp = head;
|
||||
|
||||
if (head == NULL) {
|
||||
printf("List is empty\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("List: ");
|
||||
while (temp != NULL) {
|
||||
printf("%d <-> ", temp->data);
|
||||
temp = temp->next;
|
||||
}
|
||||
printf("NULL\n");
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
int choice, value;
|
||||
|
||||
while (1) {
|
||||
printf("\n--- DOUBLY LINKED LIST MENU ---\n");
|
||||
printf("1. Insert at front\n");
|
||||
printf("2. Delete by value\n");
|
||||
printf("3. Display\n");
|
||||
printf("4. Exit\n");
|
||||
printf("Enter choice: ");
|
||||
scanf("%d", &choice);
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case 1:
|
||||
printf("Enter value to insert: ");
|
||||
scanf("%d", &value);
|
||||
insert_front(value);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
printf("Enter value to delete: ");
|
||||
scanf("%d", &value);
|
||||
delete_value(value);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
display();
|
||||
break;
|
||||
|
||||
case 4:
|
||||
printf("Exiting...\n");
|
||||
exit(0);
|
||||
|
||||
default:
|
||||
printf("Invalid choice\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
typedef struct node{
|
||||
int data;
|
||||
struct node* next;
|
||||
}Node;
|
||||
|
||||
Node* front = NULL;
|
||||
Node* rear = NULL;
|
||||
|
||||
void enqueue(int value)
|
||||
{
|
||||
Node* newnode = (Node*)malloc(sizeof(Node));
|
||||
|
||||
if(!newnode){
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
newnode->data = value;
|
||||
newnode->next = NULL;
|
||||
|
||||
if(front == NULL){
|
||||
rear = front = newnode;
|
||||
}else{
|
||||
rear->next = newnode;
|
||||
rear = newnode;
|
||||
}
|
||||
printf("added %d onto rear\n", value);
|
||||
|
||||
|
||||
}
|
||||
|
||||
int dequeue(){
|
||||
Node* temp = front;
|
||||
int item;
|
||||
if(front == NULL){
|
||||
printf("stack is empty");
|
||||
return -1;
|
||||
}
|
||||
item = temp->data;
|
||||
front = front->next;
|
||||
free(temp);
|
||||
|
||||
if(front== NULL){
|
||||
rear = NULL;
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
void display(){
|
||||
Node* temp = front;
|
||||
if(front == NULL){
|
||||
printf("Stack is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
while(temp!=NULL){
|
||||
printf("%d->",temp->data);
|
||||
temp = temp->next;
|
||||
|
||||
}
|
||||
printf("NULL\n");
|
||||
}
|
||||
int main(){
|
||||
int choice,value;
|
||||
while(1){
|
||||
printf("\n---queue menu---");
|
||||
printf("\n1. enqueue");
|
||||
printf("\n2. dequeue");
|
||||
printf("\n3. display");
|
||||
printf("\n4. exit");
|
||||
printf("\nEnter your choice: ");
|
||||
scanf("%d", &choice);
|
||||
|
||||
switch(choice){
|
||||
case 1: printf("Enter item to be added: ");
|
||||
scanf("%d", &value);
|
||||
enqueue(value);
|
||||
break;
|
||||
case 2: value = dequeue();
|
||||
if(value!=-1)
|
||||
printf("popped %d", value);
|
||||
break;
|
||||
case 3: display();
|
||||
break;
|
||||
case 4: printf("exiting\n");
|
||||
exit(0);
|
||||
default:
|
||||
printf("enter valid choice bruh");
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user