0% found this document useful (0 votes)
54 views21 pages

Circular Link List Singly Anam Code

This C++ program implements a circular singly linked list with functions to create nodes, add nodes to the beginning or a specified position, delete nodes, search for a node, display the list, update a node, and sort the list. The main function contains a menu that allows the user to select which list operation to perform and prompts for any required inputs like elements or positions.

Uploaded by

anam wazir
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)
54 views21 pages

Circular Link List Singly Anam Code

This C++ program implements a circular singly linked list with functions to create nodes, add nodes to the beginning or a specified position, delete nodes, search for a node, display the list, update a node, and sort the list. The main function contains a menu that allows the user to select which list operation to perform and prompts for any required inputs like elements or positions.

Uploaded by

anam wazir
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/ 21

#include<iostream>

using namespace std;

struct node

int data;

node *next;

};

node *tail;

class circular_llist

public:

create_node(int value)

node *temp = new node;

temp->data = value;

if (tail == NULL)

tail = temp;

temp->next = tail;

else

temp->next = tail->next;
tail->next = temp;

tail = temp;

void add_begin(int value)

if (tail == NULL)

cout<<"First Create the list."<<endl;

return;

node *temp= new node;

temp->data = value;

temp->next = tail->next;

tail->next=temp;

}
/*

* Insertion of element at a particular place

*/

void add_position(int value, int pos)

if (tail == NULL)

cout<<"First Create the list."<<endl;

return;

node *s;

s = tail->next;

for (int i = 1;i <pos-1;i++)


{

s = s->next;

node *temp = new node;

temp->next = s->next;

temp->data = value;

s->next = temp;

/*Element inserted at the end*/

if (s == tail)

tail=temp;

/*
* Deletion of element from the list

*/

void delete_element(int value)

node *temp, *s;

s = tail->next;

/* If List has only one element*/

if (tail->next == tail && tail->data == value)

{ temp = tail;

tail = NULL;

return;

if (s->data == value) /*First Element Deletion*/

temp = s;

tail->next = s->next;

delete temp;
return;

while (s->next != tail)

/*Deletion of Element in between*/

if (s->next->data == value)

temp = s->next;

s->next = temp->next;

delete temp;

cout<<"Element "<<value;

cout<<" deleted from the list"<<endl;

return;

}
s = s->next;

/*Deletion of last element*/

if (s->next->data == value)

temp = s->next;

s->next = tail->next;

tail = s;

return;

cout<<"Element "<<value<<" not found in the list"<<endl;

/*

* Search element in the list

*/

void search_element(int value)

{
node *s;

int counter = 0;

s = tail->next;

while (s != tail)

counter++;

if (s->data == value)

cout<<"Element "<<value;

cout<<" found at position "<<counter<<endl;

return;

s = s->next;

}
if (s->data == value)

counter++;

cout<<"Element "<<value;

cout<<" found at position "<<counter<<endl;

return;

cout<<"Element "<<value<<" not found in the list"<<endl;

/*

* Display Circular Link List

*/

void display_list()

{
node *s;

if (tail == NULL)

cout<<"List is empty, nothing to display"<<endl;

return;

s = tail->next;

cout<<"Circular Link List: "<<endl;

while (s != tail)

cout<<s->data<<"->";

s = s->next;

cout<<s->data<<endl;
}

/*

* Update Circular Link List

*/

void update()

int value, pos, i;

if (tail == NULL)

cout<<"List is empty, nothing to update"<<endl;

return;

cout<<"Enter the node position to be updated: ";

cin>>pos;
cout<<"Enter the new value: ";

cin>>value;

struct node *s;

s = tail->next;

for (i = 0;i < pos - 1;i++)

if (s == tail)

cout<<"There are less than "<<pos<<" elements.";

cout<<endl;

return;

s = s->next;

}
s->data = value;

cout<<"Node Updated"<<endl;

/*

* Sort Circular Link List

*/

void sort()

node *s, *ptr;

int temp;

if (tail == NULL)

cout<<"List is empty, nothing to sort"<<endl;

return;
}

s = tail->next;

while (s != tail)

ptr = s->next;

while (ptr != tail->next)

if (ptr != tail->next)

if (s->data > ptr->data)

temp = s->data;

s->data = ptr->data;

ptr->data = temp;
}

else

break;

ptr = ptr->next;

s = s->next;

circular_llist()

tail = NULL;
}

};

int main()

int choice, element, position;

circular_llist cl;

int b=1;

while (b)

cout<<endl<<"---------------------------"<<endl;

cout<<endl<<"Circular singly linked list"<<endl;

cout<<endl<<"---------------------------"<<endl;

cout<<"1.Create Node"<<endl;
cout<<"2.Add at beginning"<<endl;

cout<<"3.Add after"<<endl;

cout<<"4.Delete"<<endl;

cout<<"5.Search"<<endl;

cout<<"6.Display"<<endl;

cout<<"7.Update"<<endl;

cout<<"8.Sort"<<endl;

cout<<"9.Quit"<<endl;

cout<<"Enter your choice : ";

cin>>choice;

switch(choice)

case 1:

cout<<"Enter the element: ";

cin>>element;
cl.create_node(element);

cout<<endl;

break;

case 2:

cout<<"Enter the element: ";

cin>>element;

cl.add_begin(element);

cout<<endl;

break;

case 3:

cout<<"Enter the element: ";

cin>>element;

cout<<"Insert position: ";

cin>>position;
cl.add_position(element, position);

cout<<endl;

break;

case 4:

if (tail == NULL)

cout<<"List is empty, nothing to delete"<<endl;

break;

cout<<"Enter the element for deletion: ";

cin>>element;

cl.delete_element(element);

cout<<endl;

break;

case 5:
if (tail == NULL)

cout<<"List Empty!! Can't search"<<endl;

break;

cout<<"Enter the element to be searched: ";

cin>>element;

cl.search_element(element);

cout<<endl;

break;

case 6:

cl.display_list();

break;

case 7:
cl.update();

break;

case 8:

cl.sort();

break;

case 9:

exit(1);

break;

default:

cout<<"Wrong choice"<<endl;

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy