Popping out a number from stack in c++? - c++

my code so far is...
struct stack_struct
{
int number;
struct stack_struct *next_number;
};
stack_struct *mainStruct;
class stack_class
{
private:
struct stack_struct *head;
public:
stack_class();
~stack_class();
void pushNumber(int number);
void popNumber();
void findNumber();
void clearStack();
void sizeFinder();
void printStack();
void reverseStack();//Extra Credit
};
stack_class mainClassStack;
stack_struct *pointerFunc,*tailPointer=NULL,*pointerFunc3,*printPointer;
stack_class::stack_class()
{
head=NULL;
}
stack_class::~stack_class()
{
clearStack();
cout<<"\nList Cleared.\n";
system("pause");
}
void stack_class::popNumber()
{
stack_struct *pointerPop=NULL,*pointerPop2=NULL;
int popCounter=0,i=0;
pointerPop2=tailPointer;
if(head==NULL)
{
cout<<"\nNo Member to Delete.\n";
}
else
{
while(pointerPop2)
{
popCounter++;
//cout<<pointerFunc3->number<<endl;
pointerPop2=pointerPop2->next_number;
}
pointerPop=tailPointer;
while(i<(popCounter-2))
{
pointerPop=pointerPop->next_number;
i++;
}
pointerPop->next_number=NULL;
delete head;
head=pointerPop;
}
}
void stack_class::printStack()
{
pointerFunc3=tailPointer;
if(tailPointer==NULL)
{
cout<<"\nNo Members in List.\n";
}
else
{
cout<<"\n\nList Is:\n";
while(pointerFunc3)
{
cout<<pointerFunc3->number<<endl;
pointerFunc3=pointerFunc3->next_number;
}
}
}
Popping works fine as long as it is not the last number. If the last number is popped (list is empty), and I try to print the list, the program infinitely prints garbage. If I try to pop a number after the list is empty, the program freezes. How do I fix these?

You could also allways to create one dummy node for solve this. Otherwise:
int stack_class::popNumber()
{
if(head==NULL)
throw new Exception("Stack is empty");
int result = head->number;
delete head;
head = head->next_number;
return result;
}
void stack_class::pushNumber(int number)
{
stack_struct *elem= new stack_struct();
elem->number = number;
elem->next_number = head;
head = elem;
}
!!!!!try do not use globals variables like (stack_class mainClassStack;
stack_struct *pointerFunc,*tailPointer=NULL,)

Change your popNumber() like this,
void stack_class::popNumber()
{
stack_struct *pointerPop=NULL,*pointerPop2=NULL;
int popCounter=0,i=0;
pointerPop2=tailPointer;
if(head==NULL)
{
cout<<"\nNo Member to Delete.\n";
}
else if(head->next_number==NULL)
{
cout<<"\nThe Poped element is"<<head->number;
head=NULL;
}
else
{
// Your Code;
}
}

include cstdlib for exit
int Stack::pop()
{ if (top == NULL)
{
cerr <<"Stack is empty";
exit(1);
}
else
{
Node* zap = top;
top = zap->link;
int return_value = zap->n;
delete zap;
return return_value;
}
}
Qazi you have to put the if(top==NULL)check at the start of both procedures.

Related

Programs doesn't add more than one Node to the tree. Searching comes Up with only one node info

