Skip to content

Commit 2eb1f8c

Browse files
committed
Linked List Implementation
1 parent a2c6718 commit 2eb1f8c

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Linked List/linked_list.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
struct Node {
5+
int data;
6+
struct Node* next;
7+
Node(int data)
8+
{
9+
this->data = data;
10+
next = NULL;
11+
}
12+
};
13+
14+
struct LinkedList {
15+
Node* head;
16+
LinkedList()
17+
{
18+
head = NULL;
19+
}
20+
void push(int data)
21+
{
22+
Node* temp = new Node(data);
23+
temp->next = head;
24+
head = temp;
25+
}
26+
void display()
27+
{
28+
struct Node* temp = head;
29+
while (temp != NULL)
30+
{
31+
cout << temp->data << " ";
32+
temp = temp->next;
33+
}
34+
cout<<endl;
35+
}
36+
};
37+
38+
int main()
39+
{
40+
int n, data;
41+
LinkedList ll;
42+
cout<<"Enter the number of elements to be added in Linked List: ";
43+
cin>>n;
44+
for(int i=0;i<n;i++)
45+
{
46+
cout<<"Enter the data item "<<i+1<<": ";
47+
cin>>data;
48+
ll.push(data);
49+
}
50+
cout << "The Linked List is:\n";
51+
ll.display();
52+
return 0;
53+
}

0 commit comments

Comments
 (0)