Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "list.h"
- #include <iostream>
- using namespace std;
- Rec* first = NULL;
- Rec* last = NULL;
- Rec* current;
- bool dupeID(Rec r)
- {
- current = first;
- while (current != NULL)
- {
- if (strcmp(r.id, current->id) == 0)
- {
- return true;
- }
- current = current->next;
- }
- return false;
- }
- bool checkExists(char a[15])// checks to see if the id exists
- {
- current = first;
- while (current != NULL)
- {
- if (strcmp(a, current->id) == 0)
- {
- return false;
- }
- current = current->next;
- }
- return true;
- }
- int AddItem(Rec r)
- {
- Rec* current = new Rec; // creates new Rec
- Rec* firstCopy = first;
- bool dupe = dupeID(r);
- if (dupe) // checks to see if the id is a duplicate
- {
- cout << "You've entered a duplicate ID. Please re-enter the record with a valid ID." << endl;
- return 0;
- }
- if (first == NULL) {
- current->id = r.id;
- strcpy_s(current->firstname,15, r.firstname);
- strcpy_s(current->lastname,15, r.lastname);
- current->next = NULL;
- current->previous = NULL;
- first = current;
- last = current;
- return 1;
- }
- else {
- if (((string)last->lastname).compare(r.lastname) < 0) {
- current->id = r.id;
- strcpy_s(current->firstname, 15, r.firstname);
- strcpy_s(current->lastname, 15, r.lastname);
- current->previous = last;
- current->next = NULL;
- last->next = current;
- last = current;
- return 1;
- }
- else if (((string)first->lastname).compare(r.firstname) > 0) {
- current->id = r.id;
- strcpy_s(current->firstname, 15, r.firstname);
- strcpy_s(current->lastname, 15, r.lastname);
- current->previous = NULL;
- current->next = first;
- first->previous = current;
- first = current;
- return 1;
- }
- else {
- bool check = true;
- while (check == true) {
- if ((strcmp((char*)first->next, r.lastname) > 0)) {
- strcpy_s(current->lastname, 15, r.lastname);
- current->id = r.id;
- current->previous = firstCopy;
- current->next = firstCopy->next;
- firstCopy->next = current;
- check = false;
- return 1;
- }
- firstCopy = firstCopy->next;
- }
- }
- }
- return 0;
- }
- int DeleteItem(char* delid) { // deletes the item
- bool idExists = checkExists(delid);
- if (idExists)
- {
- cout << "The ID you entered does not exist in the list. Please try again with a valid ID." << endl;
- return 0;
- }
- Rec* RecToDelete = first;
- if (strcmp(RecToDelete->id, delid) == 0) // if the Rec is located in the front delete like this
- {
- if (RecToDelete->next == NULL && RecToDelete->previous == NULL) // if theres only 1 Rec in the list
- {
- first = NULL; // this needs to be Null to keep a refrence to the front
- return 0;
- }
- delete RecToDelete->id; // deletes the id first
- RecToDelete->next->previous = NULL; // set addresses to null... cant delete and address
- first = RecToDelete->next;
- delete RecToDelete; // deletes everything
- return 1;
- }
- else
- {
- Rec* RecAfterRecToDelete = RecToDelete->next; // gets the next record
- if (RecToDelete == NULL || RecAfterRecToDelete == NULL) // tests to see if either are NULL
- {
- return 0;
- }
- while (strcmp(RecAfterRecToDelete->id, delid) > 0 || strcmp(RecAfterRecToDelete->id, delid) < 0) // loops to search for the delid
- {
- RecToDelete = RecToDelete->next; // moves to the next Rec
- RecAfterRecToDelete = RecAfterRecToDelete->next; // moves to the next Rec
- if (RecToDelete == NULL || RecAfterRecToDelete == NULL) // tests to see if either are NULL
- {
- return 0;
- }
- }
- RecToDelete->next = RecAfterRecToDelete->next; // moves to the next spot
- if (RecAfterRecToDelete->next != NULL) // if id wasnt found
- {
- RecAfterRecToDelete->next->previous = RecToDelete;
- }
- }
- }
- void PrintList(int order) {
- Rec* ptr;
- if (order == 0) {
- ptr = first;
- if (ptr == NULL) {
- cout << "list is empty" << endl;
- }
- while (ptr != NULL) {
- cout << ptr->id << " ";
- cout << ptr->firstname << " ";
- cout << ptr->lastname << " ";
- cout << ptr->previous << " " << endl; // address of previous item
- cout << ptr << " "; // address of item
- cout << ptr->next << " " << endl; // address of next item
- cout.flush();
- ptr = ptr->next;
- }
- }
- else if (order == 1) {
- {
- ptr = first;
- while (ptr != NULL) // this gets the last Rec and traverses backwards
- {
- last = ptr;
- ptr = ptr->next;
- }
- ptr = last;
- while (ptr != NULL) {
- cout << ptr->id << " ";
- cout << ptr->firstname << " ";
- cout << ptr->lastname << " ";
- cout << ptr->previous << " " << endl; // address of previous item
- cout << ptr << " "; // address of item
- cout << ptr->next << " " << endl; // address of next item
- if (ptr->previous == NULL)
- {
- break;
- }
- ptr = ptr->previous;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment