diff --git a/Algorithms/CircularSLL.cpp b/Algorithms/CircularSLL.cpp new file mode 100644 index 0000000..2649924 --- /dev/null +++ b/Algorithms/CircularSLL.cpp @@ -0,0 +1,248 @@ +#include +using namespace std; + +template +struct Node +{ + T data; + Node *next; +}; + +template +class LinkedList +{ +public: + Node *head; + LinkedList() + { + head = NULL; + } + void insertionHead() + { + Node *temp = new Node; + cout << "\nenter the value to be inserted " << endl; + T n; + cin >> n; + temp->data = n; + if (head == NULL) + { + head = temp; + head->next = head; + } + else + { + Node *loop = head; + while (loop->next != head) + loop = loop->next; + temp->next = head; + head = temp; + loop->next = head; + } + } + void insertionTail() + { + Node *temp = new Node; + cout << "\nenter the value to be inserted " << endl; + T n; + cin >> n; + temp->data = n; + if (head == NULL) + { + head = temp; + head->next = head; + } + else + { + Node *p = head; + while (p->next != head) + p = p->next; + temp->next = head; + p->next = temp; + } + } + void deletionHead() + { + if (head != NULL) + { + if (head->next == head) + { + Node *delnode = head; + cout << "element deleted " << head->data; + head = NULL; + delete delnode; + } + else + { + Node *delnode = head; + cout << "element deleted " << head->data; + Node *loop = head; + while (loop->next != head) + loop = loop->next; + head = head->next; + loop->next = head; + delnode->next = NULL; + delete delnode; + } + } + else + { + cout << "List empty"; + } + } + void deletionTail() + { + if (head != NULL) + { + if (head->next == head) + { + Node *delnode = head; + cout << "element deleted " << head->data; + head = NULL; + delete delnode; + } + else + { + Node *temp = head; + while (temp->next->next != head) + temp = temp->next; + cout << "element deleted " << temp->next->data; + Node *delnode = temp->next; + delete delnode; + temp->next = head; + } + } + else + { + cout << "List empty"; + } + } + void search() + { + if (head == NULL) + cout << "\nList empty"; + else + { + cout << "\nenter the value to be searched " << endl; + T x; + cin >> x; + Node *temp = head; + bool found = false; + do + { + if (temp->data == x) + found = true; + temp = temp->next; + } while (temp != head); + + if (found) + cout << "\nElement found in linked list"; + else + cout << "\nElement not found in linked list"; + } + } + void reverse() + { + if (head == NULL) + { + cout << "\nList is empty."; + return; + } + else if (head->next == head) + { + cout << "\nList only has one element"; + return; + } + + Node *prev = NULL; + Node *temp = head; + Node *next; + do + { + next = temp->next; + temp->next = prev; + prev = temp; + temp = next; + } while (temp != head); + head->next = prev; + head = prev; + cout << "\nThe list has been reversed"; + } + void display() + { + Node *temp = head; + while (temp != NULL) + { + cout << temp->data << "->"; + if (temp->next == head) + break; + temp = temp->next; + } + cout << "!!!"; + } + void alt_display() + { + Node *temp = head; + while (temp != NULL) + { + cout << temp->data << "->"; + if (temp->next == head || temp->next->next == head) + break; + temp = temp->next->next; + } + cout << "!!!"; + } +}; + +int main() +{ + LinkedList l; + int ch; + char cont; + do + { + cout << "\nEnter your choice among following " << endl; + cout << "1.Insert an element at beginning" << endl; + cout << "2.Insert an element at end" << endl; + cout << "3.Delete an element from beginning" << endl; + cout << "4.Delete an element from end" << endl; + cout << "5.Search the list" << endl; + cout << "6.Reverse the list" << endl; + cout << "7.Display alternate elements of Linked List" << endl; + cout << "8.Display Linked List\n" + << endl; + cin >> ch; + switch (ch) + { + int val; + case 1: + l.insertionHead(); + break; + case 2: + l.insertionTail(); + break; + case 3: + l.deletionHead(); + break; + case 4: + l.deletionTail(); + break; + case 5: + l.search(); + break; + case 6: + l.reverse(); + break; + case 7: + l.alt_display(); + break; + case 8: + l.display(); + break; + default: + cout << "\nInvalid choice " << endl; + } + cout << "\n\nContinue?(y/n)" << endl; + cin >> cont; + } while (cont == 'y' || cont == 'Y'); + return 0; +} \ No newline at end of file diff --git a/Algorithms/DoublyLinkedList.c b/Algorithms/DoublyLinkedList.c new file mode 100644 index 0000000..a35a357 --- /dev/null +++ b/Algorithms/DoublyLinkedList.c @@ -0,0 +1,72 @@ +/* CREATING A SIMPLE DOUBLY LINKED LIST */ +/* DBLINK.C */ + +# include +# include + +struct Double +{ + int info; + struct Double *next; + struct Double *previous; +}; + +int num ; +struct Double start; +void Doubly_link_list (struct Double *); +void display (struct Double *); + +/* Function creates a simple doubly linked list */ + +void Doubly_link_list(struct Double *node) +{ + char ch; + start.next = NULL; /* Empty list */ + start.previous = NULL; + node = &start; /* Point to the start of the list */ + num = 0; + printf("\n Input choice n for break: "); + ch = getchar(); + + while( ch != 'n') + { + node->next = (struct Double *) malloc(sizeof(struct Double)); + node->next->previous = node; + node = node->next; + printf("\n Input the values of the node : %d: ", (num+1)); + scanf("%d", &node->info); + node->next = NULL; + fflush(stdin); + printf("\n Input choice n for break: "); + ch = getchar(); + num ++; + } + printf("\n Total nodes = %d", num); +} + +/* Display the list */ + +void display (struct Double *node) +{ + node = start.next; + + do { + printf("\n 0x%x", node); + printf(" %d", node->info); + node = node->next; + } while (node->next); /* Show value of last node only one time */ + + do { + printf("\n 0x%x", node ); + printf(" %d", node->info); + node = node->previous; + } while (node->previous); +} + +int main() +{ + struct Double *node = (struct Double *) malloc(sizeof(struct Double)); + Doubly_link_list(node); + printf("\n Created doubly linked list is as follows\n"); + display(node); +} \ No newline at end of file diff --git a/Algorithms/linkedlist-deletion.cpp b/Algorithms/linkedlist-deletion.cpp new file mode 100644 index 0000000..4a13e61 --- /dev/null +++ b/Algorithms/linkedlist-deletion.cpp @@ -0,0 +1,80 @@ +// A C++ program to show all type of insertion on Linked List +#include +using namespace std; + + +// defining a linked list node +struct Node { + int data; + Node* next; +}; + + +//initializing the head node with NULL +Node* head = NULL; + +//insertion at the begining of the linked list +void insert(int new_data) +{ + Node* new_node = new Node(); + new_node->data = new_data; + new_node->next = head; + head = new_node; +} + +//deletion at the beginning +void delete_first() +{ + Node* temp=new Node(); + temp=head; + head=head->next; + delete temp; +} + + +//deletion at a given position +void delete_position(int pos) +{ + Node* current=new Node(); + Node* previous=new Node(); + current=head; + for(int i=1;inext; + } + previous->next=current->next; +} + + +//printing all the values of the linked list +void display() +{ + + Node* ptr; + ptr = head; + while (ptr != NULL) { + cout<< ptr->data <<" "; + ptr = ptr->next; + } +} + + +//main code +int main() +{ + insert(69); + insert(36); + insert(13); + insert(47); + insert(54); + + delete_first(); + delete_position(3); + + cout<<"The linked list is: "; + display(); + return 0; +} + + diff --git a/Algorithms/linkedlist-insertion.cpp b/Algorithms/linkedlist-insertion.cpp new file mode 100644 index 0000000..6c92206 --- /dev/null +++ b/Algorithms/linkedlist-insertion.cpp @@ -0,0 +1,85 @@ +// A C++ program to show all type of insertion on Linked List +#include +using namespace std; + + +// defining a linked list node +struct Node { + int data; + Node* next; +}; + + +//initializing the head node with NULL +Node* head = NULL; + +//inserting at the begining of the linked list +void insert(int new_data) +{ + Node* new_node = new Node(); + new_node->data = new_data; + new_node->next = head; + head = new_node; +} + + +//inserting at a given position +void insert_atposition(int pos, int value) +{ + Node* pre=new Node(); + Node* cur=new Node(); + Node* temp=new Node(); + cur=head; + for(int i=1;inext; + } + temp->data=value; + pre->next=temp; + temp->next=cur; +} +// insert at tail +void insertAtTail(int data){ + Node*temp = head; + while(temp->next!= NULL){ + temp = temp->next; + } + Node*n = new Node(); + temp ->next = n; + n->data = data; + n->next = NULL; +} + +//printing all the values of the linked list +void display() +{ + + Node* ptr; + ptr = head; + while (ptr != NULL) { + cout<< ptr->data <<" "; + ptr = ptr->next; + } +} + + +//main code +int main() +{ + insert(69); + insert(36); + insert(13); + insert(47); + insert(54); + insert_atposition(2, 100); + // trying insertAtTail method which is recently added + insertAtTail(200); + insertAtTail(300); + + cout<<"The linked list is: "; + display(); + return 0; +} + +