#include<iostream>
#include <conio.h>
using namespace std;
class Student_Node{
public:
Student_Node *right,*left;
int ID;
string First_Name;
int Age;
float Test_Score,CGPA;
};
class Student_Tree{
Student_Node *root;
public:
Student_Tree()
{
root=NULL;
}
void take_input();
void build_tree(int id,string name,int age,float score,float cgpa);
void BST_search();
void input_sort(Student_Node *n, Student_Node *r);
void Search (int id);
void searching(int id,Student_Node *r);
void Node_Depth(int id);
void FindHeight();
int Height(Student_Node* root);
};
/////////////////////////////////////////////////////////////////////////
This is my build function. In take_input() i apply a loop to give the user choice to add as many nodes as he wants. In the loop the user gives data which is passed the obj.build_tree(int id,string name,int age,float score,float cgpa); But it adds only one Node and nothing more. When i call my Search (int id) it only outputs the data of first node.
void Student_Tree::build_tree(int id,string name,int age,float score,float
cgpa)
{
Student_Node *n= new Student_Node();
n->ID=id;
n->First_Name=name;
n->Age=age;
n->CGPA=cgpa;
n->Test_Score=score;
if(root==NULL)
{
root=n;
}
else
{
input_sort(n,root);
}
}
void Student_Tree::input_sort(Student_Node *n, Student_Node *r)
{
if(r->ID>=n->ID)
{
if(r->left!=NULL)
{
input_sort(n,r->left);
}
else
{
n=r->left;
}
}
if(r->ID<=n->ID)
{
if(r->right!=NULL)
{
input_sort(n,r->right);
}
else
{
n=r->right;
}
}
}
/////// inout takes input from user, then sends it to the build fucntion
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
void Student_Tree::Search (int id)
{
Student_Node *n=new Student_Node();
n=root;
if(n==NULL)
{
cout<<"Tree is empty"<<endl;
}
else
{
searching(id,root);
}
}
void Student_Tree::searching(int id,Student_Node *r)
{
if(r->ID>=id)
{
if(r->ID==id)
{
cout<<"ID"<<" "<<"Age"<<" "<<"CGPA"<<" "<<"First Name"<<" "<<"Test
Score"<<endl;
cout<<"-------------------------------------------------------------
---------"<<endl;
cout<<r->ID<<" "<<r->Age<<" "<<r->CGPA<<" "<<r->First_Name<<" "<<r->
Test_Score;
}
else
{
searching(id,r->left);
}
}
else if(r->ID<=id)
{
if(r->ID==id)
{
cout<<r->ID;
}
else
{
searching(id,r->right);
}
}
else
{
cout<<"Roll No Not found"<<endl;
}
}
void Student_Tree::FindHeight()
{
Height(root);
}
int Student_Tree::Height(Student_Node *r)
{
if(r==NULL)
{
return 0;
}
else
{
int lb=Height(r->left);
int rb=Height(r->right);
cout<<max(lb,rb)+1;
return max(lb,rb)+1;
}
}
/////////////////////////////////////////////////////////////////////////
void Student_Tree::Node_Depth(int id)
{
int depth = 0;
Student_Node *temp = new Student_Node;
temp = root;
// Run the loop untill temp points to a NULL pointer.
while(temp != NULL)
{
depth++;
if(temp->ID == id)
{
cout<<"\nData found at depth: "<<depth<<endl;
return;
}
// Shift pointer to left child.
else if(temp->ID > id)
temp = temp->left;
// Shift pointer to right child.
else
temp = temp->right;
}
cout<<"\n Data not found";
return;
}
void take_input()
{
Student_Tree obj;
int ID;
string First_Name;
int Age;
float Test_Score,CGPA;
cout<<"How many students data do you want to enter?"<<endl;
int no; cin>>no;
for(int i=0;i<no;i++)
{
cout<<"\t\t\t\tEnter Student Id"<<endl;
cout<<"\t\t\t\t";cin>>ID;
cout<<"\t\t\t\tEnter Student First Name"<<endl;
cout<<"\t\t\t\t";cin>>First_Name;
cout<<"\t\t\t\tEnter Student Age"<<endl;
cout<<"\t\t\t\t";cin>>Age;
cout<<"\t\t\t\tTest Student Score"<<endl;
cout<<"\t\t\t\t";cin>>Test_Score;
cout<<"\t\t\t\tEnter Student CGPA"<<endl;
cout<<"\t\t\t\t";cin>>CGPA;
obj.build_tree(ID,First_Name,Age,Test_Score,CGPA);
}
}
int main()
{
Student_Tree obj;
take_input();
cout<<"Enter node"<<endl;
int n; cin>>n;
obj.Node_Depth(n);
obj.FindHeight();
cout<<"\nEnter id to search"<<endl; int id; cin>>id;
obj.Search(id);
return 0;
}
Your build_tree function calls input_sort to add an additional node. But input_sort never adds anything to anything else. What code do you think actually attaches a second node into the tree? There doesn't seem to be any.
if(root==NULL)
{
root=n;
}
else
{
input_sort(n,root);
}
The first part of this code adds n to the tree if the tree is empty. The second part is expected to add n to the three if the tree is not empty.
void Student_Tree::input_sort(Student_Node *n, Student_Node *r)
{
if(r->ID>=n->ID)
{
if(r->left!=NULL)
{
input_sort(n,r->left);
}
else
{
n=r->left;
We're already hosed right here. This function was called with n being the new node to be added and no other pointer to that node was stored anywhere. If we change the value of n here, we lose the only pointer we had to the node we were supposed to add.
Even if we don't get hosed here, there's no code in input_sort anywhere to actually add a node to the tree, and build_tree is expecting it to.

i don't know how to call a function whose parameter is Node type

I want to call this function in main() but problem is that what parameter I should pass in it while it receives Node type argument
In Simple I just wanna see the height of my tree. So please help me, remove the error which I face during the function call of height it needs a Node type parameter and I have no idea what should I pass. Here is the whole code
#include <iostream>
using namespace std;
// Binary Search Tree
//
class Node
{
public:
int data;
Node *left,*right;
Node(Node *l=NULL ,int d=0, Node *r=NULL)
{
left=l;
data=d;
right=r;
}
};
// Binary Search Tree
class Tree
{
Node *root;
public :
Tree()
{
root=NULL;
}
bool isEmpty()
{
if(root==NULL)
return true;
else
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// insert funcation
/////////////////////////////////////////////////////////////////////////////////////////////////
void insert(int val)
{
if(isEmpty())
{
root=new Node(NULL,val,NULL);
}
else if(val < root->data && root->left==NULL)
{
Node *p=new Node(NULL ,val,NULL);
root->left=p;
}
else if(val > root->data && root->right==NULL)
{
Node *p=new Node (NULL ,val,NULL);
root->right=p;
}
else if(val < root->data)
insert(val ,root->left);
else
insert(val ,root->right);
}
////////////////////////////////////////
void insert(int val,Node *n)
{
if(val>n->data && n->right==NULL)
{
Node *p=new Node(NULL,val,NULL);
n->right=p;
}
else if(val > n->data)
insert(val,n->right);
else if(val <n->data && n->left==NULL)
{
Node *p=new Node(NULL,val,NULL);
n->left=p;
}
else
insert(val,n->left);
}
//////////////////////////////////////////////////////////////////////////////////////
// pre Order all data display
//////////////////////////////////////////////////////////////////////////////////////
void preOrder(void)
{
if(isEmpty())
cout<<"Tree is Empty\n";
else
preOrder(root);
}
void preOrder(Node *n)
{
if(n!=NULL)
{
cout<<n->data<<endl;
preOrder(n->left);
preOrder(n->right);
}
}
//////////////////////////////////////////////////////////////////////////////////////
// in fix Order all data display
//////////////////////////////////////////////////////////////////////////////////////
void inOrder()
{
if(isEmpty())
cout<<"Tree is Empty\n";
else
inOrder(root);
}
void inOrder(Node *n)
{
if(n!=NULL)
{
inOrder(n->left);
cout<<n->data<<endl;
inOrder(n->right);
}
}
//////////////////////////////////////////////////////////////////////////////////////
// post Order all data display
//////////////////////////////////////////////////////////////////////////////////////
void posOrder()
{
if(isEmpty())
cout<<"Tree is Empty\n";
else
posOrder(root);
}
void posOrder(Node *n)
{
if(n!=NULL)
{
posOrder(n->left);
posOrder(n->right);
cout<<n->data<<endl;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// Search funcation
/////////////////////////////////////////////////////////////////////////////////////////////////
void search(int val)
{
if(isEmpty())
cout<<"Tree is Empty\n";
else
search(val,root);
}
void search(int v,Node *p)
{
if(v==p->data)
cout<<"val : "<<p->data<<endl;
else if(v < p->data && p->left!=NULL)
search(v,p->left);
else if(v>p->data && p->right!=NULL)
search(v,p->right);
else
cout<<"Data Not Found \n";
}
Node *l;
int deleteKey(int val)
{
if(isEmpty())
cout<<"Tree is Empty\n";
else if(root->data==val &&(root->left==NULL&&root->right==NULL))
{
int temp=root->data;
delete root;
return temp;
}
else
deleteKey(val,root);
}
int deleteKey(int v,Node *p)
{
if(v == p->data)
{
if(p->left==NULL && p->right==NULL)
{
if(l->right==p)
{
int temp=p->data;
delete p;
l->right=NULL;
return temp;
}
else
{
int temp=p->data;
delete p;
l->left=NULL;
return temp;
}
}
else if(p->right!=NULL)
{
int temp=p->data;
deleteKey(p,p->right);
return temp;
}
else
{
int temp=p->data;
cout<<"Left : "<<p->data<<endl;
deleteKey(p,p->left,v);
return temp;
}
}
else if(v < p->data && p->left!=NULL)
{
l=p;
deleteKey(v,p->left);
}
else if(v>p->data &&p->right!=NULL)
{
l=p;
deleteKey(v,p->right);
}
else
cout<<"Data Not Found ----\n";
}
int deleteKey(Node *find ,Node *next)
{
if( next->left == NULL && next->right != NULL )
{
find->data = next->data;
deleteKey(find->right , next->right);
}
else if( next->left == NULL&& next->right==NULL)
{
find->data = next->data;
delete next;
find->right=NULL;
}
else
{
Node *q;
while(next->left!=NULL)
{
q=next;
next=next->left;
}
find->data=next->data;
delete next;
q->left=NULL;
}
}
int deleteKey(Node* find,Node *next,int v)
{
if( next->right == NULL && next->left != NULL )
{
find->data = next->data;
deleteKey(find->left , next->left,v);
}
else if( next->right == NULL&& next->left==NULL)
{
find->data = next->data;
delete next;
find->left=NULL;
}
else
{
Node *q;
while(next->right!=NULL)
{
q=next;
next=next->right;
}
find->data=next->data;
delete next;
q->right=NULL;
}
}
~Tree()
{
dist();
}
void dist()
{
dist(root);
}
void dist(Node *n)
{
if(n!=NULL)
{
dist(n->left);
dist(n->right);
delete n;
}
}
int height(Node *root)
{
int h=0;
if (isEmpty())
{
cout<<"Tree is Empty\n";
}
else
{
int left_height=height(root->left);
int right_height=height(root->right);
h=1+max(left_height, right_height);
}
return h;
}
};
int main()
{
Tree obj;
obj.height();
}
Well you have to pass, root of your tree in this function, but the better approach will be if you make one more function without any parameter and make that function public and call your this private function from it by passing this->root;
Here you can see :
public:
int getHeight()
{
return height(this->root); //pass your Tree class root
}
and make that function private in class , for efficiency.
private:
int height(Node *root)
{
int h=0;
if (isEmpty())
{
cout<<"Tree is Empty\n";
}
else
{
int left_height=height(root->left);
int right_height=height(root->right);
h=1+max(left_height, right_height);
}
return h;
}
Another, approach is to make a getRoot() function in class and get Root of Tree class in main and pass to height function. But The first approach will be better.

Why isn't my function returning the count? (Binary Tree)

Everything in my program works perfectly, except it is not showing the count for the field. I tried to change some codes but it does not seem to display it in the output. It seems like a easy mistake or something obviously but I have no idea why it doesn't display it. Can someone help?
#include <iostream>
using namespace std;
template<class T>
class BinaryTree
{
struct Node
{
T data;
Node* leftChild;
Node* rightChild;
Node(T dataNew)
{
data = dataNew;
leftChild= NULL;
rightChild = NULL;
}
};
private:
Node* root;
void Add(T new_Data, Node* &the_Root)
{
if (the_Root == NULL)
{
the_Root = new Node(new_Data);
return;
}
if (new_Data < the_Root->data)
Add(new_Data, the_Root->leftChild);
else
Add(new_Data, the_Root->rightChild);
}
int countTree(Node* the_Root)
{
if (the_Root == NULL)
return 0;
else {
int count = 1;
count += countTree(the_Root->leftChild);
count += countTree(the_Root->rightChild);
return count;
}
}
void PrintTree(Node* the_Root)
{
if (the_Root != NULL)
{
cout << the_Root->data <<" ";
PrintTree(the_Root->leftChild);
PrintTree(the_Root->rightChild);
}
}
public:
BinaryTree()
{
root = NULL;
}
void AddItem(T new_Data)
{
Add(new_Data, root);
}
int countTree()
{
return countTree(root);
}
void PrintTree()
{
PrintTree(root);
}
};
int main()
{
BinaryTree<int> *myBT = new BinaryTree<int>();
myBT->AddItem(6);
myBT->AddItem(5);
myBT->AddItem(4);
myBT->AddItem(18);
myBT->AddItem(6);
myBT->PrintTree();
myBT->countTree();
}
The count won't be shown simply because you didn't write any code to show the count.
Try changing myBT->countTree(); to cout << myBT->countTree();, and you will see the count printed.
You aren't actually doing anything with the result of myBT->countTree(). Try printing it:
std::cout << myBT->countTree() << std::endl;

Why my c++ program doesn't print all the items of a LinkedList?

I am learning c++ and tried a creating a Linked-List Data structure.
Here is the program-
main.cpp
#include <iostream>
using namespace std;
class LinkedList
{
public:
int data;
LinkedList *nextNode;
void init(int value,LinkedList *root)
{
root->data=value;
root->nextNode=NULL;
}
void add(int value,LinkedList *root)
{
LinkedList *temp=new LinkedList;
if(root->nextNode==NULL)
{
//cout<<"IF ADD()"<<endl;
temp->data=value;
temp->nextNode=NULL;
root->nextNode=temp;
}
else
{
//cout<<"else ADD()"<<endl;
while(root->nextNode!=NULL)
{
root=root->nextNode;
}
temp->data=value;
temp->nextNode=NULL;
root->nextNode=temp;
}
}
void display(LinkedList *root)
{
if(root->nextNode==NULL)
{
cout<<root->data<<endl;
}
else
{
while(root->nextNode!=NULL)
{
cout<<root->data<<endl;
root=root->nextNode;
}
}
}
void free(LinkedList *root)
{
if(root->nextNode==NULL)
{
delete root;
}
else
{
while(root->nextNode!=NULL)
{
LinkedList *temp=root->nextNode;
delete root;
root=temp;
}
}
}
};
int main()
{
LinkedList *root=new LinkedList;
root->init(1,root);
root->add(2,root);
root->add(3,root);
root->add(4,root);
root->add(5,root);
root->add(6,root);
root->add(7,root);
root->add(8,root);
root->add(9,root);
root->add(10,root);
root->display(root);
root->free(root);
//root->display(root);
//delete root;
return 0;
}
Output-
1
2
3
4
5
6
7
8
9
My question is why it doesn't print the last item i.e 10.
And one more question, as you can see I am commenting in the following line
//delete root
inside my main method.
What if I don't call my free() method and uncomment this? Would it free the whole LinkedList?
Thanks
Porblem is with this function in else statement it does not go to last node because last node's next will be NULL. One more thing is you are modifying the root in display function is should be a const function. It should not modify the root.
void display(LinkedList *root)
{
if(root->nextNode==NULL)
{
cout<<root->data<<endl;
}
else
{
while(root->nextNode!=NULL)
{
cout<<root->data<<endl;
root=root->nextNode;
}
}
}
Actual implementation should be like this.
void display(LinkedList *root) const
{
LinkedList * temp = root;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->nextNode;
}
}
Regarding your second question-> It won't clear the memory. But because its a small code after application close memory will be freed. It's always best practice to free memory otherwise you will have memory leaks and if this data structure is used heavily it can take all memory and crash the application.
Also implementation of your free function is not correct. It should be like this
void free(LinkedList *root)
{
LinkedList * temp = root;
LinkedList * nodeToFree = root;
while(temp!=NULL)
{
temp=temp->nextNode;
delete nodeToFree;
nodeToFree = temp;
}
}
while(root->nextNode!=NULL)
{
cout<<root->data<<endl;
root=root->nextNode;
}
should be
while(root!=NULL)
{
cout<<root->data<<endl;
root=root->nextNode;
}
change Condition of display
#include <iostream>
using namespace std;
class LinkedList
{
public:
int data;
LinkedList *nextNode;
void init(int value,LinkedList *root)
{
root->data=value;
root->nextNode=NULL;
}
void add(int value,LinkedList *root)
{
LinkedList *temp=new LinkedList;
if(root->nextNode==NULL)
{
//cout<<"IF ADD()"<<endl;
temp->data=value;
temp->nextNode=NULL;
root->nextNode=temp;
}
else
{
//cout<<"else ADD()"<<endl;
while(root->nextNode!=NULL)
{
root=root->nextNode;
}
temp->data=value;
temp->nextNode=NULL;
root->nextNode=temp;
}
}
void display(LinkedList *root)
{
if(root->nextNode==NULL)
{
cout<<root->data<<endl;
}
else
{
while(root !=NULL) // Change this condition
{
cout<<root->data<<endl;
root=root->nextNode;
}
}
}
void free(LinkedList *root)
{
if(root->nextNode==NULL)
{
delete root;
}
else
{
while(root->nextNode!=NULL)
{
LinkedList *temp=root->nextNode;
delete root;
root=temp;
}
}
}
Here you can display it one time only as you are changing its root value so it would be better if you use temp variable to display as done by #sanjay in above case.

How do we display a linked list?

So I created the following bit of code but the display function is giving me problems. It breaks into an infinite loop each time I try using it. Could someone please have a look at it and tell me what's going wrong ?
#include<iostream.h>
#include<conio.h>
struct node{
int info;
node *next;
}*ptr,*start,*temp;
node* create_new()
{
ptr=new node;
cout<<"\nEnter the data: ";
cin>>ptr->info;
ptr->next=NULL;
return ptr;
}
void insert_at_beg()
{
ptr=create_new();
if(start==NULL)
{
start=ptr;
}
if(start!=NULL)
{
ptr->next=start;
start=ptr;
}
}
void display()
{
temp=start;
while(temp->next!=NULL)
{
cout<<"\t"<<temp->info;
temp=temp->next;
}
}
void insert_at_end()
{
if(start==NULL)
{
start=ptr;
}
if(start!=NULL)
{
ptr=create_new();
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
}
}
void delete_from_end()
{
if(start==NULL)
{
cout<<"NULL LL";
}
else
{
temp=start;
while(temp->next!=NULL)
{
ptr=temp;
temp=temp->next;
}
ptr->next=NULL;
delete temp;
}
}
void delete_from_beg()
{
if(start==NULL)
cout<<"\nNULL LL";
else
start=start->next;
}
void delete_from_mid()
{
int el;
if(start==NULL)
{
cout<<"\nNULL LL";
}
else
{
cout<<"\nEnter element that you want to delete: ";
cin>>el;
temp=start;
while(temp->next!=NULL&&temp->info!=el)
{
ptr=temp;
temp=temp->next;
}
ptr->next=temp->next;
delete temp;
}
}
void main()
{
clrscr();
start=NULL;
temp=NULL;
ptr=NULL;
insert_at_beg();
display();
getch();
}
Your bug is in this code:
void insert_at_beg()
{
ptr=create_new();
if(start==NULL)
{
start=ptr;
}
if(start!=NULL)
{
ptr->next=start;
start=ptr;
}
}
The first time in start will be null, so you'll do start=ptr. However, now if(start!=NULL) will be true, so you'll do ptr->next=start. Since ptr and start both point to the same thing you'll create an infinite loop.
Firstly I would not recommend you to use global variables especially when you have functions that themselves output parameters of the same type (and value).
The problem according to me is in the function insert_at_beg():
// yes start is NULL initially
if(start==NULL)
{
start=ptr; // now start is not NULL!!
}
//This statement will also be entered.
if(start!=NULL)
{
ptr->next=start;
start=ptr;
}
}
Instead use an else
if(start==NULL)
{
start=ptr; // now start is not NULL!!
}
else
{
ptr->next=start;
start=ptr;
}
}
Also, instead of:
#include<iostream.h>
#include<conio.h>
Use just #include<iostream>
Your bug that cause infinite loop is somewhere else (insert_at_beg(), I saw that someone already added the details so I won't do this too) in the code.
You still have a problem in your code:
void display()
{
temp=start;
while(temp->next!=NULL)
{
cout<<"\t"<<temp->info;
temp=temp->next;
}
}
You will not print the last elemnt. You stop when the next of the current element (temp) is NULL.
Change this to
while(temp) // equivalent to while(temp !=NULL)
Your display function can be simplified:
void display()
{
node * p = start;
while (p != NULL)
{
cout << "\t" << p->info;
p = p->next;
}
cout << endl; // The side effect of this is to print blank line when empty list.
}