-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglyinkedlist
155 lines (141 loc) · 2.83 KB
/
singlyinkedlist
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
/traversing a singly linkedlist//
// #include<iostream>
// using namespace std;
// struct node{
// int data;
// struct node *next;
// };
// void print(node *&head){
// node *temp=head;
// while (temp!=NULL)
// {
// cout<<temp->data<<endl;
// temp=temp->next;
// }
// }
// int main(){
// struct node *head;
// struct node *one=NULL;
// struct node *two=NULL;
// struct node *three=NULL;
// one =new node();
// two =new node();
// three=new node();
// one->data=1;
// two->data=2;
// three->data=3;
// one->next=two;
// two->next=three;
// three->next=NULL;
// head=one;
// print(head);
// }
//singly linked list operations//
// #include<iostream>
// using namespace std;
// class node{
// public:
// int data;
// node *next;
// };
// void insertatmiddle(node *prevnode,int position,int x){
// if (prevnode==NULL)
// {
// return;
// }
// else
// {
// node *newnode=new node();
// newnode->data=x;
// newnode->next=prevnode->next;
// prevnode->next=newnode;
// }
// }
// void insertatend(node *&head,int x){
// node *newnode=new node();
// newnode->data=x;
// newnode->next=NULL;
// node *temp=head;
// while (temp->next!=NULL)
// {
// temp=temp->next;
// }
// temp->next=newnode;
// }
// void insertatbegin(node *&head,int x){
// node *temp=new node();
// temp->data=x;
// temp->next=head;
// head=temp;
// }
// void deleteatfirst(node *&head){
// if (head==NULL)
// {
// return;
// }
// node *temp=head;
// head=head->next;
// delete temp;
// }
// void deleteatend(node *&head){
// if (head==NULL)
// {
// return;
// }
// node *temp=head;
// node *prevnode=NULL;
// while (temp->next!=NULL)
// {
// prevnode=temp;
// temp=temp->next;
// }
// delete temp;
// prevnode->next=NULL;
// }
// void deletefrommid(node *&head,int position){
// node *temp=head;
// node *prev=head;
// for(int i = 0; i < position; i++)
// {
// if(i == 0 && position == 1)
// {
// head = head->next;
// free(temp);
// }
// else
// {
// if (i == position - 1 && temp)
// {
// prev->next = temp->next;
// free(temp);
// }
// else
// {
// prev = temp;
// if(prev == NULL) // position was greater than number of nodes in the list
// break;
// temp = temp->next;
// }
// }
// }
// }
// void print(node *&head){
// node *temp=head;
// while (temp!=NULL)
// {
// cout<<temp->data<<endl;
// temp=temp->next;
// }
// }
// int main()
// {
// node *head=NULL;
// insertatbegin(head,5);
// insertatbegin(head,10);
// insertatbegin(head,15);
// insertatbegin(head,20);
// deletefrommid(head,2);
// // deleteatfirst(head);
// // deleteatend(head);
// print(head);
// }