Stack using a linear linked list of arrays

Please support our C advertiser: Download Intel® Parallel Studio Eval
Reply

Join Date: Nov 2006
Posts: 2
Reputation: jess_redrose is an unknown quantity at this point 
Solved Threads: 0
jess_redrose jess_redrose is offline Offline
Newbie Poster

Stack using a linear linked list of arrays

 
0
  #1
Nov 4th, 2006
Hi everyone, I've worked on this thing for about three days now and am really getting frustrated. I need to make a stack using linear linked lists of arrays. I've got my class to compile without complaining, but am really struggling with the function definitons of push and peek. Any help would be greatly appreciated. Here is what I have so far:

  1. //function to "push", move data to the stack
  2. void Stack::push(char newItem[])
  3. {
  4. //create new node
  5. node *newPtr = new node;
  6. assert(newPtr != NULL);
  7. newPtr->item = newItem;
  8. //inserts new node
  9. newPtr->next = topPtr;
  10. topPtr = newPtr;
  11. }
  12.  
  13.  
  14. //function to return the top item in stack
  15. void Stack::peek(char stackTop[]) const
  16. {
  17. stackTop[] = topPtr->item;
  18. }
Last edited by niek_e; 33 Days Ago at 8:39 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,163
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1558
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Stack using a linear linked list of arrays

 
0
  #2
Nov 4th, 2006
probably want something like this. The function should return a const char*
  1. const char* Stack:peek(void) const
  2. {
  3. return topPtr->item;
  4. }
  5.  
  6. // example calling
  7. Stack stack;
  8. const char* item = stack.peek();
My NewYear's resolution is to lose weight.

Weight Lost since 1 Jan 2010: 8.7 lbs
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 2
Reputation: jess_redrose is an unknown quantity at this point 
Solved Threads: 0
jess_redrose jess_redrose is offline Offline
Newbie Poster

Re: Stack using a linear linked list of arrays

 
0
  #3
Nov 5th, 2006
I figured out how to use the peek function by changing a few things in my class, but I'm still lost on how to assign an array of characters to a node in a linked list. I keep getting the compiler error "incompatible types of assignment char to char[]" Here is the code I have... I'm not sure I've changed it at all, most of the changing has been within my class.

  1. void Stack::push(char newItem[])
  2. {
  3. node *newPtr = new node;
  4. assert(newPtr != NULL);
  5.  
  6. newPtr->item = newItem; //setting char item[] to newItem[]
  7. topPtr = newPtr;
  8. }
Last edited by ~s.o.s~; Nov 5th, 2006 at 12:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,163
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1558
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Stack using a linear linked list of arrays

 
0
  #4
Nov 5th, 2006
please post structure node. And use code tags -- see my signature for link.
My NewYear's resolution is to lose weight.

Weight Lost since 1 Jan 2010: 8.7 lbs
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 6888 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC