Data Structures Assignment 02 PDF

Summary

This document contains C code for implementing various operations on circular and doubly circular linked lists. It includes functions for insertion, deletion, and display. Also includes a menu-driven program for the implementation.

Full Transcript

7.Write a menu driven program to implement following operationson the circular linked list. (a) Insert a node at the front of the linked list. (b) Insert a node at theend of the linked list. (c) Insert a node such that linked list is inascending order.(according to info. Field) (d) Delete a first no...

7.Write a menu driven program to implement following operationson the circular linked list. (a) Insert a node at the front of the linked list. (b) Insert a node at theend of the linked list. (c) Insert a node such that linked list is inascending order.(according to info. Field) (d) Delete a first node of the linked list. (e) Delete a node before specified position. (f) Deleteanode after specified position. \#include \ \#include \ struct node { int data; struct node \*next; } \*head = NULL, \*tail = NULL; void display() { if (head == NULL) { printf(\"\\nlist is empty.\\n\"); return; } struct node \*temp = head; printf(\"\\ncircular linked list: \"); do { printf(\"%d -\> \", temp-\>data); temp = temp-\>next; } while (temp != head); printf(\"%d (head)\\n\", head-\>data); } void insert\_front() { int val; printf(\"\\nenter value to insert at front: \"); scanf(\"%d\", &val); struct node \*new\_node = (struct node\*)malloc(sizeof(struct node)); new\_node-\>data = val; if (head == NULL) { head = tail = new\_node; new\_node-\>next = head; } else { new\_node-\>next = head; head = new\_node; tail-\>next = head; } } void insert\_end() { int val; printf(\"\\nenter value to insert at end: \"); scanf(\"%d\", &val); struct node \*new\_node = (struct node\*)malloc(sizeof(struct node)); new\_node-\>data = val; if (head == NULL) { head = tail = new\_node; new\_node-\>next = head; } else { tail-\>next = new\_node; tail = new\_node; tail-\>next = head; } } void insert\_ascending() { int val; printf(\"\\nenter value to insert in ascending order: \"); scanf(\"%d\", &val); struct node \*new\_node = (struct node\*)malloc(sizeof(struct node)); new\_node-\>data = val; if (head == NULL \|\| head-\>data \>= val) { new\_node-\>next = head; head = new\_node; if (tail == NULL) tail = new\_node; tail-\>next = head; } else { struct node \*temp = head; while (temp-\>next != head && temp-\>next-\>data \< val) { temp = temp-\>next; } new\_node-\>next = temp-\>next; temp-\>next = new\_node; if (temp == tail) tail = new\_node; } } void delete\_first() { if (head == NULL) { printf(\"\\nlist is empty, nothing to delete.\\n\"); return; } struct node \*temp = head; if (head == tail) { head = tail = NULL; } else { head = head-\>next; tail-\>next = head; } free(temp); printf(\"\\nfirst node deleted.\\n\"); } void delete\_before(int pos) { if (head == NULL \|\| pos \next; int count = 2; if (pos == 2) { delete\_first(); return; } while (current != head && count \< pos - 1) { prev = current; current = current-\>next; count++; } if (current == head) { printf(\"\\nposition out of range.\\n\"); return; } prev-\>next = current-\>next; free(current); printf(\"\\nnode before position %d deleted.\\n\", pos); } void delete\_after(int pos) { if (head == NULL) { printf(\"\\nlist is empty.\\n\"); return; } struct node \*temp = head; int count = 0; while (count \< pos && temp-\>next != head) { temp = temp-\>next; count++; } if (temp-\>next == head \|\| temp == tail) { printf(\"\\nno node to delete after position %d.\\n\", pos); return; } struct node \*to\_delete = temp-\>next; temp-\>next = to\_delete-\>next; if (to\_delete == tail) { tail = temp; } free(to\_delete); printf(\"\\nnode after position %d deleted.\\n\", pos); } int main() { int choice, pos; while (1) { printf(\"\\nmenu:\"); printf(\"\\n1. insert at front\"); printf(\"\\n2. insert at end\"); printf(\"\\n3. insert in ascending order\"); printf(\"\\n4. display\"); printf(\"\\n5. delete first node\"); printf(\"\\n6. delete before position\"); printf(\"\\n7. delete after position\"); printf(\"\\n8. exit\"); printf(\"\\nenter your choice: \"); scanf(\"%d\", &choice); switch (choice) { case 1: insert\_front(); break; case 2: insert\_end(); break; case 3: insert\_ascending(); break; case 4: display(); break; case 5: delete\_first(); break; case 6: printf(\"\\nenter the position: \"); scanf(\"%d\", &pos); delete\_before(pos); break; case 7: printf(\"\\nenter the position: \"); scanf(\"%d\", &pos); delete\_after(pos); break; case 8: exit(0); default: printf(\"\\ninvalid choice.\\n\"); } printf(\"SAWAN PARMAR\\n0801IT231117\"); } return 0; } ![](media/image2.png) 8.Write a menu driven program to implement following operationson the doubly circular linked list. (a) Insert a node at the front of the linked list. (b) Insert a node at theend of the linked list. (c) Insert a node such that linked list is inascending order.(according to info. Field) (d) Delete a first node of the linked list. (e) Delete a node before specified position. (f) Deleteanode after specified position. \#include \ \#include \ struct node { int data; struct node \*prev; struct node \*next; } \*head = NULL, \*tail = NULL; void display() { if (head == NULL) { printf(\"\\nlist is empty.\\n\"); return; } struct node \*temp = head; printf(\"\\ndoubly circular linked list: \"); do { printf(\"%d \ \", temp-\>data); temp = temp-\>next; } while (temp != head); printf(\"%d (head)\\n\", head-\>data); } void insert\_front() { int val; printf(\"\\nenter value to insert at front: \"); scanf(\"%d\", &val); struct node \*new\_node = (struct node\*)malloc(sizeof(struct node)); new\_node-\>data = val; if (head == NULL) { head = tail = new\_node; new\_node-\>next = new\_node-\>prev = head; } else { new\_node-\>next = head; new\_node-\>prev = tail; head-\>prev = new\_node; head = new\_node; tail-\>next = head; } } void insert\_end() { int val; printf(\"\\nenter value to insert at end: \"); scanf(\"%d\", &val); struct node \*new\_node = (struct node\*)malloc(sizeof(struct node)); new\_node-\>data = val; if (head == NULL) { head = tail = new\_node; new\_node-\>next = new\_node-\>prev = head; } else { new\_node-\>prev = tail; new\_node-\>next = head; tail-\>next = new\_node; head-\>prev = new\_node; tail = new\_node; } } void insert\_ascending() { int val; printf(\"\\nenter value to insert in ascending order: \"); scanf(\"%d\", &val); struct node \*new\_node = (struct node\*)malloc(sizeof(struct node)); new\_node-\>data = val; if (head == NULL \|\| head-\>data \>= val) { insert\_front(); } else { struct node \*temp = head; while (temp-\>next != head && temp-\>next-\>data \< val) { temp = temp-\>next; } new\_node-\>next = temp-\>next; new\_node-\>prev = temp; temp-\>next-\>prev = new\_node; temp-\>next = new\_node; if (temp == tail) { tail = new\_node; } } } void delete\_first() { if (head == NULL) { printf(\"\\nlist is empty, nothing to delete.\\n\"); return; } struct node \*temp = head; if (head == tail) { head = tail = NULL; } else { head = head-\>next; head-\>prev = tail; tail-\>next = head; } free(temp); printf(\"\\nfirst node deleted.\\n\"); } void delete\_before(int pos) { if (head == NULL \|\| pos \next != head) { temp = temp-\>next; count++; } if (temp == head \|\| temp-\>prev == tail) { printf(\"\\nno node to delete before position %d.\\n\", pos); return; } struct node \*to\_delete = temp; temp-\>prev-\>next = temp-\>next; temp-\>next-\>prev = temp-\>prev; if (temp == head) { head = temp-\>next; } free(to\_delete); printf(\"\\nnode before position %d deleted.\\n\", pos); } void delete\_after(int pos) { if (head == NULL) { printf(\"\\nlist is empty.\\n\"); return; } struct node \*temp = head; int count = 1; while (count \< pos && temp-\>next != head) { temp = temp-\>next; count++; } if (temp-\>next == head) { printf(\"\\nno node to delete after position %d.\\n\", pos); return; } struct node \*to\_delete = temp-\>next; temp-\>next = to\_delete-\>next; to\_delete-\>next-\>prev = temp; if (to\_delete == tail) { tail = temp; } free(to\_delete); printf(\"\\nnode after position %d deleted.\\n\", pos); } int main() { int choice, pos; while (1) { printf(\"\\nmenu:\"); printf(\"\\n1. insert at front\"); printf(\"\\n2. insert at end\"); printf(\"\\n3. insert in ascending order\"); printf(\"\\n4. display\"); printf(\"\\n5. delete first node\"); printf(\"\\n6. delete before position\"); printf(\"\\n7. delete after position\"); printf(\"\\n8. exit\"); printf(\"\\nenter your choice: \"); scanf(\"%d\", &choice); switch (choice) { case 1: insert\_front(); break; case 2: insert\_end(); break; case 3: insert\_ascending(); break; case 4: display(); break; case 5: delete\_first(); break; case 6: printf(\"\\nenter the position: \"); scanf(\"%d\", &pos); delete\_before(pos); break; case 7: printf(\"\\nenter the position: \"); scanf(\"%d\", &pos); delete\_after(pos); break; case 8: exit(0); default: printf(\"\\ninvalid choice.\\n\"); } printf(\"SAWAN PARMAR\\n0801IT231117\");} return 0; }![](media/image4.png) ![](media/image6.png) 9.Write a programme to merge two different linked list. \#include \ \#include \ struct node{ int data; struct node \*next; }\*head1=NULL,\*tail1=NULL,\*head2=NULL,\*tail2=NULL; void display(){ struct node \*temp; temp=(struct node\*)malloc(sizeof(struct node)); temp=head1; while(temp!=NULL){ printf(\"%d-\>\",temp-\>data); temp=temp-\>next; } printf(\"null\\n\"); } void merge(){ tail1-\>next=head2; tail1=tail2; } int main(){ printf(\"Enter no of element in list1:\"); int n; int i=0; scanf(\"%d\",&n); do{ struct node \*newnode; newnode=(struct node\*)malloc(sizeof(struct node)); printf(\"enter element:\"); scanf(\"%d\",&newnode-\>data); if(head1==NULL){ head1=newnode; tail1=newnode; } else{ tail1-\>next=newnode; tail1=newnode; tail1-\>next=NULL; } i++; }while(i\data); if(head2==NULL){ head2=newnode; tail2=newnode; } else{ tail2-\>next=newnode; tail2=newnode; tail2-\>next=NULL; } j++; }while(j\data); temp = temp-\>next; } printf(\"null\\n\"); } void printmid() { if (head == NULL) { printf(\"list is empty\\n\"); return; } struct node \*fastptr = head; struct node \*slowptr = head; while (fastptr != NULL && fastptr-\>next != NULL) { fastptr = fastptr-\>next-\>next; slowptr = slowptr-\>next; } printf(\"mid element: %d\\n\", slowptr-\>data); } int main() { printf(\"Enter number of elements in list: \"); int n; scanf(\"%d\", &n); for (int i = 0; i \< n; i++) { struct node \*newnode = (struct node \*)malloc(sizeof(struct node)); printf(\"Enter element: \"); scanf(\"%d\", &newnode-\>data); newnode-\>next = NULL; if (head == NULL) { head = newnode; tail = newnode; } else { tail-\>next = newnode; tail = newnode; } } display(); printmid(); printf(\"SAWAN PARMAR\\n\"); printf(\"0801IT231117\"); return 0; }![](media/image8.png) 11\. Write a programme to reverse a linkedlist. \#include \ \#include \ struct node { int data; struct node \*next; } \*head = NULL, \*tail = NULL; void display() { struct node \*temp = head; while (temp != NULL) { printf(\"%d -\> \", temp-\>data); temp = temp-\>next; } printf(\"null\\n\"); } void reverse() { struct node \*prev = NULL; struct node \*curr = head; struct node \*nex = NULL; while (curr != NULL) { nex = curr-\>next; curr-\>next = prev; prev = curr; curr = nex; } head = prev; } int main() { printf(\"enter number of elements in list: \"); int n; scanf(\"%d\", &n); for (int i = 0; i \< n; i++) { struct node \*newnode = (struct node \*)malloc(sizeof(struct node)); printf(\"enter element: \"); scanf(\"%d\", &newnode-\>data); newnode-\>next = NULL; if (head == NULL) { head = newnode; tail = newnode; } else { tail-\>next = newnode; tail = newnode; } } display(); reverse(); printf(\"reversed linked list\\n\"); display(); printf(\"\\nSAWAN PARMAR\\n\"); printf(\"0801IT231117\\n\"); return 0; } 12\. Write a programme for stack using array to implement push, pop and display operation. \#include \ \#include \ \#define max 100 typedef struct { int data\[max\]; int top; } stack; void init(stack \*s) { s-\>top = -1; } void push(stack \*s, int val) { if (s-\>top == max - 1) { printf(\"stack overflow\\n\"); return; } s-\>data\[++(s-\>top)\] = val; } int pop(stack \*s) { if (s-\>top == -1) { printf(\"stack underflow\\n\"); return -1; } return s-\>data\[(s-\>top)\--\]; } void display(stack \*s) { if (s-\>top == -1) { printf(\"stack is empty\\n\"); return; } printf(\"stack elements: \"); for (int i = 0; i \top; i++) { printf(\"%d \", s-\>data\[i\]); } printf(\"\\n\"); } int main() { stack s; init(&s); int choice, val; do { printf(\"1\>push\\n2\>pop\\n3\>display\\n4\>exit\\n\"); printf(\"enter choice: \"); scanf(\"%d\", &choice); if (choice == 1) { printf(\"enter value: \"); scanf(\"%d\", &val); push(&s, val); } else if (choice == 2) { val = pop(&s); if (val != -1) { printf(\"popped value: %d\\n\", val); } } else if (choice == 3) { display(&s); } else if (choice != 4) { printf(\"invalid choice\\n\"); } } while (choice != 4); return 0; } ![](media/image10.png)![](media/image12.png) 13\. Write a programme for stack using linkedlist to implement push, pop and peek operation. \#include \ \#include \ struct node { int data; struct node \*next; } \*head = NULL, \*tail = NULL; void push(int val) { struct node \*newnode = (struct node \*)malloc(sizeof(struct node)); newnode-\>data = val; newnode-\>next = head; head = newnode; } int pop() { if (head == NULL) { printf(\"stack underflow\\n\"); return -1; } struct node \*temp = head; int val = temp-\>data; head = head-\>next; free(temp); return val; } int peek() { if (head == NULL) { printf(\"stack is empty\\n\"); return -1; } return head-\>data; } int main() { int n = 0; printf(\"1\>push\\n2\>pop\\n3\>peek\\n\"); while (n == 0) { int v; printf(\"enter no: \"); scanf(\"%d\", &v); if (v == 1) { int h; printf(\"val: \"); scanf(\"%d\", &h); push(h); } else if (v == 2) { int res = pop(); if (res != -1) { printf(\"popped: %d\\n\", res); } } else if (v == 3) { int res = peek(); if (res != -1) { printf(\"top: %d\\n\", res); } } else { n = 1; } } printf(\"SAWAN PARMAR\\n\"); printf(\"\\n0801IT231117\\n\"); return 0; } ![](media/image14.png)

Use Quizgecko on...
Browser
Browser