forked from philona/cppcodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlistimpl.cpp
More file actions
112 lines (102 loc) · 1.87 KB
/
linkedlistimpl.cpp
File metadata and controls
112 lines (102 loc) · 1.87 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
#include <bits/stdc++.h>
using namespace std;
#define null NULL
struct Node{
int data;
Node *link;
};
Node *head = null;
void insertBeg(int d){
Node *ptr = new Node();
ptr->data = d;
ptr->link = head;
head = ptr;
}
void insertend(int d){
Node *ptr = new Node();
ptr->data=d;
ptr->link=null;
if(head==null){
head = ptr;
}
else{
Node *temp = head;
while(temp!=null){
temp=temp->link;
}
temp->link = ptr;
}
}
void deleteBeg(){
if(head ==null){
cout<<"Empty list";
}
else{
Node *ptr = head;
head = ptr->link;
free(ptr);//del the node from memory
}
}
void deleteend(){
Node *ptr = head;
Node *prev = null;
if(head ==null){
cout<<"Empty list";
}
else if(head->link==null){
ptr=head;
head = null;
free(ptr);
}
else{
while(ptr!=null){
prev = ptr;
ptr = ptr-> link;
}
prev->link = null;
free(ptr);
}
}
int middleelement(){
Node *slow,*fast;
slow = head;
fast = head;
if(head==null){
cout<<"empty list";
}
else{
while(fast->link!=null || fast==null){
fast = fast->link->link;
slow = slow->link;
}
return slow->data;
}
}
void reverselist(){
Node *prev,*curr,*forw;
prev=null;
curr = head;
while(curr!=null){
forw = curr->link;
curr->link = prev;
prev=curr;
curr = forw;
}
head = prev;
}
void delnode(Node *ptr){
Node *temp;
temp=ptr->link;
ptr->data = temp->data;
ptr->link = temp->link;
free(temp);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Node*ptr = new Node();
ptr -> data =2;
ptr->link = null;
head = ptr;
}