-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataPractice.cpp
More file actions
111 lines (96 loc) · 2.19 KB
/
Copy pathDataPractice.cpp
File metadata and controls
111 lines (96 loc) · 2.19 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<iostream>
#include <stdlib.h>
using namespace std;
struct Node* creatLinkList(int arr[], int size);
int searchLinkList(struct Node *head, int value);
void reverseLinkList( struct Node *head);
struct Node
{
int data;
struct Node* next;
};
int main()
{
int n=0;
cout<<"Enter array size: ";
cin>>n;
int a[n];
cout<<"Enter the numbers: ";
for(int i=0; i<n; i++)
{
cin>>a[i];
}
struct Node *head = NULL;
head = creatLinkList(a, n);
//Loop for print
while (head != NULL)
{
cout<< head->data<<" -> ";
head = head->next;
}
cout <<"NULL"<<endl;
head = creatLinkList(a, n);
int x;
cout<<"Searching value: ";
cin>>x;
cout<<"Searching value index is: "<<searchLinkList(head, x)<<endl;
struct Node *newHead = head;
reverseLinkList(newHead);
return 0;
}
void reverseLinkList( struct Node *head)
{
struct Node *prev = NULL, *current = head, *next = NULL;
while(current != NULL)
{
//store the next node
next = current -> next;
//reverse the link/connection
current -> next = prev;
//propagate
prev = current;
current = next;
}
head = prev;
while (head != NULL)
{
cout<< head->data<<" -> ";
head = head->next;
}
cout <<"NULL"<<endl;
}
struct Node *creatLinkList(int arr[], int size)
{
struct Node *head = NULL, *temp = NULL, *current = NULL;
for (int i = 0; i<size; i++)
{
temp = (struct Node*) malloc(sizeof(struct Node));
temp->data = arr[i];
temp->next = NULL;
if (head == NULL)
{
head = temp;
current = temp;
}
else
{
current-> next = temp;
current = current-> next;
}
}
return head;
}
int searchLinkList(struct Node *head, int value)
{
int index = 1;
while(head != NULL)
{
if(head -> data == value)
{
return index;
}
index++;
head = head -> next;
}
return -1;
}