CS1044: Programming with C++

Virginia Tech Computer Science Department
Tom Plunkett
tplunket@csgrad.cs.vt.edu

Graded Assignment #5-6: Pointer and Linked List Questions

 

Requirements

Due Date: Please email all deliverables to me by Midnight, Monday, November 9th. The project can be turned in by November 16th, for reduced credit. No submissions will be accepted after November 16th.
 

Pointer Questions

What is the output from the following code?
    int *p1, *p2, A[100], i, index;
    i=57;
    A[0] = 51;
    A[1] = 107;
    A[2] = 100;
    p2 = &i;
    p1=A;
    index=0;
    cout << *p1 <<endl;
    cout <<p1[index] <<endl;
    cout <<*p2 <<endl;
    index++;
    p2 = new int;
    cout << *p1 <<endl;
    cout << p1[index]<<endl;
    cout << *p<<endl;
 
Linked List Question
 
 
 

Type Definitions

struct Bag
{
    double size[20];
    int count;
    Bag *next;
    Bag *previous;
};
typedef Bag* BagPtr;

What is the output produced by the following code

BagPtr head, tail, current;
head=new Bag;
tail=new Bag;
head->next=tail;
head->previous=NULL;
head->size[0]=100.00;
(*head).count=0;
tail->previous=head;
tail->next=NULL;
(*tail).count=(head->count)++;
current=tail->previous;
tail->size[0]=50.0;
cout << current->count<<endl;
cout << tail->count<<endl;
cout << (*current->next).size[0]<<endl;
cout << (*current->previous).size[0]<<endl;