941,506 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9176
  • C RSS
Nov 4th, 2006
0

Stack using a linear linked list of arrays

Expand Post »
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 Nick Evan; Jan 7th, 2010 at 8:39 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jess_redrose is offline Offline
2 posts
since Nov 2006
Nov 4th, 2006
0

Re: Stack using a linear linked list of arrays

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();
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5563
Solved Threads: 2275
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,897 posts
since Aug 2005
Nov 5th, 2006
0

Re: Stack using a linear linked list of arrays

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jess_redrose is offline Offline
2 posts
since Nov 2006
Nov 5th, 2006
0

Re: Stack using a linear linked list of arrays

please post structure node. And use code tags -- see my signature for link.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5563
Solved Threads: 2275
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,897 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help: Passing arrays between functions
Next Thread in C Forum Timeline: Help making an Output File look presentable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC