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++ visual studio 
Cpp :: reverse in vector c++ 
Cpp :: substring function in c++ 
Cpp :: problem category codechef solution in c++ 
Cpp :: C++ programming code to remove all characters from string except alphabets 
Cpp :: Programming Languages codechef solution in c++ 
Cpp :: online converter c++ to c 
Cpp :: C++ CHEAT SHEAT 
Cpp :: progress indicator raytracer 
Cpp :: how to get characters through their ascii value in c++ 
Cpp :: lap trinh file explorer c++ 
Cpp :: even number program in c++ using for loop stack overflow 
Cpp :: uint16_t does not name a type 
Cpp :: Road sign detection and recognition by OpenCV in c 
Cpp :: how to pronounce beaucoup 
Cpp :: triangle angle sum 
Cpp :: sfml thread multi argument function 
Cpp :: what c++ library is arccos in 
Cpp :: argument to number C++ 
Cpp :: private access specifier in c++ program 
Cpp :: nothrow new in cpp 
Cpp :: cpp pointer to two dimensional array 
Cpp :: c++ system() from variable 
Cpp :: hamming c++ 
Cpp :: C++ References 
Cpp :: MPI_PUT 
Cpp :: friend class in c++ 
Cpp :: To toggle (flip the status of) the k-th item of the set 
Cpp :: Reading package lists... Done Building dependency tree Reading state information... Done mysql-server is already the newest version (5.7.36-0ubuntu0.18.04.1). 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 
Cpp :: declare static table filled cpp 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =