From d5927698d05cad84a8e5d36a8c49377d3c4ecea9 Mon Sep 17 00:00:00 2001 From: Haider Ali Date: Thu, 27 Oct 2022 21:44:07 +0500 Subject: [PATCH] Reverse printing of linked list --- .../PrintReverseUsingSinglyLinkedList (1).cpp | 259 ++++++++++++++++++ 1 file changed, 259 insertions(+) create mode 100644 C Program/PrintReverseUsingSinglyLinkedList (1).cpp diff --git a/C Program/PrintReverseUsingSinglyLinkedList (1).cpp b/C Program/PrintReverseUsingSinglyLinkedList (1).cpp new file mode 100644 index 000000000..9b2dc92f1 --- /dev/null +++ b/C Program/PrintReverseUsingSinglyLinkedList (1).cpp @@ -0,0 +1,259 @@ +#include +using namespace std; + +struct stu{ + + int id; + stu *next = NULL; + +}; + +stu *first = NULL; +stu *last = NULL; + +int opt; +int key; + +void insert_end(); +void insert_start(); +void display(); +void first_last_even(); +void first_last_odd(); +void palindrome(); +void reverse(); + +int main(){ + + do{ + + cout<<"\n0.Exit"; + cout<<"\n1.Insert at end"; + cout<<"\n2.Insert at start"; + cout<<"\n3.Display"; + cout<<"\n4.First & Last for Even nodes"; + cout<<"\n5.First & Last for Odd nodes"; + cout<<"\n6.Palindrome"; + cout<<"\n7.Reverse"; + + cout<<"\n\n\nEnter your choice: "; + cin>> opt; + + switch(opt){ + + case 1:{ + insert_end(); + break; + } + + case 2:{ + insert_start(); + break; + } + + case 3:{ + display(); + break; + } + + case 4:{ + first_last_even(); + break; + } + + case 5:{ + first_last_odd(); + break; + } + + case 6:{ + palindrome(); + break; + } + case 7:{ + reverse(); + break; + } + } + } + while(opt != 0); + +return 0; + +} + + +void insert_end(){ + + stu *current = new stu; + + cout<<"\n\nEnter the id: "; + cin>>current->id; + + if(first == NULL){ + cout<<"\n\nEmpty"; + first = last = current; + } + else{ + last->next = current; + last = current; + } + +} + +void insert_start(){ + + stu *current = new stu; + + cout<<"\n\nEnter the id: "; + cin>>current->id; + + if(first == NULL){ + cout<<"\nEmpty"; + first = last = current; + } + else{ + current->next = first; + first = current; + } + +} + +void display(){ + + cout<<"\n"; + + stu *current = new stu; + stu *p = first; + + if(first == NULL){ + cout<<"\n\nEmpty"; + first = last = current; + } + else{ + while(p != NULL){ + cout<< p->id<<" "; + p = p->next; + } + } + + cout<<"\n"; + +} + +void first_last_even(){ + + // for even number of nodes + + stu *temp = first; + stu *p = first; + stu *p1 = NULL; + + stu *k = last; + + int count = 0; + + while(temp != k){ + while(p != k){ + count++; + p1 = p; + p = p->next; + } + + cout<id<<" "; //1 + cout<id<<" "; //5 + + temp = temp->next; //2 + k = p1; //4 + + p = temp; + + } + + cout<<"\n\nThe no. of nodes are: "<next; + } + + cout<id<<" "; //1 + cout<id<<" "; //5 + + temp = temp->next; //2 + k = p1; //4 + + p = temp; + + } + cout<id; + + cout<<"\n\nThe no. of nodes are: "<next; + + } + if(temp->id == k->id){ + temp = temp->next; + k = p1; + p = temp; + cout<<"\n\ntrue"; + } + else{ + cout<<"\n\nfalse"; + break; + } +} + +cout<<"\n\n"; + +} + +void reverse(){ + + stu *p = first; + stu *p1 = NULL; + stu *p2 = first; + + //1 2 3 4 5 + while(p != p2){ + while(p != NULL){ + p1 = p; + p = p->next; + + } + cout<id<<" "; + p = p1; + } + + cout<<" "; +} \ No newline at end of file