-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tushar goel
authored and
tushar goel
committed
Oct 1, 2018
1 parent
da009d7
commit b57d558
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Node{ | ||
public: | ||
int data; | ||
Node* next; | ||
Node(int d){ | ||
data = d; | ||
next = NULL; | ||
} | ||
}; | ||
|
||
Node* insertAtEnd(Node* head, int x, Node* &tail){ | ||
if (head == NULL){ | ||
head = new Node(x); | ||
tail = head; | ||
return head; | ||
} | ||
|
||
tail->next = new Node(x); | ||
tail = tail->next; | ||
return head; | ||
} | ||
|
||
Node* createLL(){ | ||
int x; | ||
Node* head = NULL; | ||
Node* tail = NULL; | ||
|
||
while(true){ | ||
cin >> x; | ||
if (x == -1) break; | ||
head = insertAtEnd(head, x, tail); | ||
} | ||
return head; | ||
} | ||
|
||
void printLL(Node* head){ | ||
Node* cur = head; | ||
while(cur != NULL){ | ||
cout << cur->data << "-->"; | ||
cur = cur->next; | ||
} | ||
cout << endl; | ||
} | ||
int main(){ | ||
//enter -1 for last number | ||
Node* head=createLL(); | ||
//To traverse use printLL | ||
printLL(head); | ||
} |
Binary file not shown.