-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
158 lines (145 loc) · 3.92 KB
/
linkedlist.cpp
File metadata and controls
158 lines (145 loc) · 3.92 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *next;
};
void insert_at_beginning(node **pointertohead, int x){ //** for getting address, for derefrencing this, use *(** is pointer to pointer)
node *temp = new node();
temp -> data = x;
//temp ->next = NULL;
//if(head != NULL){temp -> next = head;}
temp -> next = *pointertohead;
*pointertohead = temp;
}
void insert_at_nthposition(node **pointertohead, int x, int n){
node *temp1 = new node();
temp1 -> data = x;
temp1 -> next = NULL;
if(n == 1){
temp1 -> next = *pointertohead;
*pointertohead = temp1;
return;
}
node *temp2 = *pointertohead;
for(int i = 0; i < n - 2; i++){ // to calculate address of n-1 th node
temp2 = temp2 ->next;
}
temp1 -> next = temp2 -> next;
temp2 -> next = temp1;
}
void delete_at_nthposition(node **pointertohead, int n){
node *temp1 = *pointertohead; // temp1 points to first node
if(n == 1){
*pointertohead = temp1 -> next;
delete temp1;
return;
}
for(int i = 0; i < n - 2; i++){
temp1 = temp1 -> next;
}
//temp1 points to (n-1)th node
node *temp2 = temp1 -> next; //nth node
temp1 -> next = temp2 -> next; //n-1th node connected to n+1th node
delete temp2;
}
void reverse_linkedlist(node **pointertohead){
node *current = *pointertohead;
node *prev, *nextnode;
prev = NULL;
while(current != NULL){
nextnode = current -> next;
current -> next = prev;
prev = current;
current = nextnode;
}
*pointertohead = prev;
}
void reverse_linkedlist_recursion(node **pointertohead, node *p){
if(p->next == NULL){
*pointertohead = p;
return;
}
reverse_linkedlist_recursion(pointertohead, p->next);
node *q = p -> next;
q -> next = p;
p -> next = NULL;
}
void print_all_elements(node* head){
// (when head is global)node *temp = head; //if we modify head, then we will lose the reference of the first node
cout << "List is: ";
while(head != NULL){
cout << head -> data << " ";
head = head -> next;
}
cout << endl;
}
void print_forward_recursively(node* head){
if(head == NULL){
return;
}
cout << head -> data;
print_forward_recursively(head -> next);
}
void print_reverse_recursively(node* head){
if(head == NULL){
return;
}
print_reverse_recursively(head -> next);
cout << head -> data;
}
int getNthFromLast(node *head, int n)
{
node *temp = head;
int len = 0;
while(temp-> next != NULL){
len++;
temp = temp->next;
}
if(len < n){
return -1;
}
temp = head;
for(int i=1; i < len-n+1;i++){
temp = temp->next;
}
return temp->data;
}
int getNthFromLastrecursive(node *head, int n)
{
int i = 0;
getNthFromLastrecursive(head->next, n);
i++;
if(i == n){
return head->data;
}
}
int length_of_linkedlist(node *head){
if(head == NULL){
return 0;
}
return 1 + length_of_linkedlist(head->next);
}
int main(int argc, char* argv[]){
node* head = NULL; //empty list
int n, x; //n is the number of elements you want to insert
cin >> n;
for(int i = 0; i < n; i++){
cout << "Enter the number";
cin >> x; //x is element
insert_at_beginning(&head, x);
print_all_elements(head);//prints elements after every number inserted
}
// insert_at_nthposition(&head, 5, 2);
// delete_at_nthposition(&head, 2);
// print_all_elements(head);
// reverse_linkedlist(&head);
// print_all_elements(head);
// print_forward_recursively(head);
// cout << endl;
// print_reverse_recursively(head);
//reverse_linkedlist_recursion(&head, head);
//print_all_elements(head);
cout << length_of_linkedlist(head);
return 0;
}