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
|
#include <iostream>
#include <string>
using namespace std;
struct Cafe
{
string item;
int count;
Cafe *link;
};
typedef Cafe* CafePtr;
void head_insert(CafePtr& head, string i, int c);
CafePtr searchnum(CafePtr& head, int target);
CafePtr searchstr(CafePtr& head, string target);
void insert (CafePtr after_me, string i, int c);//insert a node in the list
void print (CafePtr head); //print the list
void remove(CafePtr before, CafePtr discard); //remove a node from the list
int main(){
CafePtr head;
CafePtr iter, h;
head = new Cafe;
head->link = NULL;
head_insert(head, "QL", 3808);
head_insert(head, "CHGHS", 1312);
head_insert(head, "REI", 10466);
head_insert(head, "CPT", 1678);
head_insert(head, "NetApp", 6887);
head_insert(head, "EJ", 36937);
head_insert(head, "WFM", 41717);
head_insert(head, "SAS", 6046);
head_insert(head, "BCG", 1958);
head_insert(head, "Google", 18500);
print (head);
//head ->item = "Google";
//head ->count = 34311; //If i try to do this with other items, the program just crashes
CafePtr after_me = searchstr(head, "Google");
insert(after_me, "SAS", 6373);
CafePtr before = searchstr(head, "Google");
CafePtr discard = searchstr(head, "BCG");
remove(before, discard);
//Needs to turn into a linked list that looks like: (by using insert and remove)
//head_insert(head, "CPT", 1896);
//head_insert(head, "Ultimate", 1440);
//head_insert(head, "EJ", 35114);
//head_insert(head, "Hilcorp", 1012);
//head_insert(head, "NetApp", 7426);
//head_insert(head, "WFM", 43927);
//head_insert(head, "BCG", 2314);
//head_insert(head, "CHGHS", 1378);
//head_insert(head, "SAS", 6373);
//head_insert(head, "Google", 34311);
cout << endl;
print(head);
return 0;
}
void head_insert(CafePtr& head, string i, int c)
{
CafePtr temp_ptr;
temp_ptr = new Cafe;
temp_ptr -> item = i;
temp_ptr -> count = c;
temp_ptr -> link = head;
head = temp_ptr;
}
CafePtr searchnum(CafePtr& head, int target)
{
CafePtr here = head;
if(here == NULL)
{
return NULL;
}
else
{
while(here->count !=target && here->link != NULL)
here = here->link;
if(here->count == target)
return here;
else
return NULL;
}
}
CafePtr searchstr(CafePtr& head, string target)
{
CafePtr here = head;
if(here == NULL)
{
return NULL;
}
else
{
while(here->item !=target && here->link != NULL)
here = here->link;
if(here->item == target)
return here;
else
return NULL;
}
}
void insert (CafePtr after_me, string i, int c)
{
CafePtr temp_ptr;
temp_ptr = new Cafe;
temp_ptr->item = i;
temp_ptr->count = c;
temp_ptr->link = after_me->link;
after_me->link = temp_ptr; //this lets the previous link point to temp_ptr
}
void print (CafePtr head)
{
for (CafePtr iter = head; iter !=NULL; iter=iter->link)
{
cout << (iter->item) << "\t" << (iter->count) << endl;
}
}
void remove(CafePtr before, CafePtr discard)
{
before->link = discard->link;
delete discard; //after adding this, you can remove any node but the head node
}
|