-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinklist.cpp
70 lines (63 loc) · 1.32 KB
/
linklist.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
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node *next;
public:
Node(int d){
data=d;
next=nullptr;
}
};
Node* convertArrayToLL(int arr[], int n){
Node *head= new Node(arr[0]);
Node *mover= head;
for(int i=1; i<n; i++){
Node *tmp= new Node(arr[i]);
mover->next = tmp;
mover = tmp;
}
return head;
}
Node* insertAtFirst(Node * head, int elem){
Node *tmp=head;
head = new Node(elem);
head->next=tmp;
return head;
}
Node* insertAtLast(Node * head, int elem){
Node *tmp=head;
while(tmp!=NULL){
if(tmp->next==nullptr){
Node *newNode = new Node(elem);
tmp->next=newNode;
break;
}
tmp=tmp->next;
}
return head;
}
void displayLL(Node *head){
Node *tmp = head;
while(tmp!=NULL){
cout<<tmp->data;
if(tmp->next!=nullptr)
cout<<"->";
tmp = tmp->next;
}
cout<<endl;
}
int main(){
cout<<"INTER SIZE OF LL: ";
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)cin>>arr[i];
Node *head = convertArrayToLL(arr,n);
displayLL(head);
head = insertAtFirst(head,10);
displayLL(head);
head = insertAtLast(head,50);
displayLL(head);
return 0;}