forked from sarthakd999/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedList.cpp
63 lines (55 loc) · 1021 Bytes
/
SinglyLinkedList.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
Node* createNode(int data)
{
Node *node= new Node();
node->data=data;
node->next=NULL;
return node;
}
Node* insert(Node *head, int data)
{
if(head==NULL)
head=createNode(data);
else
{
Node *curr=head;
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=createNode(data);
}
return head;
}
void TraversingLinkedList(Node *head)
{
if(head==NULL)
cout<<"Linked List is empty\n";
else
{
cout<<"The linked list is: \n";
Node *curr=head;
while(curr!=NULL)
{
cout<<curr->data<<" ";
curr=curr->next;
}
}
}
int main()
{
Node *head=NULL;
head=insert(head,9);
insert(head,5);
insert(head,68);
insert(head,80);
insert(head,1);
TraversingLinkedList(head);
return 0;
}