bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_2 / eduincs / pgrm_028.cpp

pgrm_028.cpp (1844B)


      1 /* Circluar linked list */
      2 
      3 #include <iostream>
      4 using namespace std;
      5 
      6 struct Node {
      7     int info;
      8     Node *next = this;
      9 };
     10 
     11 class circularlist {
     12 private:
     13     Node *cl = nullptr;
     14 
     15 public:
     16     void insertEnd();
     17     void display();
     18     ~circularlist();
     19 };
     20 
     21 typedef class circularlist cl;
     22 
     23 void circularlist::insertEnd() {
     24     Node *newNode = new Node;
     25     cout << "Enter info: ";
     26     cin >> newNode->info;
     27 
     28     if (cl == nullptr) {
     29         cl = newNode;
     30         cl->next = cl;
     31     } else {
     32         newNode->next = cl->next;
     33         cl->next = newNode;
     34         cl = newNode;
     35     }
     36     cout << endl << "Inserted " << newNode->info << " at the end\n";
     37 }
     38 
     39 void circularlist::display() {
     40     if (cl == nullptr) {
     41         cout << "List is empty\n";
     42         return;
     43     }
     44 
     45     Node *t = cl->next;
     46     cout << "Circular List: ";
     47     do {
     48         cout << t->info << " -> ";
     49         t = t->next;
     50     } while (t != cl->next);
     51     cout << "(head)\n";
     52 }
     53 
     54 circularlist::~circularlist() {
     55     if (cl == nullptr) return;
     56 
     57     Node *t = cl->next;
     58     Node *p;
     59     while (t != cl) {
     60         p = t->next;
     61         delete t;
     62         t = p;
     63     }
     64     delete cl;
     65     cl = nullptr;
     66 }
     67 
     68 int main() {
     69     circularlist list;
     70     int choice;
     71 
     72     while (true) {
     73         cout << "\n--- Circular Linked List Menu ---\n";
     74         cout << "1. Insert at End\n";
     75         cout << "2. Display List\n";
     76         cout << "3. Exit\n";
     77         cout << "Enter your choice: ";
     78         cin >> choice;
     79 
     80         switch (choice) {
     81             case 1:
     82                 list.insertEnd();
     83                 break;
     84             case 2:
     85                 list.display();
     86                 break;
     87             case 3:
     88                 cout << "Exiting program...\n";
     89                 return 0;
     90             default:
     91                 cout << "Invalid choice! Please try again.\n";
     92         }
     93     }
     94 
     95     return 0;
     96 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror