-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLL_prac.cpp
78 lines (67 loc) · 1.47 KB
/
LL_prac.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define f(i,a,b) for(i=a;i<b;i++)
#define fr(i,a,b) for(i=a;i>=b;i--)
#define sp "\t"
using namespace std;
int i;
struct node
{
int data;
struct node *next;
node()
{
data=0;
next=NULL;
}
};
struct node *head=NULL;
void create_LL(int n)
{
head = new node(); //dynamic allocation of new Node
cout<<"\nEnter Data for Node 1:\n"; //1st element
cin>>head->data;
if(head!=NULL) //rest of the elements
{
struct node *temp,*p=head;
f(i,0,n-1)
{
cout<<"\nEnter Data for Node "<<i+2<<":\n";
struct node *temp=new node();
cin>>temp->data;
p->next=temp;
p=temp;
}
free(temp);
}
}
void display_LL()
{
if(head==NULL)
{
cout<<"\nLinked List is Empty!\n";
}
else
{
struct node *temp=head;
i=0;
while(temp!=NULL)
{
cout<<"\n"<<i+1<<sp<<"->"<<sp<<temp->data;
i++;
temp=temp->next;
}
free(temp);
}
}
main()
{
int n;
cout<<"\nEnter the No. of Nodes you want to Insert:\n";
cin>>n;
if(n!=0)
{
create_LL(n);
}
display_LL();
}