double linked list (insert beginning or end)
pls help, our prof asked us to create a double linked list, and this is what i came up with:
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
|
struct node{
int data;
node *LS, *RS;
}*head, *tail, *tmp, *p;
void begin()
{
tmp = new node;
cout << "Enter number: ";
cin >> tmp->data;
if (head = NULL)
{
tmp->RS = NULL;
}
else
{
tmp->RS = head;
head = tmp;
}
//tmp->RS = NULL;
////tail = head;
//if (head == NULL)
// head = tmp;
//else {
// while (tail->RS != NULL)
// tail = tmp->RS;
// //tail = tail->RS;
// //tail->RS = tmp;
//}
}
void end()
{
tmp = new node;
cout << "Enter number: ";
cin >> tmp->data;
//tail = head;
tmp->RS = NULL;
if (head == NULL)
{
//tmp->LS = NULL;
head = tmp;
}
else {
while (head != NULL)
head = head->RS;//tmp->LS = tmp;
head->RS = tmp;//tail->LS = tmp;
head->RS->LS = head;
}
}
void outputt()
{
//tail = head;
if (head == NULL)
cout << "Empty";
else {
if (tail != NULL) {
tail = tail->LS;
cout << &tail << " " << tail->data << " " << &tail << endl;
tail = tail->RS;
}
else
cout << 0 << " " << tail->data << " " << &tail << endl;
tail = tail->RS;
}
system("pause>0");
}
|
i dont know how to put RS(right side) and LS(leftside) together on the outputt function. T_T
Topic archived. No new replies allowed.