diff --git a/Order Of Programs.md b/Order Of Programs.md index 9d4da17..78baa2f 100644 --- a/Order Of Programs.md +++ b/Order Of Programs.md @@ -29,4 +29,6 @@ |27 | BubbleSort.c | BubbleSort.exe | WAP to sort an array using bubble sort | |28 | LinearSearch.c | LinearSearch.exe | WAP to search an element in an array using Linear Search | |29 | BinarySearch.c | BinarySearch.exe | WAP to search an element in an array using Binary Search | -|30 | SelectionSort.c | SelectionSort.exe | WAP to sort an array using selection sort | \ No newline at end of file +|30 | SelectionSort.c | SelectionSort.exe | WAP to sort an array using selection sort | +|31 | StackUsingLinkedList.c | StackUsingLinkedList.exe | WAP to Implement a Stack using Linked List | +|32 | SinglyLinkedList.c | SinglyLinkedList.exe | WAP to implement singly linked lists | \ No newline at end of file diff --git a/README.md b/README.md index a644d07..115e38e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # C Programs For Everybody To Try! -_`Last Updated: April 28' 2021`_ +_`Last Updated: June 11' 2021`_ ## Introduction: diff --git a/SinglyLinkedList.c b/SinglyLinkedList.c new file mode 100644 index 0000000..496ef9f --- /dev/null +++ b/SinglyLinkedList.c @@ -0,0 +1,54 @@ +// WAP to implement singly linked lists + +#include +#include + +struct node { + int data; + struct node *next; +}; + +struct node *head = NULL; +struct node *current = NULL; + +//display the list +void printList() { + + struct node *ptr = head; + + printf("\n[head] =>"); + //start from the beginning + while(ptr != NULL) { + printf(" %d =>",ptr->data); + ptr = ptr->next; + } + + printf(" [null]\n"); +} + +//insert link at the first location +void insert(int data) { + //create a link + struct node *link = (struct node*) malloc(sizeof(struct node)); + + //link->key = key; + link->data = data; + + //point it to old first node + link->next = head; + + //point first to new first node + head = link; +} + +int main() { + insert(10); + insert(20); + insert(30); + insert(1); + insert(40); + insert(56); + + printList(); + return 0; +} diff --git a/SinglyLinkedList.exe b/SinglyLinkedList.exe new file mode 100644 index 0000000..b6524e7 Binary files /dev/null and b/SinglyLinkedList.exe differ diff --git a/StackUsingLinkedList.c b/StackUsingLinkedList.c new file mode 100644 index 0000000..ee5ba37 --- /dev/null +++ b/StackUsingLinkedList.c @@ -0,0 +1,182 @@ +// WAP to Implement a Stack using Linked List + +#include +#include + +struct node +{ + int info; + struct node *ptr; +}*top,*top1,*temp; + +int topelement(); +void push(int data); +void pop(); +void empty(); +void display(); +void destroy(); +void stack_count(); +void create(); + +int count = 0; + +void main() +{ + int no, ch, e; + + printf("\n 1 - Push"); + printf("\n 2 - Pop"); + printf("\n 3 - Top"); + printf("\n 4 - Empty"); + printf("\n 5 - Exit"); + printf("\n 6 - Dipslay"); + printf("\n 7 - Stack Count"); + printf("\n 8 - Destroy stack"); + + create(); + + while (1) + { + printf("\n Enter choice : "); + scanf("%d", &ch); + + switch (ch) + { + case 1: + printf("Enter data : "); + scanf("%d", &no); + push(no); + break; + case 2: + pop(); + break; + case 3: + if (top == NULL) + printf("No elements in stack"); + else + { + e = topelement(); + printf("\n Top element : %d", e); + } + break; + case 4: + empty(); + break; + case 5: + exit(0); + case 6: + display(); + break; + case 7: + stack_count(); + break; + case 8: + destroy(); + break; + default : + printf(" Wrong choice, Please enter correct choice "); + break; + } + } +} + +/* Create empty stack */ +void create() +{ + top = NULL; +} + +/* Count stack elements */ +void stack_count() +{ + printf("\n No. of elements in stack : %d", count); +} + +/* Push data into stack */ +void push(int data) +{ + if (top == NULL) + { + top =(struct node *)malloc(1*sizeof(struct node)); + top->ptr = NULL; + top->info = data; + } + else + { + temp =(struct node *)malloc(1*sizeof(struct node)); + temp->ptr = top; + temp->info = data; + top = temp; + } + count++; +} + +/* Display stack elements */ +void display() +{ + top1 = top; + + if (top1 == NULL) + { + printf("Stack is empty"); + return; + } + + while (top1 != NULL) + { + printf("%d ", top1->info); + top1 = top1->ptr; + } +} + +/* Pop Operation on stack */ +void pop() +{ + top1 = top; + + if (top1 == NULL) + { + printf("\n Error : Trying to pop from empty stack"); + return; + } + else + top1 = top1->ptr; + printf("\n Popped value : %d", top->info); + free(top); + top = top1; + count--; +} + +/* Return top element */ +int topelement() +{ + return(top->info); +} + +/* Check if stack is empty or not */ +void empty() +{ + if (top == NULL) + printf("\n Stack is empty"); + else + printf("\n Stack is not empty with %d elements", count); +} + +/* Destroy entire stack */ +void destroy() +{ + top1 = top; + + while (top1 != NULL) + { + top1 = top->ptr; + free(top); + top = top1; + top1 = top1->ptr; + } + free(top1); + top = NULL; + + printf("\n All stack elements destroyed"); + count = 0; +} \ No newline at end of file diff --git a/StackUsingLinkedList.exe b/StackUsingLinkedList.exe new file mode 100644 index 0000000..6633afa Binary files /dev/null and b/StackUsingLinkedList.exe differ