forked from sarthakd999/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linearsearchlinkedlist.cpp
71 lines (66 loc) · 1.19 KB
/
linearsearchlinkedlist.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
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
void reverse(Node *head)
{
Node *current = head;
Node *prev = NULL, *next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
void insert(Node **head, int val)
{
Node *node = new Node();
node->data = val;
node->next = (*head);
(*head) = node;
}
void printList(Node *node)
{
while (node != NULL)
{
cout << node->data << "->";
node = node->next;
}
}
int main()
{
//FAST;
Node *head = NULL;
int a;
while (a)
{
cout << "Enter 1 to insert a node\nEnter 0 to exit" << endl;
cin >> a;
int p;
switch (a)
{
case 1:
{
cout << "Insert data into Linked List\n";
cin >> p;
insert(&head, p);
printList(head);
break;
}
}
cout << "Enter 3 to reverse linked list\n";
int n;
cin >> n;
if (n == 3)
reverse(head);
printList(head);
}
return 0;
}