#include <iostream>
using namespace std;
struct node
{
int data; // data of the node
node *next; // pointer to the next node
};
node *head = NULL; // head is the pointer to the first node
void insertNode(int value)
{
node *newNode, *last; // newNode is the new node to be inserted and last is the last node of the list
newNode = new node; // newNode is creating a new node
newNode->data = value; // newNode's data is set to the value passed to the function
newNode->next = NULL; // newNode's next is set to NULL
if (head == NULL) // if the head (the pointer to the first node)is NULL
{
head = newNode; // head is set to newNode
newNode->next = NULL; // newNode's next is set to NULL to indicate that it is the last node
}
else
{
last = head; // last is set to the head to start the search from the first node
while (last->next != NULL) // while last's next is not NULL
{
last->next; // last is set to the next node
}
last->next = newNode; // last's next is set to newNode by pointing it to the new node
newNode->next = NULL; // newNode's next is set to NULL to indicate that it is the last node
}
}
int main()
{
insertNode(5); // inserting the first node
insertNode(6); // inserting the second node
insertNode(7); // inserting the third node
insertNode(8); // inserting the fourth node
insertNode(9); // inserting the fifth node
insertNode(10); // inserting the last node
return 0;
}
#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
}
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;
struct Node {
int data;
struct Node *next;
};
/**
* 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;
}
/* 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;
// 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;
}
}
Code Example |
---|
Cpp :: c++ math |
Cpp :: c++ array pointer |
Cpp :: calculator in cpp |
Cpp :: variables in c++ |
Cpp :: resize vector c++ |
Cpp :: double array c++ |
Cpp :: oop in cpp |
Cpp :: linux c++ sigint handler |
Cpp :: how to take full sentence in c++ |
Cpp :: uparam(ref) |
Cpp :: disallowcopy c++ |
Cpp :: c++ sizeof |
Cpp :: c++ convert int to cstring |
Cpp :: how to reset linerenderer unity |
Cpp :: set iterator |
Cpp :: ue4 int to enum c++ |
Cpp :: c++ cin |
Cpp :: bit++ codeforces in c++ |
Cpp :: how to extract a folder using python |
Cpp :: casting to a double in c++ |
Cpp :: __builtin_popcount long long |
Cpp :: minimum characters to make string palindrome |
Cpp :: copy vector c++ |
Cpp :: converting int to string c++ |
Cpp :: print all subsequences |
Cpp :: data type c++ |
Cpp :: bit masking tricks |
Cpp :: function for reversing an array c++ stl |
Cpp :: object inside class c++ |
Cpp :: c shortest path dijkstra |