Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-09-2003, 09:15 PM   #1 (permalink)
w00t
Totally Inept
 
w00t's Avatar
 
Join Date: Jul 2002
Location: The Great Northwest
Posts: 195
w00t is on a distinguished road
Pointers

Pointers are cool
__________________
Office Space:
Best Movie.
Ever.

Contrary to popular belief, the true function of a programmer
is to turn coffee into source code.
w00t is offline   Reply With Quote
Old 01-09-2003, 09:21 PM   #2 (permalink)
anon
Guest
 
Posts: n/a
Re: Pointers

Quote:
Originally posted by w00t
Pointers are cool
Yeah, but them damn kids abuse them always pointing at people...
  Reply With Quote
Old 01-09-2003, 09:24 PM   #3 (permalink)
Ilya020
Techno Rat
 
Ilya020's Avatar
 
Join Date: Jan 2003
Location: San Diego
Posts: 559
Ilya020 is on a distinguished road
Send a message via AIM to Ilya020
my teacher hits my head with hers ;/

no joke!


Ilya
__________________
> SELECT * FROM users WHERE clue > 0
0 rows returned
Ilya020 is offline   Reply With Quote
Old 01-11-2003, 04:12 PM   #4 (permalink)
DesertWolf
Non-profit Techie
 
DesertWolf's Avatar
 
Join Date: Dec 2002
Location: Mesa AZ
Posts: 76
DesertWolf is on a distinguished road
Send a message via AIM to DesertWolf
You might think they are cool,,but most of the students I tutor in C++ are having problems with pointers.
If you really want to play around with pointer check out Cocoa (Objective C for Mac OSX) almost everything is a pointer.
DesertWolf is offline   Reply With Quote
Old 01-11-2003, 04:15 PM   #5 (permalink)
anon
Guest
 
Posts: n/a
Pointer are evil! I have seen idiots point to pointers of pointers! AHH! :o But they are generally good.
  Reply With Quote
Old 01-11-2003, 04:18 PM   #6 (permalink)
DesertWolf
Non-profit Techie
 
DesertWolf's Avatar
 
Join Date: Dec 2002
Location: Mesa AZ
Posts: 76
DesertWolf is on a distinguished road
Send a message via AIM to DesertWolf
Vlad ..thats called a link list LOL
DesertWolf is offline   Reply With Quote
Old 01-11-2003, 04:20 PM   #7 (permalink)
anon
Guest
 
Posts: n/a
Quote:
Originally posted by DesertWolf
Vlad ..thats called a link list LOL
*fake laughing*

*doesn't really understand*
  Reply With Quote
Old 01-11-2003, 04:28 PM   #8 (permalink)
DesertWolf
Non-profit Techie
 
DesertWolf's Avatar
 
Join Date: Dec 2002
Location: Mesa AZ
Posts: 76
DesertWolf is on a distinguished road
Send a message via AIM to DesertWolf
A linked list is basicly an array made up of pointers pointing to the next pointer in the list and so on.....Works fine as long as you don't forget to keep your links pointing where they need to.
DesertWolf is offline   Reply With Quote
Old 01-11-2003, 04:29 PM   #9 (permalink)
anon
Guest
 
Posts: n/a
ah ok, my knowledge of coding/computers is very amateur:o
  Reply With Quote
Old 02-06-2003, 10:21 PM   #10 (permalink)
carrja99
Registered User
 
Join Date: Feb 2003
Posts: 34
carrja99 is on a distinguished road
Quote:
Originally posted by Vlad902
Pointer are evil! I have seen idiots point to pointers of pointers! AHH! :o But they are generally good.
How is that a bad thing???

Code:
*Description: Queries the user for a filename,
 *which should contain a one lined, postfix
 *equation, and solves this equation by using
 *a linked list implementation of a stack.
 ***********************************************/
#include <iostream.h>
#include <fstream.h>
#include <cstring>

struct node
	{
	int key;
	node* next; 
	};

typedef node* stack_ptr; /*stack pointer for parameter passing */

void push(stack_ptr &list, int value)
	{
	if(!list) /* if the list is empty, do this */
		{
		list = new node;       
		list->key = value;
		list->next = NULL;

		return;
		}
	/****************************************** 
	 *create a new node, give it a value, then*
	 *add it to the beginning of the list, and*
	 *set the list pointer to the first node. *
	 ******************************************/

	node* tmp = new node;   
	tmp->key = value;        
	tmp->next = list;	
	list = tmp;		
	}			

int pop(stack_ptr &list)
	{
	if(!list)
		{
		cout << "The stack is currently empty!" << endl;
		return 0;
		}
	/**************************************
	* rtn_value is set to the key of the
	* current node, then a temp node is
	* created to point to the next node 
	* while the current node is deleted, 
	* the list pointer is then set to the
	* temp node as the current node
	***************************************/
	int rtn_val = list->key;     
	stack_ptr tmp = list->next;    
	delete list;		       
	list = tmp;		       	

	return rtn_val;
	}

void print_list(stack_ptr& list)
	{
	if(!list)
		{
		cout << "The stack is currently empty!" << endl;
		return;
		}

	node *tmp = list; /* use a temporary pointer to avoid problems */

	while(tmp)
		{
		cout << tmp->key << " ";
		tmp = tmp->next;
		}

	cout << endl;
	}

int main()
	{
	stack_ptr t = NULL;
	char inFileName[16], str1[50];
	int v;
	ifstream read;

	/****** Ask user for input file *****/
	cout << "Enter file name to be read: ";
	cin >> inFileName;

	/***** open file **********/

	read.open(inFileName);
	if (read.fail())
		{
		cout << "Input file opening failed!"<<endl;
		exit(1);
		}

        /* read file, make function calls to solve equation */

	for (int i = 0; !read.eof(); i++)   
		{
		read.getline(str1,50, ' '); /* gets current token */	
		                            /* using ' ' as diliminator */
		if (isdigit(str1[0]))
	 		{
			v = atoi(str1); /* convert string to int value */
			push(t, v);     
			}
		else if (str1[0] == '+')
			{
			v = pop(t) + pop(t);
			push (t, v);
			}
		else if (str1[0] == '-')
			{
			v = pop(t) - pop(t);
			push (t, v);
			}	
		else if (str1[0] == '*')
			{
			v = pop(t) * pop(t);
			push (t, v);
			}
		else if (str1[0] == '/')
			{
			v = pop(t) / pop(t);
			push (t, v);
			}
		
		}
	cout << "The result is: ";
	
	print_list(t); /* at this juncture, if input file is in
			  correct format, one item should be on
                          the list, the solution.            */

return 0;
}
Try implementing the above code using an array rather than a linked list.
carrja99 is offline   Reply With Quote
Old 03-02-2003, 10:29 AM   #11 (permalink)
arkadius
Registered User
 
Join Date: Mar 2003
Posts: 3
arkadius is on a distinguished road
Hello,
Since we're on linked lists, maybe someone can help me with this:
linked list copy constructor and a destructor

basically the class is as follows

class SortedList
{
private:
struct Node
{
SomeItem data;
Node *next;
}
Node *head;
int size;
public:
// retrieve, insert, remove whatnot
};

//
SortedList::SortedList(SortedList &SL)
{
//ok, here's what I'm thinking
head = SL.head;
size = SL.size;
Node *pCur;
pCur = new Node;
pCur = SL.head->next; // did I just lose the node here?
head->next = pCur;

// assuming the first part is right how do I get the
// next nodes from SL? when do I make a heap for them?
}

Any help would be appreciated.
arkadius is offline   Reply With Quote
Old 03-02-2003, 10:44 AM   #12 (permalink)
carrja99
Registered User
 
Join Date: Feb 2003
Posts: 34
carrja99 is on a distinguished road
first off, make sure that you use code tags when posting code, it makes it alot cleaner. Specify the start of the code by using the tag [ code ] and stop by using [ /code ], w/o the spaces.

Code:
class SortedList
{
private:
struct Node
{
SomeItem data;
Node *next;
}
Node *head;
int size;
public:
// retrieve, insert, remove whatnot
};

//
SortedList::SortedList(SortedList &SL)
{
//ok, here's what I'm thinking
head = SL.head;
size = SL.size;
Node *pCur;
pCur = new Node;
pCur = SL.head->next; // did I just lose the node here?
head->next = pCur;

// assuming the first part is right how do I get the
// next nodes from SL? when do I make a heap for them?
}
Now that being done, ir is really hard for me to figure out what you are doing, as you are calling functions you havent defined yet. But I looked at this:

Code:
pCur = SL.head->next;
and had to wonder, are you decideding to have head NULL and pointing to the first item, or is head the first item?
If it is the first one, this line is fine, if the latter, use
Code:
 pCur = SL.head;
Anyway, the next part doesn't make sense to me, why do you want head->next to equal pCur when you just had pCur = head->next? Seems you will lose a node each time you do that, If you want to traverse the list, just use the following, assuming pCur is pointing at the beginning of the list:
Code:
while(pCur->next != NULL)
   {
   pCur = pCur->next;
   }
something like this will keep getting the next node until you reach the end, which should be NULL.
carrja99 is offline   Reply With Quote
Old 03-02-2003, 11:18 AM   #13 (permalink)
arkadius
Registered User
 
Join Date: Mar 2003
Posts: 3
arkadius is on a distinguished road
Ok... yes, the head is the first in the list.

So once I have set the head to the adress of the SL.head, and now I need to traverse the list and actually copy the nodes

Code:
while(pCur->next != NULL)
   {
   pCur = pCur->next; // ok, so this traverses the list
   // but it still references the address of the original node (SL)
   // so calling any function, that mods the list, at this point
   // would affect the SL list, right? So what's next? a new pointer?
   }
So I need something that keeps track of the last node, traverses the SL list, and something that creates the new nodes

Code:
Node *pPrev;
Node *pCur;
Node *pSL;

head = SL.head;
pPrev = head;
pSL = SL.head->next;

while(pSL != NULL)
{
  pCur = new Node;
  pCur->data = pSL->data; 
  pPrev->next = pCur;
  pSL = pSL->next;
  pPrev = pCur;
}
This makes somewhat of a sense to me to, I guess am I waaay of the target? And thank's again
arkadius is offline   Reply With Quote
Old 03-02-2003, 11:44 AM   #14 (permalink)
carrja99
Registered User
 
Join Date: Feb 2003
Posts: 34
carrja99 is on a distinguished road
Oh! Are you trying to keep track of the previous node? for example:

Code:
[]->[]->[]->[]->[]
         ^   ^
         |   |
       prev  current
in that case, there are several ways you could achieve this, the easiest being to use two pointers, one pointing to a current (but considered previous) node, and the other pointing to next. As for insertion and deletion, it's fairly easy.

the basis for insertion is to create a new node, then traverse the list till you find a node whose node->next is greater than the node you want to insert. you then woulld connect your newnode to the next node, and connect the current to the new node.

as for deletion, you simply need to make a copy of the link from the deleted node, example;
Code:
tmp = deletednode->next;
delete deletednode;
currentNode->next = tmp;
carrja99 is offline   Reply With Quote
Old 03-02-2003, 12:15 PM   #15 (permalink)
arkadius
Registered User
 
Join Date: Mar 2003
Posts: 3
arkadius is on a distinguished road
I have insert and remove, it's just that darn copy constructor that's giving me a headache. Basically, I'm required to have a method that has the list as a parameter. The thing I wrote compiled but I just have no way to test it right now and I'm paranoid ;_;
Thanks for the help though, I'll surely be back.
arkadius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
far or near iwanttolearnc Standard C, C++ 2 06-15-2004 09:28 PM
A day late and pointers short saline Standard C, C++ 2 10-04-2003 10:33 AM
What does void do? w00t Standard C, C++ 11 08-22-2002 07:26 AM
pointers .. sde Standard C, C++ 2 06-24-2002 12:54 PM


All times are GMT -8. The time now is 04:05 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting