-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedListImplementation.cpp
91 lines (84 loc) · 1.54 KB
/
linkedListImplementation.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
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node* link;
};
Node* head;
void print(Node* head)
{
Node* temp = head;
while(temp!=NULL)
{
cout<<temp->data;
temp = temp->link;
}
}
void recursiveReversePrint(Node* p)
{
if(p->link == NULL) return;
cout<<p->data;
recursiveReversePrint(p->link);
}
void reverseLinkedList()
{
Node* current, *next, *prev;
current = head;
prev = NULL;
while(current != NULL)
{
next = current->link;
current->link = prev;
prev = current;
current = next;
}
head = prev;
}
void deleteNodeAtNthPosition(int pos)
{
Node* temp = head;
if(pos == 1)
{
head = temp->link;
free(temp);
return;
}
for(int i=0; i<pos-2; i++)
temp = temp->link;
Node* temp1 = temp->link;
temp->link = temp1->link;
free(temp1);
}
void insertAtNthPosition(int data, int pos)
{
Node* temp2 = new Node();
temp2->data = data;
temp2->link = NULL;
Node* temp = head;
if(pos == 1)
{
temp2->link = head;
head = temp2;
return;
}
for(int i = 0; i<pos-2; i++)
temp = temp->link;
temp2->link = temp->link;
temp->link = temp2;
}
int main()
{
head = NULL;
insertNode(2);
insertNode(4);
insertNode(6);
insertAtNthPosition(9,1);
print(head);
//deleteNodeAtNthPosition(2);
//reverseLinkedList();
//cout<<"\n";
//recursiveReversePrint(head);
//print(head);
return 0;
}