Create a while loop
Aug 18, 2012 at 7:27pm UTC
Hi All,
I have been working on pointer's and arrays lately and need help finishing this. So what would be the way to make this program display the letters A through C in this, I am assuming a while loop but dont understand how it would work.
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
#include <iostream>
using namespace std;
struct Node
{
char letter;
Node * next;
};
int main()
{
Node * head;
Node * tail;
Node * newNode;
newNode = new Node;
newNode->letter = 'A' ;
newNode->next = NULL;
head = newNode;
tail = head;
newNode = new Node;
newNode->letter = 'B' ;
newNode->next = NULL;
tail->next = newNode;
tail = newNode;
newNode = new Node;
newNode->letter = 'C' ;
newNode->next = NULL;
tail->next = newNode;
tail = newNode;
system("pause" );
return 0;
}
I thought through the stack that the referenced point "Node" would keep changing so if you inputted a whiel loop after the last section
1 2 3 4 5 6 7 8 9 10 11
newNode = new Node;
newNode->letter = 'C' ;
newNode->next = NULL;
tail->next = newNode;
tail = newNode;
[INSERT WHILE LOOP HERE]
system("pause" );
return 0;
? I am not looking for a hand out but an understand and a push in the right direction. Thank you!
Aug 18, 2012 at 7:32pm UTC
To print out all nodes' letters
1 2 3 4 5 6 7 8 9
Node *current = head;
while ( current )
{
std::cout << current->letter << ' ' ;
current = current->next;
}
std::cout << std::endl;
Aug 18, 2012 at 10:25pm UTC
int i = 0
while ( i<100)
do
cout <<i;
char i == i+1
Aug 18, 2012 at 11:29pm UTC
Bad design. Use encapsulation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
template <typename T>
typedef struct
{
struct node_t
{
node_t *succ;
node_t *pred;
T data;
}
node_t *head;
node_t *tail;
void push_back(T const &);
///other methods
} container_t;
Better organization.
Topic archived. No new replies allowed.