Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Single Linked List
- //
- // Created by Harshit Jindal on 20/08/18.
- // Copyright © 2018 Harshit Jindal. All rights reserved.
- //
- #include <iostream>
- using namespace std;
- class Node
- {
- public:
- int data;
- Node *next;
- };
- void traverseList(Node *n)
- {
- while (n != NULL)
- {
- cout << n -> data << endl;
- n = n->next;
- }
- }
- int main(int argc, const char * argv[]) {
- Node* head = NULL;
- Node* second = NULL;
- Node* third = NULL;
- head = (Node*)malloc(sizeof(Node));
- second = (Node*)malloc(sizeof(Node));
- third = (Node*)malloc(sizeof(Node));
- head->data = 50; //assign data in first node
- head->next = second; // Link first node with
- second->data = 2;
- second->next = third;
- third->data = 23;
- third->next = NULL;
- traverseList(head);
- return 0;
- }
Add Comment
Please, Sign In to add comment