From fa9541974426f0a62e87788c4d9ab2a548ee9b5a Mon Sep 17 00:00:00 2001 From: RSsingla Date: Wed, 24 Oct 2018 21:21:00 +0530 Subject: [PATCH] Single link list in c++ --- singly linked list.txt | 423 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 423 insertions(+) create mode 100644 singly linked list.txt diff --git a/singly linked list.txt b/singly linked list.txt new file mode 100644 index 0000000..15bdf40 --- /dev/null +++ b/singly linked list.txt @@ -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"<