From d5d72049070eaf5a6cac4d25f7dbd3621d77c71f Mon Sep 17 00:00:00 2001 From: Rishi Chawla Date: Wed, 17 Oct 2018 02:11:41 +0530 Subject: [PATCH] Added Linked List and Stack using Array --- .../DoublyLinkedList/DoublyLinkedList.c | 152 ++++++++++++++++++ .../Linked List/SinglyLinkedList.c | 147 +++++++++++++++++ Data Structures/Stack/stack_using_array.c | 115 +++++++++++++ 3 files changed, 414 insertions(+) create mode 100644 Data Structures/DoublyLinkedList/DoublyLinkedList.c create mode 100644 Data Structures/Linked List/SinglyLinkedList.c create mode 100644 Data Structures/Stack/stack_using_array.c diff --git a/Data Structures/DoublyLinkedList/DoublyLinkedList.c b/Data Structures/DoublyLinkedList/DoublyLinkedList.c new file mode 100644 index 00000000..a62fbc31 --- /dev/null +++ b/Data Structures/DoublyLinkedList/DoublyLinkedList.c @@ -0,0 +1,152 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; + struct Node *prev; +}; + +struct Node *create_ll(int data) +{ + struct Node *head = malloc(sizeof(struct Node)); + head->data = data; + head->next = NULL; + head->prev = NULL; +} + +void insertNode_beg(struct Node *head, int data) +{ + struct Node *n = malloc(sizeof(struct Node)); + n->data = head->data; + n->next = head->next; + n->prev = head; + head->data = data; + head->next = n; +} + +void insertNode_bet(struct Node *head, int e, int data) +{ + struct Node *ptr = head, *n = malloc(sizeof(struct Node)); + if (ptr != NULL && ptr->data == e) + { + insertNode_beg(head, data); + return; + } + while (ptr != NULL && ptr->data != e) + { + ptr = ptr->next; + } + if (ptr == NULL) + { + printf("Element not found"); + return; + } + n->data = data; + n->next = ptr; + n->prev = ptr->prev; + (ptr->prev)->next = n; + ptr->prev = n; +} + +void insertNode_end(struct Node *head, int data) +{ + struct Node *n = malloc(sizeof(struct Node)); + struct Node *ptr = head; + while (ptr->next != NULL) + { + ptr = ptr->next; + } + ptr->next = n; + n->prev = ptr; + n->data = data; + n->next = NULL; +} + +void deleteNode(struct Node *head, int e) +{ + struct Node *ptr = head; + if (ptr != NULL && ptr->data == e) + { + ptr = ptr->next; + head->next = ptr->next; + head->data = ptr->data; + free(ptr); + printf("Element deleted.\n"); + return; + } + while (ptr != NULL && ptr->data != e) + { + ptr = ptr->next; + } + if (ptr == NULL) + { + printf("Element not found"); + return; + } + (ptr->prev)->next = ptr->next; + (ptr->next)->prev = ptr->prev; + free(ptr); + printf("Element deleted.\n"); +} + +void printList(struct Node *head) +{ + struct Node *ptr = head; + printf("Elements entered are: \n"); + while (ptr != NULL) + { + printf("%d\t", ptr->data); + ptr = ptr->next; + } + printf("\n"); +} + +int main() +{ + int n = 0, i = 0; + printf("Enter element to create list: "); + scanf("%d", &n); + struct Node *head = create_ll(n); + printf("Press 1 to enter element at the beginning.\nPress 2 to enter element before given element in the list.\nPress 3 to enter element at the end.\nPress 4 to delete element.\nPress 5 to print List\n"); + do + { + int j = 0; + scanf("%d", &j); + switch (j) + { + case 1: + printf("Enter element to be inserted at the beginning: "); + scanf("%d", &n); + insertNode_beg(head, n); + break; + case 2: + printf("Enter element in the list: "); + int e = 0; + scanf("%d", &e); + printf("Enter element to be inserted: "); + scanf("%d", &n); + insertNode_bet(head, e, n); + break; + case 3: + printf("Enter element to be inserted at the end: "); + scanf("%d", &n); + insertNode_end(head, n); + break; + case 4: + printf("Enter element to be deleted: "); + scanf("%d", &n); + deleteNode(head, n); + break; + case 5: + printList(head); + break; + default: + printf("Invalid input\n"); + } + printf("Press 1 to continue\n"); + scanf("%d", &i); + } while (i == 1); + return 0; +} \ No newline at end of file diff --git a/Data Structures/Linked List/SinglyLinkedList.c b/Data Structures/Linked List/SinglyLinkedList.c new file mode 100644 index 00000000..0eccfbe6 --- /dev/null +++ b/Data Structures/Linked List/SinglyLinkedList.c @@ -0,0 +1,147 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}; + +struct Node *create_ll(int data) +{ + struct Node *head = malloc(sizeof(struct Node)); + head->data = data; + head->next = NULL; +} + +void insertNode_beg(struct Node *head, int data) +{ + struct Node *n = malloc(sizeof(struct Node)); + n->data = head->data; + n->next = head->next; + head->data = data; + head->next = n; +} + +void insertNode_bet(struct Node *head, int e, int data) +{ + struct Node *ptr = head, *n = malloc(sizeof(struct Node)), *p; + if (ptr != NULL && ptr->data == e) + { + insertNode_beg(head, data); + return; + } + while (ptr != NULL && ptr->data != e) + { + p = ptr; + ptr = ptr->next; + } + if (ptr == NULL) + { + printf("Element not found"); + return; + } + n->data = data; + n->next = ptr; + p->next = n; +} + +void insertNode_end(struct Node *head, int data) +{ + struct Node *n = malloc(sizeof(struct Node)); + struct Node *ptr = head; + while (ptr->next != NULL) + { + ptr = ptr->next; + } + ptr->next = n; + n->data = data; + n->next = NULL; +} + +void deleteNode(struct Node *head, int e) +{ + struct Node *ptr = head, *p; + if (ptr != NULL && ptr->data == e) + { + ptr = ptr->next; + head->next = ptr->next; + head->data = ptr->data; + free(ptr); + printf("Element deleted.\n"); + return; + } + while (ptr != NULL && ptr->data != e) + { + p = ptr; + ptr = ptr->next; + } + if (ptr == NULL) + { + printf("Element not found"); + return; + } + p->next = ptr->next; + free(ptr); + printf("Element deleted.\n"); +} + +void printList(struct Node *head) +{ + struct Node *ptr = head; + printf("Elements entered are: \n"); + while (ptr != NULL) + { + printf("%d\t", ptr->data); + ptr = ptr->next; + } + printf("\n"); +} + +int main() +{ + int n = 0, i = 0; + printf("Enter element to create list: "); + scanf("%d", &n); + struct Node *head = create_ll(n); + printf("Press 1 to enter element at the beginning.\nPress 2 to enter element before given element in the list.\nPress 3 to enter element at the end.\nPress 4 to delete element.\nPress 5 to print List\n"); + do + { + int j = 0; + scanf("%d", &j); + switch (j) + { + case 1: + printf("Enter element to be inserted at the beginning: "); + scanf("%d", &n); + insertNode_beg(head, n); + break; + case 2: + printf("Enter element in the list: "); + int e = 0; + scanf("%d", &e); + printf("Enter element to be inserted: "); + scanf("%d", &n); + insertNode_bet(head, e, n); + break; + case 3: + printf("Enter element to be inserted at the end: "); + scanf("%d", &n); + insertNode_end(head, n); + break; + case 4: + printf("Enter element to be deleted: "); + scanf("%d", &n); + deleteNode(head, n); + break; + case 5: + printList(head); + break; + default: + printf("Invalid input\n"); + } + printf("Press 1 to continue\n"); + scanf("%d", &i); + } while (i == 1); + return 0; +} \ No newline at end of file diff --git a/Data Structures/Stack/stack_using_array.c b/Data Structures/Stack/stack_using_array.c new file mode 100644 index 00000000..0a715667 --- /dev/null +++ b/Data Structures/Stack/stack_using_array.c @@ -0,0 +1,115 @@ +#include +#include + +#define MAX 500 + +struct Stack +{ + int top; + int* a; +}; + +typedef struct Stack stack; + +stack* createStack() +{ + stack *s = malloc(sizeof(stack)); + s->top = -1; + s->a = malloc(MAX * sizeof(int)); +} + +int isEmpty(stack* s) +{ + if (s->top == -1) + { + return 1; + } + else + { + return 0; + } +} + +int isFull(stack *s) +{ + if (s->top == MAX-1) + { + return 1; + } + else + { + return 0; + } +} + +void push(stack *s, int n) +{ + if (isFull(s)) + { + printf("Stack overflow\n"); + } + else + { + s->a[++s->top] = n; + printf("%d pushed to stack\n", n); + } +} + +void pop (stack *s) +{ + if (isEmpty(s)) + { + printf("Underflow\n"); + } + else + { + printf("%d popped from the stack", s->a[s->top]); + s->top--; + } +} + +int peek(stack *s) +{ + if (isEmpty(s)) + { + printf("Stack is empty\n"); + } + else + { + return s->a[s->top]; + } +} + +void display(stack* s) +{ + int i = 0; + for (i = s->top; i >= 0; i--) + { + printf("%d\t", s->a[i]); + } + printf("\n"); +} + +int main() +{ + stack* s = createStack(); + int c = 0; + printf("Enter 1 to push elements to stack.\nEnter 2 to pop elements from the stack.\nEnter 3 to peek topmost element in stack.\nEnter 4 to display content of the stack.\n"); + scanf("%d", &c); + do + { + switch (c) + { + case 1: printf ("Enter element to be pushed: "); + int n = 0; + scanf("%d", &n); + push(s, n); break; + case 2: pop(s); break; + case 3: printf("%d\n", peek(s)); break; + case 4: display(s); break; + default: printf("Invalid input"); + } + scanf("%d", &c); + } while (c != -1); + return 0; +} \ No newline at end of file