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 :: nested conditional operator 
Cpp :: count c++ 
Cpp :: c++ define function pointer 
Cpp :: std::string substr 
Cpp :: Basic Makefile C++ 
Cpp :: c language all keywords in string 
Cpp :: phi function 
Cpp :: converting int to string c++ 
Cpp :: not c++ 
Cpp :: raspberry pi mount external hard drive 
Cpp :: binary tree 
Cpp :: deque 
Cpp :: 83. remove duplicates from sorted list solution in c++ 
Cpp :: char input in c++ 
Cpp :: codeforces problem 1030A solution 
Cpp :: recherche recursive le max dans une liste 
Cpp :: pointers in cpp 
Cpp :: 1822. Sign of the Product of an Array leetcode in c++ 
Cpp :: scope resulation operator :: in c++ 
Cpp :: strcmp in c++ header file 
Cpp :: print all substrings in c++ 
Cpp :: c++ stoi binary negative number string to decimal 
Cpp :: A Subtask Problem codechef solution in cpp 
Cpp :: how to use string in if else statement c++ 
Cpp :: case 1 or 2 c++ 
Cpp :: variadic template constructor matches better than copy constructor 
Cpp :: go to particular place in vector using iterator 
Cpp :: how are c++ references implemented 
Cpp :: converting a string to lowercase inbuld function in cpp 
Cpp :: what is require to run min max function on linux in cpp 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =