8 Replies - 395 Views - Last Post: 08 October 2011 - 02:20 PM Rate Topic: -----

#1 black_yurizan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 86
  • Joined: 18-June 11

Can't run other linked list program

Posted 07 October 2011 - 12:39 PM

Okay I'm trying to practice using linked lists, so I can get a better understanding of it. I did a linked list program in the past, but I basically copied it from a book and didn't know what I was doing. Anyways, I started doing a new program that has to do with linked list and whenever I try to run it the old program that I did with linked list shows up. What could the problem be? I was thinking that it possibly had to do with me not putting in delete functions to all the pointers that use the "new keyword". I put in the delete functions on the old linked list program, yet it still did not let me run the new linked list program. Does anyone know what could be the problem?


I'll show the code to the new linked list program if that helps anybody
#include<iostream>
#include<cstdlib>
#include<string>

using namespace std;

struct node
{ int num;
 node *link;
       }*p;
       
int main()
{
    node * root;
    
    root = new node;
    
    root->num=5;
    root->link=p;
    p=root;
    
    node *q;
    
    for(q=p;q!=NULL; q->link)
    { cout<<q->link<<endl;
                    }
                 
                    
    system("pause"); return 0;
}



This post has been edited by black_yurizan: 07 October 2011 - 12:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Can't run other linked list program

#2 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Can't run other linked list program

Posted 07 October 2011 - 12:55 PM

I can see a problem here:
 for(q=p;q!=NULL; q->link)

You have forgot to update pointer q.It should be updated to move forward:
for(q=p;q!=NULL;q = q->link)

This post has been edited by aaa111: 07 October 2011 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 2,270
  • Joined: 31-December 10

Re: Can't run other linked list program

Posted 07 October 2011 - 12:58 PM

You're only creating one node, and you never delete that node at the end. You might want to create another class that is actually the linked list, and it will hold a node that will be the root. The linked-list class would have functions for adding nodes, deleting nodes, and traversing the list.

Even if you had more than 1 node in the list in your current code, the for-loop you have to print is wrong. A correct version of iterating through a linked-list would look like this:
for(node* curr = root; curr; curr = curr->link)
    cout << "curr->num = '" << curr->num << "'\n";


Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Can't run other linked list program

Posted 07 October 2011 - 12:58 PM

> for(q=p;q!=NULL; q->link)
You probably want q = q->link, to walk down the list.

Likewise, print the data, not the link.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,684
  • Joined: 16-October 07

Re: Can't run other linked list program

Posted 07 October 2011 - 02:11 PM

#include<iostream>
#include<cstdlib>
#include<string>

using namespace std;

struct node {
	int num;
	node *link;
}; // *p; globals are always bad.  why do you need this?
       
int main() {
	node *root, *p;
    
	root = new node;
	root->num=5;
	root->link=p; // p is currently garbage
	p=root; 
	// p is now root
	// making the point of haveing a name root unclear
	// p->link still points to crap

	node *q;
	for(
		q=p; // fine, q points to the start of you list
		q!=NULL; // fine, go until q is null
		q->link // this doesn't actually do anything, just returns q->link
		) 
	{
		// ok, you've now skipped the value in q and are printing out the pointer address
		cout<<q->link<<endl;
	}
	// this will run forever, printing out the same thing over and over
	
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#6 black_yurizan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 86
  • Joined: 18-June 11

Re: Can't run other linked list program

Posted 08 October 2011 - 11:16 AM

okay, I did what everyone suggested which was to add the q=q->link in the for loop. However, the old link list is still running whenever I try to run the new link list program. So I'm assuming that the problem has to do with old link list program or something.


So this is the old link list program. Could the possibility of this problem have to do with the fact that I didn't add enough delete functions?
include<iostream>
using namespace std;
class CAT 
{  public:
  CAT(){itsAge=1;}
  CAT(int age):itsAge(age){}
 ~CAT(){};
 int GetAge() const { return itsAge; }
private:
int itsAge;
      };
      
      
     class Node
    {
    public:
Node (CAT*);
~Node();
void SetNext(Node * node) { itsNext = node; }
Node * GetNext() const { return itsNext; }
CAT * GetCat() const { return itsCat; }
void Insert(Node *);
void Display();
 private:
CAT *itsCat;
Node * itsNext;

           }; 
           
     Node::Node(CAT *pCat):
     itsCat(pCat), 
     itsNext(0)
     {}      
     
     Node::~Node()
     {
       cout<<"Deleting node...\n";
       delete itsCat;
       itsCat = 0;
       delete itsNext;
       itsNext = 0;
                  
                  }
                  
      void Node::Insert (Node* newNode)
      {
           if (!itsNext)
           itsNext = newNode;
           else
           {int  NextCatsAge = itsNext->GetCat()->GetAge();
            int  NewAge = newNode->GetCat()->GetAge();
            int  ThisNodeAge = itsCat->GetAge();
            
            if ( NewAge >= ThisNodeAge && NewAge < NextCatsAge)
            { newNode ->SetNext(itsNext);
              itsNext = newNode;
             }
            else
            itsNext->Insert(newNode);
           }
           
       }    
       
       
       
       void Node::Display()
       {
           if(itsCat->GetAge()> 0 )
           {cout<<"My cat is ";
           cout<<itsCat->GetAge()<<" years old"<<endl;
           }
           if(itsNext)
           {
                      itsNext->Display();
           }
        }      
        
        int main ()
        {
            Node *pNode = 0;
            CAT *pCat = new CAT(0);
            int age;
            
            Node *pHead = new Node (pCat);
            
            while(1)
            { cout<<" New cat's age ? ( 0 to quit):";
              cin >> age;
              if(!age)
              break;
              pCat = new CAT (age);
              pNode = new Node(pCat);
              pHead->Insert(pNode);
              
                    } 
            pHead->Display();
            delete pHead;
            delete pCat;
            delete pNode;
            cout<<" Exiting..."<<endl;
            
            system("pause"); return 0;
        }



Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,684
  • Joined: 16-October 07

Re: Can't run other linked list program

Posted 08 October 2011 - 12:29 PM

So... this code has nothing to do with prior code.

You're posting code from "Teach Yourself C++ in 21 Days, Second Edition" found here: http://newdata.box.s...1.htm#Heading48

It's not the best code, even if someone did manage to publish it. ( Which makes me think the above link mightn't be the most legal. )

