LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2004, 06:48 PM   #1
karlan
Member
 
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158

Rep: Reputation: 30
Simple link list problem


can anyone look over this really quick and tell me some mistakes i made
Code:
#include <iostream>
using namespace std;
class windowLnkList{
    public:
	windowLnkList(windowLnkList *whereToPush = 0){
		////////////////
		///////Note: that above means the class after(Pnext)
		//////	     and below means the class before(Pprev)
		////////////////
		//This is where a chain link is inserted

		//(1)This is where we add references to the above/below class to this one
		Pprev = (whereToPush != 0) ? (whereToPush)               : (0);//Link to "object below's" location, if exists
		Pnext = (whereToPush != 0) ? (whereToPush->Pnext)        : (0);//Link to "object above's" location, if exists

		//(2)This is where we add references to this class to the above/below classes
		        (Pnext != 0)       ? (Pnext->Pprev = this)       : (0);//link object above, if app, to this location
			(whereToPush != 0) ? (whereToPush->Pnext = this) : (0);//link object below, if app, to this location
		}
	~windowLnkList(){
		if(Pprev != 0)//if this isn't the first item in the list
			Pprev->Pnext = Pnext;
		if(Pnext != 0)//if this isn't the last item in the list
			Pnext->Pprev = Pprev;
		}
	void display(){
		cout << "Pnext: " << &Pnext << endl;
		cout << "this: " << this << endl;
		cout << "Pprev: " << &Pprev << endl << endl;
		if(Pnext != 0){
			Pnext->display();}
		}
	windowLnkList *Pprev;
	windowLnkList *Pnext;
};
int main(int argc, char *argv){
windowLnkList class1;
//class1.display();
windowLnkList class2(class1);
//class2.display();
windowLnkList class3(class1);
class1.display();
}
 
Old 01-16-2004, 07:21 PM   #2
macro-linux
LQ Newbie
 
Registered: Sep 2003
Posts: 25

Rep: Reputation: 15
I am in the win,so can't compile it,
but in the main function, there must be a return type,
the other I an't help you!
 
Old 01-17-2004, 08:33 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Re: Simple link list problem

Isn't this:
Code:
Pprev = (whereToPush != 0) ? (whereToPush) : (0);
Pnext = (whereToPush != 0) ? (whereToPush->Pnext) : (0);
..the same as:
Code:
Pprev = whereToPush;
Pnext = whereToPush->Pnext;

And isn't this:
Code:
(Pnext != 0) ? (Pnext->Pprev = this) : (0);
... a confusing way of saying:
Code:
if (Pnext != NULL) {
        Pnext->Pprev = this;
}
 
Old 01-22-2004, 11:41 AM   #4
spectrumver1
Member
 
Registered: Oct 2003
Location: 127.0.0.1
Distribution: Gentoo x86_64, Fedora Core 1 x86_32
Posts: 31

Rep: Reputation: 15
yeah, but I think the (0) should be changed to NULL to prevent any value from being stored there, and I personally like the ( ) ? : way of making if statements. Potato PotAto.

Hasta
Jeff
 
Old 01-22-2004, 01:05 PM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Ya, you definitly want to use NULL, there is a big different between something pointing to 0 and something pointing to NULL.

As far as the () ? :, it sucks for readability when statements get more complex then simple if-then-else statements which is why few people use it. In general most new guides to C and C++ tell people to shy away from them in general. I remember a professor I had a in college telling us if he saw those operators in our code we would be repeating his class.

It was his feeling that those, along with goto's, we completely unexceptable in 99.9% of all cases.

I complied and ran this code with a return statement and NULL's and it seams to work fine for me.

Last edited by jtshaw; 01-22-2004 at 01:12 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
link list reversl program vijeesh_ep Programming 3 02-27-2005 12:17 AM
need simple program to manage a mailing list linuxlimbo Linux - Software 0 12-21-2004 05:30 AM
What is Cursor implementation of Link List ? indian Programming 0 11-26-2004 10:54 AM
simple link question (ln) true_atlantis Linux - Newbie 1 12-02-2003 02:08 PM
Simple program link as Dockapp in Fluxbox Nerd2 Linux - Software 1 05-31-2003 08:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration