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
|
#include <iostream>
#include <string>
using namespace std;
typedef unsigned short int ushort;
void enter_data(string list_data[12], ushort list_ptr[12], ushort& start, ushort& no_of_recs);
void print_list(string list_data[12], ushort list_ptr[12], ushort start, ushort no_of_recs);
int main()
{
ushort pointer[12], start_rec, records;
string data[12];
records = 0;
enter_data(data, pointer, start_rec, records);
print_list(data, pointer, start_rec, records);
enter_data(data, pointer, start_rec, records);
print_list(data, pointer, start_rec, records);
return 0;
}
void print_list(string list_data[12], ushort list_ptr, ushort start, ushort no_of_recs)
{
//print out linked list so far
cout<<start<<endl;
for(int i = 1; i == no_of_recs; ++i)
{
cout<<i<<" "<<list_data(i)<<" "<<list_ptr(i)<<endl;
}
}
void enter_data(string list_data, ushort list_ptr, ushort& start, ushort& no_of_recs)
{
ushort ptr, ptr2, prev_ptr;
bool flag;
string new_data;
if(no_of_recs == 0)
{
start = 1;
ptr =0;
}
else
{
ptr = no_of_recs;
}
do
{
flag = false;
ptr += 1;
cout<<"Enter Data";
cin>>new_data;
no_of_recs += 1;
if(new_data != "XXX")
{
list_data(ptr) = new_data;
list_ptr(ptr) = 0;
if(ptr != 1)
{
//if ptr = 1 then there is no more data to check
if((list_data(ptr)) < (list_data(start)))
{
//the new ddata needs to be added at the start of the linked list
list_ptr(ptr) = start;
start = ptr;
}
else
{
ptr2 = start;
do
{
if((list_data(ptr)) < (list_data(ptr2)))
{
list_ptr(prev_ptr) = ptr;
list_ptr(ptr) = ptr2;
flag = true;
//insert data into middle of list
}
else
{
if(list_ptr(ptr2) == 0)
{
list_ptr(ptr2) = ptr;
flag = true;
//add record at the end
}
else
{
prev_ptr = ptr2;
ptr2 = list_ptr(ptr2);
}
}
}while(flag != true);
}
}
}
}while(new_data != "XXX");
no_of_recs -= 1;
}
|