-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab7.cpp
More file actions
85 lines (71 loc) · 1.45 KB
/
Copy pathLab7.cpp
File metadata and controls
85 lines (71 loc) · 1.45 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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Node definition
struct Node {
string ssn;
string name;
Node * next;
};
// Append node to end of linked list
void append(Node * & head, string SSN, string Name)
{
Node *NewNode = new Node;
NewNode->ssn = SSN;
NewNode->name = Name;
if(head == NULL)
{
head = NewNode;
}
else
{
Node *temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = NewNode;
NewNode->next = NULL;
}
}
// Search linked list for given SSN and prints the location and name for that SSN
void search(Node * head, string SSN)
{
Node *temp = head;
int index = 0;
while(temp != NULL)
{
if((temp->ssn).compare(SSN) == 0)
{
cout<<"Found at location "<< index<<", belongs to "<<temp->name;
}
index++;
temp = temp->next;
}
}
int main()
{
Node *head = NULL;
fstream input;
input.open("sample.txt");
char i;
string SSN;
string First;
string Last;
string Full;
while(!input.eof())
{
input>>i;
input>>SSN;
input>>First;
input>>Last;
Full = First + " " + Last;
append(head,SSN, Full);
}
cout<<"Input a SSN: "<<endl;
string inputSSN;
cin>>inputSSN;
search(head, inputSSN);
return 0;
}