Double Linked Lists

So Im trying to make a double linked list that I can search for the name and also search for people only below a certain age, but I dont know how to do that. So far Ive only got the one but Im having trouble.

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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
#include <string>
using namespace std;

 typedef string Elem;				// list element type
  class DNode {					// doubly linked list node
  private:
    Elem elem;					// node element value
	int age;
    DNode* prev;				// previous node in list
    DNode* next;				// next node in list
    friend class DLinkedList;			// allow DLinkedList access
  };

class DLinkedList {				// doubly linked list
  public:
    DLinkedList();				// constructor
    ~DLinkedList();				// destructor
    bool empty() const;				// is list empty?
    const Elem& front() const;			// get front element
    const Elem& back() const;			// get back element
    void addFront(const Elem& name);		// add to front of list
    void addBack(const Elem& name);		// add to back of list
    void removeFront();				// remove from front
    void removeBack();				// remove from back
	void displayViaAge(string age);
	void displayViaName(const Elem& name);
	void removeName(const Elem& name);
  private:					// local type definitions
    DNode* header;				// list sentinels
    DNode* trailer;
  protected:					// local utilities
    void add(DNode* v, const Elem& name);		// insert new node before v
    void remove(DNode* v);			// remove node v
  };

  DLinkedList::DLinkedList() {			// constructor
    header = new DNode;				// create sentinels
    trailer = new DNode;
    header->next = trailer;			// have them point to each other
    trailer->prev = header;
  }

    DLinkedList::~DLinkedList() {			// destructor
    while (!empty()) removeFront();		// remove all but sentinels
    delete header;				// remove the sentinels
    delete trailer;
  }
        						// insert new node before v
 void DLinkedList::add(DNode* v, const Elem& name) {
    DNode* u = new DNode;   
	u->elem = name;      // create a new node and set name
    u->next = v;                 // make v the successor of u
    u->prev = v->prev;           // set u's predecessor to v's current predecessor
    u->prev->next = u;           // make u the successor of v's predecessor
    v->prev = u;                 // finally make u the predecessor of v
}
 /*
  void DLinkedList::removeName(const Elem& name) {
    DNode* u = new DNode;   u->elem = name;      // create a new node and set name
    u->next = v;                 // make v the successor of u
    u->prev = v->prev;           // set u's predecessor to v's current predecessor
    u->prev->next = u;           // make u the successor of v's predecessor
    v->prev = u;                 // finally make u the predecessor of v
}
*/
  void DLinkedList::addFront(const Elem& name)	// add to front of list
    { add(header->next, name); }
  
  void DLinkedList::addBack(const Elem& name)	// add to back of list
    { add(trailer, name); }

   void DLinkedList::remove(DNode* v) {		// remove node v
    DNode* u = v->prev;				// predecessor
    DNode* w = v->next;				// successor
    u->next = w;				// unlink v from list
    w->prev = u;
    delete v;
  }

  void DLinkedList::removeFront()		// remove from font
    { remove(header->next); }
  
  void DLinkedList::removeBack()		// remove from back
    { remove(trailer->prev); }

 
	 bool DLinkedList::empty() const		// is list empty?
    { return (header->next == trailer); }

  const Elem& DLinkedList::front() const	// get front element
    { return header->next->elem; }

  const Elem& DLinkedList::back() const		// get back element
    { return trailer->prev->elem; }

  void DLinkedList::displayViaAge(string age) {                     //Displays person via age
      int check = 0;
	  DNode*temp = header;

       while(temp!=trailer)
       {
		  age = str.find(temp->elem);

		   if(check == 1){
              cout << temp->elem <<endl;
              temp = temp -> next;
		   }
       }
	   
	   cout << temp->elem<<endl;
  }

   void DLinkedList::displayViaName(const Elem& name) {                     //Displays person via age
      	  	  int check = 0;
	  DNode*temp = header;

       while(temp!=trailer)
       {

		   if(temp->elem == name){
              cout << "Yes that Person is in out system" << endl;
			  check = 1;
           }
		   temp = temp -> next;
       }
	   
	   if(temp->elem == name){
              cout << "Yes that Person is in out system" << endl;
			  check = 1;
	   }
	   if(check == 0){ cout << "Sorry that person is not in our system" << endl;}
	   check = 0;
  }

  class Person {
  public: 
	void print();
	string getName();
	int getAge();
  private:
	  string name;
	  int age;
  };

int main(){
	char input = 'z';
	string entry;
	int age;
	DLinkedList person;

	person.addFront("Takkun Bradly 19");
	person.addFront("Devindra Ardnived 18");
	person.addFront("SeboY Wang 20");
	person.addFront("DoubleX Slash 31");
	person.addFront("Uncle Jelly 17");
	person.addFront("test 12");
	
	cout << "What would you like to do?" << endl;
	cout << "Enter 'A' to: Add a new person" << endl;
	cout << "Enter 'B' to: Remove a person" << endl;
	cout << "Enter 'C' to: Search for people via age" << endl;
	cout << "Enter 'D' to: Search for people via name" << endl;
	cout << "Enter 'E' to: Average all the total ages" << endl;
	cout << "Enter 'F' to: Quit" << endl;

while(input != 'f') {

	cin >> input;
	cout << endl;

		while ((input != 'a')&&(input != 'b')&&(input != 'c')&&(input != 'd')&&(input != 'e')&&(input != 'f')) {
			
			cout << "Please enter a valid selection" << endl;			
			cin >> input;
		}

		if ((input == 'a')){
			cout << "Please enter their name and age: ";
			cin.ignore();
			getline(cin, entry);
			person.addFront(entry);
			
		}

		if ((input == 'b')){
			cout << "Who would you like to remove: ";
			cin.ignore();
			getline(cin, entry);
			person.removeFront();
		}

		if ((input == 'c')){
			cout << "What is the age of the person you are looking for?: ";
			cin.ignore();
			getline(cin, entry);
			person.displayViaAge(entry);
		}

		if ((input == 'd')){
			cout << "What is the name of the person you are looking for?: ";
			cin.ignore();
			getline(cin, entry);
			person.displayViaName(entry);
		}

		if ((input == 'e')){
			cout << "The total average of ages are: " << endl;
		}

		cout << endl;
	}
}
Topic archived. No new replies allowed.