|
 |
|
 |
01-09-2003, 09:15 PM
|
#1 (permalink)
|
|
Totally Inept
Join Date: Jul 2002
Location: The Great Northwest
Posts: 195
|
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.
|
|
|
01-09-2003, 09:21 PM
|
#2 (permalink)
|
|
Guest
|
Re: Pointers
Quote:
Originally posted by w00t
Pointers are cool
|
Yeah, but them damn kids abuse them always pointing at people... 
|
|
|
|
01-09-2003, 09:24 PM
|
#3 (permalink)
|
|
Techno Rat
Join Date: Jan 2003
Location: San Diego
Posts: 559
|
my teacher hits my head with hers ;/
no joke!
Ilya
__________________
> SELECT * FROM users WHERE clue > 0
0 rows returned
|
|
|
01-11-2003, 04:12 PM
|
#4 (permalink)
|
|
Non-profit Techie
Join Date: Dec 2002
Location: Mesa AZ
Posts: 76
|
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. 
|
|
|
01-11-2003, 04:15 PM
|
#5 (permalink)
|
|
Guest
|
Pointer are evil! I have seen idiots point to pointers of pointers! AHH! :o But they are generally good. 
|
|
|
|
01-11-2003, 04:18 PM
|
#6 (permalink)
|
|
Non-profit Techie
Join Date: Dec 2002
Location: Mesa AZ
Posts: 76
|
Vlad ..thats called a link list  LOL
|
|
|
01-11-2003, 04:20 PM
|
#7 (permalink)
|
|
Guest
|
Quote:
Originally posted by DesertWolf
Vlad ..thats called a link list LOL
|
*fake laughing*
*doesn't really understand* 
|
|
|
|
01-11-2003, 04:28 PM
|
#8 (permalink)
|
|
Non-profit Techie
Join Date: Dec 2002
Location: Mesa AZ
Posts: 76
|
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.
|
|
|
01-11-2003, 04:29 PM
|
#9 (permalink)
|
|
Guest
|
ah ok, my knowledge of coding/computers is very amateur:o
|
|
|
|
02-06-2003, 10:21 PM
|
#10 (permalink)
|
|
Registered User
Join Date: Feb 2003
Posts: 34
|
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.
|
|
|
03-02-2003, 10:29 AM
|
#11 (permalink)
|
|
Registered User
Join Date: Mar 2003
Posts: 3
|
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.
|
|
|
03-02-2003, 10:44 AM
|
#12 (permalink)
|
|
Registered User
Join Date: Feb 2003
Posts: 34
|
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 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.
|
|
|
03-02-2003, 11:18 AM
|
#13 (permalink)
|
|
Registered User
Join Date: Mar 2003
Posts: 3
|
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 
|
|
|
03-02-2003, 11:44 AM
|
#14 (permalink)
|
|
Registered User
Join Date: Feb 2003
Posts: 34
|
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;
|
|
|
03-02-2003, 12:15 PM
|
#15 (permalink)
|
|
Registered User
Join Date: Mar 2003
Posts: 3
|
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.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 09:35 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|