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
|
#include <iostream>
#include <cstddef>
#include <string>
using namespace std;
struct Node
{
string item;
int count;
Node *link;
};
typedef Node* NodePtr;
void insert(NodePtr after_me, string an_item, int a_number);
void head_insert(NodePtr& head, string an_item, int a_number);
void show_list(NodePtr& head, string target);
NodePtr search(NodePtr head, string target);
void list_remove(NodePtr& head, string remove_item, string target);
int main()
{
string new_item, target, remove_item;
int new_count;
NodePtr head = NULL;
head_insert(head, "Tea", 2);
head_insert(head, "Jam", 3);
head_insert(head, "Rolls", 10);
cout << "List contains:" << endl;
show_list(head, target);
NodePtr after_me = head; after_me = after_me ->link;
cout << "Enter the item you wish to insert (string) \n";
cin >> new_item;
cout << "Enter the count of new item \n";
cin >> new_count;
cout << "Enter the item after which you want \n";
cout << "to insert the new node \n";
cin >> target;
after_me = search(head, target);
if(after_me != NULL)
{
cout << "\nWill insert " << new_item << " with count" << endl << new_count << " after the node with " << (after_me -> item) << endl << endl;
insert(after_me, new_item, new_count);
cout << "List now contains: " << endl;
//list_remove(head, remove_item, target);
show_list(head, target);
}
else
{
cout << "I can't find " << target << " in the list, so I can't insert anything \n";
}
system ("pause");
return 0;
}
void list_remove(NodePtr& head, string remove_item, string target)
{
NodePtr remove_ptr; //pointer to the node that is being removed
remove_ptr = search(head, remove_item); // finds the address for item being removed and stores it in remove_ptr
}
//Uses cstddef:
void insert(NodePtr after_me, string an_item, int a_number)
{
NodePtr temp_ptr;
temp_ptr = new Node;
temp_ptr -> item = an_item;
temp_ptr -> count = a_number;
temp_ptr ->link = after_me -> link;
after_me ->link = temp_ptr;
}
//Uses cstddef:
void head_insert(NodePtr& head, string an_item, int a_number)
{
NodePtr temp_ptr;
temp_ptr = new Node;
temp_ptr -> item = an_item;
temp_ptr -> count = a_number;
temp_ptr->link = head;
head = temp_ptr;
}
//Uses iostream and cstddef:
void show_list(NodePtr& head, string target)
{
NodePtr here = head;
while (here != NULL)
{
cout << here-> item << "\t";
cout << here-> count << endl;
here = here->link;
//delete (&target) //When I tried deleting target, I got a runtime error
}
}
NodePtr search(NodePtr head, string target)
{ // Point to the head node
NodePtr here = head;
// If the list is empty nothing to search
if (here == NULL)
{
return NULL;
}
// Search for the item
else
{
// while you have still items and you haven't found the target yet
while (here-> item != target && here->link != NULL)
here = here->link;
// Found the target, return the pointer at that location
if (here-> item == target)
return here;
// Search unsuccessful, return Null
else
return NULL;
}
}
|