-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLink_List_Search.cpp
93 lines (68 loc) · 1.16 KB
/
Link_List_Search.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
92
93
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node * createNode()
{
struct node *newnode;
newnode=(struct node *) malloc(sizeof(struct node));
cin>>newnode->data;
newnode->next=0;
return newnode;
}
struct node * createLinklist(int n)
{
struct node *head=0,*temp,*newnode;
cout<<"input new data"<<endl;
for (int i = 0; i <n; ++i)
{
newnode=createNode();
if(head==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
return head;
}
int searchdata(struct node *head,int p)
{
struct node *temp,*newnode,*Nextnode;
int i=1;
temp=head;
while(p!=temp->data)
{
temp=temp->next;
i++;
}
return i;
}
void display(struct node *head)
{
struct node *temp;
temp=head;
while(temp!=0)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main()
{
cout<<" input number of data you want insert "<<endl;
int n,x,p;
cin>>n;
struct node *head;
head=createLinklist(n);
cout<<"input the number which you want search"<<endl;
cin>>x;
p=searchdata(head,x);
cout<<"Position is : "<<p<<endl;
}