xxxxxxxxxx
Singly Linked list is a type of linear data structure that stores data of same
type and data is stored in linear fashion. Singly linked list contains a node
and each node has two section one is data/value and second section contains
the memory address of next node . In this way nodes are connected to each other.
xxxxxxxxxx
/*
Linked List with Dynamic Memory Allocation in C++
*/
struct Node {
int data;
Node *next;
};
class LinkedList {
private:
Node *head = new Node;
public:
LinkedList() {
this->head->next = nullptr;
cout << endl << endl;
}
~LinkedList() {
Node *current = head;
cout << "Destructor: " << endl;
while (current != nullptr) {
Node *nextNode = current->next;
cout << " delete on " << current->data << endl;
delete current;
current = nextNode;
}
}
LinkedList &append(const int &data) {
if(this->head->next == nullptr) {
Node *node = new Node;
node->data = data;
this->head->next = node;
} else {
Node *newNode = new Node;
newNode->data = data;
newNode->next = nullptr;
Node *currentNode = this->head;
while (currentNode->next != nullptr) {
currentNode = currentNode->next;
}
currentNode->next = newNode;
}
return *this;
}
LinkedList &insert(const int &data) {
Node *newNode = new Node;
newNode->data = data;
newNode->next = this->head->next;
this->head->next = newNode;
return *this;
}
LinkedList &display() {
Node *currentNode = this->head;
while (currentNode->next != nullptr) {
currentNode = currentNode->next;
cout << "[" << currentNode->data << " | " << currentNode->next << ((currentNode->next != nullptr) ? "] ---> " : "] ");
}
cout << endl;
return *this;
}
bool search(const int &data) const {
Node *currentNode = this->head;
do {
if (currentNode->data == data) {
return true;
}
} while(currentNode = currentNode->next);
return false;
}
LinkedList &remove(const int &data) {
Node *currentNode = this->head;
do {
if (currentNode->next->data == data) {
Node *temp = currentNode->next;
currentNode->next = currentNode->next->next;
delete temp;
break;
}
} while((currentNode = currentNode->next) && (currentNode->next != nullptr));
return *this;
}
};
int main() {
/* Example usage: */
LinkedList list = LinkedList();
list.append(1).append(2).append(3).append(4);
list.display();
list.insert(5).display();
bool isFound = list.search(4);
cout << std::boolalpha << endl;
cout << isFound;
cout << endl << endl;
list.remove(3).display().remove(4).display().remove(5).display();
cout << endl;
/*
Example Output:
[1 | 0x7f9092f05c80] ---> [2 | 0x7f9092f05c90] ---> [3 | 0x7f9092f05ca0] ---> [4 | 0x0]
[5 | 0x7f9092f05c70] ---> [1 | 0x7f9092f05c80] ---> [2 | 0x7f9092f05c90] ---> [3 | 0x7f9092f05ca0] ---> [4 | 0x0]
true
[5 | 0x7f9092f05c70] ---> [1 | 0x7f9092f05c80] ---> [2 | 0x7f9092f05ca0] ---> [4 | 0x0]
[5 | 0x7f9092f05c70] ---> [1 | 0x7f9092f05c80] ---> [2 | 0x0]
[1 | 0x7f9092f05c80] ---> [2 | 0x0]
Destructor:
delete on 0
delete on 1
delete on 2
*/
return 0;
}
display singly in c++
xxxxxxxxxx
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
void display() {
struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The linked list is: ";
display();
return 0;
}
xxxxxxxxxx
struct node
{
int data;
struct node *next;
};
/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
/* Assign data values */
one->data = 1;
two->data = 2;
three->data=3;
/* Connect nodes */
one->next = two;
two->next = three;
three->next = NULL;
/* Save address of first node in head */
head = one;
xxxxxxxxxx
#include <iostream>
// Node class
class Node {
public:
int data;
Node* next;
};
// Linked list class
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = nullptr;
}
// Function to insert a new node at the beginning of the list
void insert(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
head = newNode;
}
// Function to display the elements of the list
void display() {
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
};
int main() {
LinkedList list;
// Insert elements into the list
list.insert(5);
list.insert(10);
list.insert(15);
list.insert(20);
// Display the list
std::cout << "Linked List: ";
list.display();
return 0;
}
xxxxxxxxxx
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
void setData();
void printData();
};
node* head =0;
void node::setData()
{
int choice=1;
head=new node();
cout <<"Enter data : ";
cin>>head->data;
head->next=NULL;
node* temp;
temp=head;
node* newnode;
cout<<"Do you want to link another list if yes press 1 else press 2 : ";
cin>>choice;
while(choice==1)
{
newnode=new node();
cout <<"\nEnter data : ";
cin>>newnode->data;
newnode->next=0;
temp->next=newnode;
temp=temp->next;
cout<<"\nDo you want to link another list if yes press 1 else press 2 : ";
cin>>choice;
}
}
void node::printData ()
{
cout<<"\n\nPrinting Linked List........\n\n";
node* temp;
temp=head;
while(temp!=0)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main(){
node obj;
obj.setData();
obj.printData();
}
xxxxxxxxxx
class Node
{
public:
int data;
Node *next;
Node()
{
this->data = 0;
this->next = NULL;
}
Node(int data)
{
this->data = data;
this->next = NULL;
}
~Node(){
this->next= NULL;
}
};
xxxxxxxxxx
#include<iostream>
#include<string>
using namespace std;
class Node
{
public:
int data; //Data to be stored in the node
Node* next; //Next pointer poiting the next node
Node(int val) //Constructor
{
data = val;
next = NULL; //It will always keep the next pointer of the last Node as NULL
}
};
void InsertAtHead(Node* &head,int val)
{
Node* n= new Node(val); //Creates a new Node next to the head, Remember that every old Node is a head to a new Node.
n->next=head; //This will make the new node to point towards the head.
head=n; //We assigned the new node n as the head.
}
void InsertAtTail(Node* &head,int val)// passing head by reference so when we replace it by temp and do changes in temp the head doesnt get effected by the changes done to temp
{
Node* n= new Node(val); //Creates a new Node next to the head, Remember that every old Node is a head to a new Node.
if(head==NULL ) //if head is NULL then we assign the already created Node n as head.
{
head=n;
return;
}
Node* temp=head; //Temp is a Node datatype pointer varaible which stores the address of the head.
while(temp->next!=NULL)
{
temp=temp->next; //It collects the next Node in the previous head.
}
//Now we are at the last Node
temp->next=n; //It links the new Node with the link list by giving the Node to the last Node next and the new Node is already consisting NULL at its next.
}
void display(Node* head) // passing by value because no more modification in print function
{
Node* temp=head; //Again assigned temp as pointer variable for head
while(temp!=NULL)
{
cout<<temp->data<<"->"; //Printing the data in temp
temp = temp->next; //Storing next Node in the temp
}
cout<<"NULL "<<endl;
}
int main()
{
Node* head = NULL; //Assigening Null to head
InsertAtTail(head,1);
InsertAtTail(head,2);
InsertAtTail(head,3);
display(head);
InsertAtHead(head,4);
display(head); //Displays the
}