Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ linked list

#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;
}
Comment

c++ linked list

#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
}
Comment

Linked list in c++

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;
Comment

cpp linked list

struct Node {
  int data;
  struct Node *next;
};
Comment

C++ linked list

/**
 * 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;
}
Comment

linkedlist in c++

/* 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;
Comment

linkedlist in c++

// 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;
  }
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ switch statement 
Cpp :: min element in vector c++ 
Cpp :: c++ #include 
Cpp :: SUMOFPROD2 solution 
Cpp :: cpp map insert 
Cpp :: stl c++ 
Cpp :: find substring in string c++ 
Cpp :: strring length in c++ 
Cpp :: c++ remove chars from string 
Cpp :: toupper c++ 
Cpp :: enum c++ 
Cpp :: string number to integer number C++ 
Cpp :: kmp algorithm c++ 
Cpp :: stl in c++ 
Cpp :: c++ find index of element in array 
Cpp :: rock paper scissor c++ 
Cpp :: C++ fibo 
Cpp :: C++ linked list iterator 
Cpp :: hierarchical inheritance in c++ employee 
Cpp :: c++ json parser euc-kr 
Cpp :: . Write a C++ program to calculate area of a Triangle 
Cpp :: how to make randomizer c++ 
Cpp :: C++ detaching threads 
Cpp :: Accessing C++ Array Elements 
Cpp :: cpp undefined reference to function 
Cpp :: dangling pointer 
Cpp :: c++ copy constructor 
Cpp :: files c++ 
Cpp :: c++ constructor 
Cpp :: Programming Languages codechef solution in c++ 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =