Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // Node structure
- struct Node {
- int data;
- Node* next;
- };
- // Function to insert a node at the end of the list
- void push(Node** head_ref, int data) {
- Node* new_node = new Node();
- new_node->data = data;
- new_node->next = *head_ref;
- Node* temp = *head_ref;
- if (*head_ref != NULL) {
- while (temp->next != *head_ref)
- temp = temp->next;
- temp->next = new_node;
- }
- else
- new_node->next = new_node;
- *head_ref = new_node;
- }
- // Function to print the list
- void printList(Node* head) {
- Node* temp = head;
- if (head != NULL) {
- do {
- cout << temp->data << " ";
- temp = temp->next;
- } while (temp != head);
- }
- }
- int main() {
- Node* head = NULL;
- push(&head, 1);
- push(&head, 2);
- push(&head, 3);
- push(&head, 4);
- cout << "Circular Linked List: ";
- printList(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment