Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Insert at Head in Circular Linked Lists #152

Merged
merged 1 commit into from
May 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*

This is a Circular Linked List program which gets a integer data from the user and
creates a node which is inserted at the beginning to the Linked List.

Since, it is a Circular Linked List, the last node would point to the head node.

*/

#include <iostream>

using namespace std;


/*

Node definition:
1. Integer Data
2. Pointer to next node.

*/

class cll_node {
public:
int data;
cll_node* next;
};

void createCLL(cll_node* &head) {

int choice;

cll_node* temp = head;

do {
int data;

cout << "Enter Data : ";
cin >> data;

cll_node* newNode = new cll_node();
newNode->data = data;
newNode->next = NULL;

if(head == NULL) {
head = newNode;
newNode->next = head;
temp = head;
} else {
newNode->next = head;
temp = head;
while(temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
head = newNode;
}

cout << "Do you want to continue? (1/0) : ";
cin >> choice;

} while(choice == 1);

}

void display(cll_node* head) {
cout << "The elements are : ";
if(head == NULL)
return;
cll_node* temp = head;

do {
cout << temp->data << " ";
temp = temp->next;
} while(temp != head);
cout << endl;
}

int main() {

cll_node* head = NULL;

createCLL(head);
display(head);

return 0;
}