From 11511cf2cd4862028e18de5899947d33b420bc89 Mon Sep 17 00:00:00 2001 From: sukaran <32487359+sukaran@users.noreply.github.com> Date: Thu, 10 Oct 2019 22:35:40 +0530 Subject: [PATCH] Add files via upload --- A3_Q6_101753016.cpp | 76 ++++++++ linked_list.cpp | 423 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 499 insertions(+) create mode 100644 A3_Q6_101753016.cpp create mode 100644 linked_list.cpp diff --git a/A3_Q6_101753016.cpp b/A3_Q6_101753016.cpp new file mode 100644 index 0000000..a8297aa --- /dev/null +++ b/A3_Q6_101753016.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + +//Declare Node + // TO be NOTED + //LINKED LIST POSITIONS(indexing) ARE + //taken to be starting from '0' +struct Node{ + int num; + Node *next; +}; + +//Declare starting node +struct Node *head=NULL; + +//Insert node at start +void insertNode(int n){ + struct Node *newNode=new Node; + newNode->num=n; + newNode->next=head; + head=newNode; +} + +//display all nodes +void display(){ + if(head==NULL){ + cout<<"List is empty!"<num<<"->"; + temp=temp->next; + } + cout<num) + { + cout<next; + }} + } + +int main(){ + + //display(); + insertNode(60); + insertNode(50); + insertNode(40); + insertNode(35); + insertNode(30); + insertNode(20); + insertNode(10); + insertNode(5); + display(); + search(50); + // order of calling is in descending w.r.t the key values, so that they APPEAR in the order as given + //display(); + return 0; +} diff --git a/linked_list.cpp b/linked_list.cpp new file mode 100644 index 0000000..845150c --- /dev/null +++ b/linked_list.cpp @@ -0,0 +1,423 @@ +/* + * C++ Program to Implement Singly Linked List + */ +#include +#include +#include +using namespace std; +/* + * Node Declaration + */ +struct node +{ + int info; + struct node *next; +}*start; + +/* + * Class Declaration + */ +class single_llist +{ + public: + node* create_node(int); + void insert_begin(); + void insert_pos(); + void insert_last(); + void delete_pos(); + void sort(); + void search(); + void update(); + void reverse(); + void display(); + single_llist() + { + start = NULL; + } +}; + +/* + * Main :contains menu + */ +main() +{ + int choice, nodes, element, position, i; + single_llist sl; + start = NULL; + while (1) + { + cout<>choice; + switch(choice) + { + case 1: + cout<<"Inserting Node at Beginning: "<info = value; + temp->next = NULL; + return temp; + } +} + +/* + * Inserting element in beginning + */ +void single_llist::insert_begin() +{ + int value; + cout<<"Enter the value to be inserted: "; + cin>>value; + struct node *temp, *p; + temp = create_node(value); + if (start == NULL) + { + start = temp; + start->next = NULL; + } + else + { + p = start; + start = temp; + start->next = p; + } + cout<<"Element Inserted at beginning"<>value; + struct node *temp, *s; + temp = create_node(value); + s = start; + while (s->next != NULL) + { + s = s->next; + } + temp->next = NULL; + s->next = temp; + cout<<"Element Inserted at last"<>value; + struct node *temp, *s, *ptr; + temp = create_node(value); + cout<<"Enter the postion at which node to be inserted: "; + cin>>pos; + int i; + s = start; + while (s != NULL) + { + s = s->next; + counter++; + } + if (pos == 1) + { + if (start == NULL) + { + start = temp; + start->next = NULL; + } + else + { + ptr = start; + start = temp; + start->next = ptr; + } + } + else if (pos > 1 && pos <= counter) + { + s = start; + for (i = 1; i < pos; i++) + { + ptr = s; + s = s->next; + } + ptr->next = temp; + temp->next = s; + } + else + { + cout<<"Positon out of range"<next;s !=NULL;s = s->next) + { + if (ptr->info > s->info) + { + value = ptr->info; + ptr->info = s->info; + s->info = value; + } + } + ptr = ptr->next; + } +} + +/* + * Delete element at a given position + */ +void single_llist::delete_pos() +{ + int pos, i, counter = 0; + if (start == NULL) + { + cout<<"List is empty"<>pos; + struct node *s, *ptr; + s = start; + if (pos == 1) + { + start = s->next; + } + else + { + while (s != NULL) + { + s = s->next; + counter++; + } + if (pos > 0 && pos <= counter) + { + s = start; + for (i = 1;i < pos;i++) + { + ptr = s; + s = s->next; + } + ptr->next = s->next; + } + else + { + cout<<"Position out of range"<>pos; + cout<<"Enter the new value: "; + cin>>value; + struct node *s, *ptr; + s = start; + if (pos == 1) + { + start->info = value; + } + else + { + for (i = 0;i < pos - 1;i++) + { + if (s == NULL) + { + cout<<"There are less than "<next; + } + s->info = value; + } + cout<<"Node Updated"<>value; + struct node *s; + s = start; + while (s != NULL) + { + pos++; + if (s->info == value) + { + flag = true; + cout<<"Element "<next; + } + if (!flag) + cout<<"Element "<next == NULL) + { + return; + } + ptr1 = start; + ptr2 = ptr1->next; + ptr3 = ptr2->next; + ptr1->next = NULL; + ptr2->next = ptr1; + while (ptr3 != NULL) + { + ptr1 = ptr2; + ptr2 = ptr3; + ptr3 = ptr3->next; + ptr2->next = ptr1; + } + start = ptr2; +} + +/* + * Display Elements of a link list + */ +void single_llist::display() +{ + struct node *temp; + if (start == NULL) + { + cout<<"The List is Empty"<info<<"->"; + temp = temp->next; + } + cout<<"NULL"<