xxxxxxxxxx
// Linked list implementation in C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// Creating a node
class Node {
public:
int value;
Node* next;
};
int main() {
Node* head;
Node* one = NULL;
Node* two = NULL;
Node* three = NULL;
// allocate 3 nodes in the heap
one = new Node();
two = new Node();
three = new Node();
// Assign value values
one->value = 1;
two->value = 2;
three->value = 3;
// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;
// print the linked list value
head = one;
while (head != NULL) {
cout << head->value;
head = head->next;
}
}
xxxxxxxxxx
#include<bits/stdc++.h>
using namespace std;
struct Node
{
Node *next;
int data;
};
void print(Node *n)
{
while(n!=NULL)
{
cout<<n->data<<" ";
n=n->next;
}
}
int main()
{
Node* head,*second,*third = NULL;
head=new Node();
second=new Node();
third=new Node();
//First Node
head->data=1;
head->next=second;
//Second Node
second->data=2;
second->next=third;
//Third Node
third->data=3;
third->next=NULL;
print(head);
//@rahulsharmaah
}
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;
}
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
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
void trimLeftTrailingSpaces(string &input) {
input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) {
return !isspace(ch);
}));
}
void trimRightTrailingSpaces(string &input) {
input.erase(find_if(input.rbegin(), input.rend(), [](int ch) {
return !isspace(ch);
}).base(), input.end());
}
vector<int> stringToIntegerVector(string input) {
vector<int> output;
trimLeftTrailingSpaces(input);
trimRightTrailingSpaces(input);
input = input.substr(1, input.length() - 2);
stringstream ss;
ss.str(input);
string item;
char delim = ',';
while (getline(ss, item, delim)) {
output.push_back(stoi(item));
}
return output;
}
ListNode* stringToListNode(string input) {
// Generate list from the input
vector<int> list = stringToIntegerVector(input);
// Now convert that list into linked list
ListNode* dummyRoot = new ListNode(0);
ListNode* ptr = dummyRoot;
for(int item : list) {
ptr->next = new ListNode(item);
ptr = ptr->next;
}
ptr = dummyRoot->next;
delete dummyRoot;
return ptr;
}
void prettyPrintLinkedList(ListNode* node) {
while (node && node->next) {
cout << node->val << "->";
node = node->next;
}
if (node) {
cout << node->val << endl;
} else {
cout << "Empty LinkedList" << endl;
}
}
int main() {
string line;
while (getline(cin, line)) {
ListNode* head = stringToListNode(line);
prettyPrintLinkedList(head);
}
return 0;
}
xxxxxxxxxx
/* 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;