0% found this document useful (0 votes)
13 views

Program of Linked List

Uploaded by

himanshjha459
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Program of Linked List

Uploaded by

himanshjha459
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Here's a C++ program for a circular linked list:

```

#include <iostream>

using namespace std;

// Node structure

struct Node {

int data;

Node* next;

};

// Circular Linked List class

class CircularLinkedList {

public:

Node* head;

// Constructor

CircularLinkedList() { head = NULL; }

// Insert at beginning

void insertAtBeginning(int data) {

Node* newNode = new Node();

newNode->data = data;

if (head == NULL) {

head = newNode;
newNode->next = head;

} else {

Node* temp = head;

while (temp->next != head) {

temp = temp->next;

temp->next = newNode;

newNode->next = head;

head = newNode;

// Insert at end

void insertAtEnd(int data) {

Node* newNode = new Node();

newNode->data = data;

if (head == NULL) {

head = newNode;

newNode->next = head;

} else {

Node* temp = head;

while (temp->next != head) {

temp = temp->next;

temp->next = newNode;

newNode->next = head;
}

// Delete at beginning

void deleteAtBeginning() {

if (head == NULL) {

cout << "List is empty." << endl;

} else if (head->next == head) {

delete head;

head = NULL;

} else {

Node* temp = head;

while (temp->next != head) {

temp = temp->next;

Node* newNode = head->next;

temp->next = newNode;

delete head;

head = newNode;

// Delete at end

void deleteAtEnd() {

if (head == NULL) {

cout << "List is empty." << endl;

} else if (head->next == head) {


delete head;

head = NULL;

} else {

Node* temp = head;

while (temp->next->next != head) {

temp = temp->next;

delete temp->next;

temp->next = head;

// Print list

void printList() {

if (head == NULL) {

cout << "List is empty." << endl;

} else {

Node* temp = head;

do {

cout << temp->data << " ";

temp = temp->next;

} while (temp != head);

cout << endl;

};
int main() {

CircularLinkedList cll;

cll.insertAtBeginning(1);

cll.insertAtBeginning(2);

cll.insertAtEnd(3);

cll.insertAtEnd(4);

cout << "Circular Linked List: ";

cll.printList();

cll.deleteAtBeginning();

cll.deleteAtEnd();

cout << "After deletion: ";

cll.printList();

return 0;

```

Output:

```

Circular Linked List: 2 1 3 4

After deletion: 1 3

```
This C++ program demonstrates the following operations on a circular linked
list:

- Insertion at the beginning (`insertAtBeginning`)

- Insertion at the end (`insertAtEnd`)

- Deletion at the beginning (`deleteAtBeginning`)

- Deletion at the end (`deleteAtEnd`)

- Printing the list (`printList`)

You can modify and extend this code to suit your specific requirements.

You might also like