Stack Using Linked ListCode CrazeMar 19, 20231 min read#include <stdio.h>#include<stdlib.h>#include<math.h>#define size 5int top=-1;void push(void);void pop(void);void peek_top(void);void isEmpty(void);void isFull(void);void display(void);struct stack{ int data; struct node *next;};struct stack *head=0;void main() { int c,e; do{ switch(c) { case 1: push(); break; case 2: pop(); break; case 3: peek_top(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: printf("\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1 : Push\n2 : Pop\n3 : Peek\n4 : isEmpty\n5 : isFull\n6 : Display\n7 : Exit"); printf("\nEnter your choice : "); scanf("%d",&c); }while(c); }void push(){ struct stack *newnode,*temp; if(top<size) { newnode=(struct stack *)malloc(sizeof(struct stack)); printf("\nEnter data : "); scanf("%d",&newnode->data); newnode->next=0; top++; if(head==0) { head=temp=newnode; } else { temp=head; while(temp->next!=0) { temp=temp->next; } temp->next=newnode; } } else { printf("\nStack is full"); }}void display(){ struct stack *temp; temp=head; while(temp!=0) { printf("--%d",temp->data); temp=temp->next; }}void pop(){ struct stack *temp,*prevnode; printf("\n\n----Deletion From End----"); temp=head; top--; while(temp->next!=0) { prevnode=temp; temp=temp->next; } if(temp==head) { head=0; } else { prevnode->next=0; } free(temp); printf("\nTop : %d",top);}void peek_top(){ struct stack *temp; temp=head; while(temp->next!=0) { temp=temp->next; } printf("\nTop : %d",top); printf("\nPeek : %d",temp->data);}void isEmpty(){ if(top==-1) { printf("\nStack is empty"); } else { printf("\nStack is not empty"); }}void isFull(){ if(top==size-1) { printf("\nStack is full"); } else { printf("\nStack is not full"); }}
Stack Using ArrayCode CrazeMar 18, 20231 min read#include <stdio.h>#include<stdlib.h>#include<math.h>#define size 100int stack[size];int top=-1;void push(void);void pop(void);void peek_top(void);void isEmpty(void);void isFull(void);void display(void);void main() { int c,e; do{ switch(c) { case 1: push(); break; case 2: pop(); break; case 3: peek_top(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: printf("\n\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1 : Push\n2 : Pop\n3 : Peek\n4 : isEmpty\n5 : isFull\n6 : Display"); printf("\nEnter your choice : "); scanf("%d",&c); }while(c); }void push(){ int v; printf("\nEnter value : "); scanf("%d",&v); if(top==size-1) { printf("\nStack is full!!"); } else { top++; stack[top]=v; }}void display(){ int i; if(top==-1) { printf("\nStack is empty"); } else { for(i=0;i<=top;i++) { printf("--%d",stack[i]); } }}void pop(){ if(top==-1) { printf("\nStack is empty!!"); } else { top--; }}void peek_top(){ printf("\nTop : %d",top); printf("\nTop Value: %d",stack[top]);}void isEmpty(){ if(top==-1) { printf("\nStack is empty!!"); } else { printf("\nStack is not empty!!"); }}void isFull(){ if(top==size-1) { printf("\nStack is full!!"); } else { printf("\nStack is not full!!"); }}
Doubly Circular Linked ListCode CrazeMar 14, 20231 min readA doubly circular linked list is a type of linked list where each node has both a "next" and a "previous" pointer, allowing for traversal in both directions. Additionally, the last node in the list points back to the first node, forming a circle.#include <stdio.h>#include <stdlib.h>struct Node { int data; struct Node* next; struct Node* prev;};struct DoublyCircularLinkedList { struct Node* head; int count;};struct Node* create_node(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->next = NULL; node->prev = NULL; return node;}struct DoublyCircularLinkedList* create_list() { struct DoublyCircularLinkedList* list = (struct DoublyCircularLinkedList*)malloc(sizeof(struct DoublyCircularLinkedList)); list->head = NULL; list->count = 0; return list;}void append(struct DoublyCircularLinkedList* list, int data) { struct Node* node = create_node(data); if (list->head == NULL) { list->head = node; node->next = node; // make the node point to itself node->prev = node; } else { node->next = list->head; node->prev = list->head->prev; list->head->prev->next = node; list->head->prev = node; } list->count++;}int remove_node(struct DoublyCircularLinkedList* list, int data) { if (list->head == NULL) { return 0; } struct Node* current = list->head; do { if (current->data == data) { if (current == list->head) { list->head = current->next; } current->prev->next = current->next; current->next->prev = current->prev; free(current); list->count--; return 1; } current = current->next; } while (current != list->head); return 0;}void print_list(struct DoublyCircularLinkedList* list) { if (list->head == NULL) { return; } struct Node* current = list->head; do { printf("%d ", current->data); current = current->next; } while (current != list->head); printf("\n");}int main() { struct DoublyCircularLinkedList* list = create_list(); append(list, 1); append(list, 2); append(list, 3); print_list(list); // output: 1 2 3 remove_node(list, 2); print_list(list); // output: 1 3 return 0;}