I still don't see a question. I'm at a loss to see how you tried to implement any suggestions.
Was This Post Helpful? 0
  • +
  • -

#8 black_yurizan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 86
  • Joined: 18-June 11

Re: Can't run other linked list program

Posted 08 October 2011 - 01:27 PM

@baagavi My question was, why should another program that has linked list be running when I try to compile and run my recently new linked list. The people on here suggested to make q=q->link to be able to update the list. If you want to see the updated code than here it is

#include<iostream>
#include<cstdlib>
#include<string>

using namespace std;

struct node
{ int num;
 node *link;
       }*p;
       
int main()
{
    node * root;
    
    root = new node;
    
    root->num=5;
    root->link=p;
    p=root;
    
    node *q;
    
    for(q=p;q!=NULL; q= q->link)
    { cout<<q->num<<endl;
                    }
                 
                    
    system("pause"); return 0;
}





Maybe I'm not asking the right question or maybe people never had this happen to them. So, I'm just going to delete the old link list and hope that will make the new link list program run. Thanks, to all the people that tried to help me
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,684
  • Joined: 16-October 07

Re: Can't run other linked list program

Posted 08 October 2011 - 02:20 PM

Ok, so here's your code.

#include<iostream>
#include<cstdlib>
#include<string>

using namespace std;

struct node{
	int num;
	node *link;
}; // global still bad *p;
       
int main() {
	node *root, *p;

	root = new node;

	root->num=5;
	root->link=p; // p is still uninitialized
	p=root; // ->link still points to crap

	// good, you fixed this
	for(node *q=p;q!=NULL; q= q->link) {
		cout<<q->num<<endl;
	}
	
	return 0;
}




Now, as I stated before, if you don't initialize p, it will just point to crap. It might even point to crap still in memory from a previous execution.

Understand that freed or deleted memory is not zeroed out memory. Neither is available memory. Why bother, really? You're only going to write over it.

Let's play a game:
#include<iostream>
#include<cstdlib>

using namespace std;

struct node {
	int num;
	node *link;
};
       
int main() {
	node *p;
	
	cout << (unsigned long)p << endl;
	for(int i=0; i<20; i++) {
		cout<< p->num << " : " << (unsigned long)p->link << endl;
		p = p->link;
	}
	
	return 0;
}



Depending on how lucky I get on my drunk pointer walk, I might get all 20 steps, but chances are I'll crash before then.

However, you might notice that first step is usually around the same range. It can reasonably step on a prior program...

I'm going to assume you have a book and you're trying to learn... Here's how you'd do a simple linked list:
#include<iostream>
#include<cstdlib>
#include<string>

using namespace std;

struct node {
	int num;
	node *link;
};
       
int main() {
	// always start with NULL, we point at nothing
	node *newNode, *root = NULL;
	// list is now root --> NULL
	
	// add our first node
	newNode = new node; 
	newNode->num = 2;
	newNode->link = root; // recall, root is NULL
	root = newNode; // root now points to top
	// list is now root --> (2) --> NULL
	
	// add another node
	newNode = new node; 
	newNode->num = 3;
	newNode->link = root; // pointing to the node containing 2
	root = newNode; // list is now root --> (3) --> (2) --> NULL

	// add another node
	newNode = new node; 
	newNode->num = 5;
	newNode->link = root; // pointing to the node containing 3
	root = newNode; // list is now root --> (3) --> (2) --> NULL
	

	for(node *p=root; p!=NULL; p=p->link) {
		cout<<p->num<<endl;
	}
	
	return 0;
}



For a slightly more classy version, give your node a constructor:
#include<iostream>
#include<cstdlib>
#include<string>

using namespace std;

struct node {
	int num;
	node *link;
	// require paramters for our node
	node(int n, node *l) : num(n), link(l) { }
};
       
int main() {
	// always start with NULL, we point at nothing
	node *root = NULL;
	// list is now root --> NULL
	
	// add our first node
	root = new node(2,root); 
	// this is tricky, but handy
	// you pass the current root, which is NULL, and the becomes the linke
	// list is now root --> (2) --> NULL
	
	root = new node(3,root); // list is now root --> (3) --> (2) --> NULL
	root = new node(5,root); // list is now root --> (5) --> (3) --> (2) --> NULL

	for(node *p=root; p!=NULL; p=p->link) {
		cout<<p->num<<endl;
	}
	
	return 0;
}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1