forked from rituburman/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinglyLinkedList.cpp
More file actions
36 lines (34 loc) · 777 Bytes
/
singlyLinkedList.cpp
File metadata and controls
36 lines (34 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//only for positive numbers
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
void display() {
struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
int NumberofInput=0;
cout<<"Enter The Elements and enter a negative number to stop entering the element and start displaying them"<<endl;
while(NumberofInput>=0)
{
cin>>NumberofInput;
insert(NumberofInput);
}
cout<<"The linked list is: ";
display();
return 0;
}