Solved

linked list rep. of stack

Posted on 1998-11-18
5
359 Views
Last Modified: 2012-05-04
What is the code for a linked list rep. of a stack in C++.
Lost my code. need it though.
0
Comment
Question by:krn
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1254377
This question sounds suspiciously like homework.  In that case t will be unethical for us to write it for you.  However, we can help with *specific* problems.

To get you started:

A stack usually implements two operations, PUSH - which pushes an item on the stack and POP - which returns the topmost item and removes it from the stack (sometimes the query and removal are split into two distinct operations).

An implementation using a linked-list can either add and remove items from the beginning of the list (if the list supports such operations) or keep a pointer to the last item.

Case #1:
  PUSH(item) - Add the new item to the beginning of the list.
  POP() - Return the first item and remove it from the list.

Case #2:
  PUSH(item) - Add the new item to the end of the list and update the pointer.
  POP() - Return the item pointed to (last item) and remove it from the list.

0
 

Author Comment

by:krn
ID: 1254378
Well in that case, I must decrease the points.
I will, on the pother hand, ask more questions about the problem as they arise.
0
 

Accepted Solution

by:
baprebap earned 200 total points
ID: 1254379
i will just write the POP and PUSH for you.
define a struvture as follows

struct  stacktype
{
 char *info;
 struct stacktype *link;;
};

allocate memory dynamically using the malloc() command.
firstnode is initialised to NULL in the main().the fn is invoked as
firstnode=push(firstnode,"KLJA');
pop is also called as above except that it does not take the char * field.
you may use typedef for struct stacktype * .it will be a better programming style.
pop may also be used to return the element deleted..to do so change the return type


struct stacktype *push(struct stacktype *firstnode,char *info)
{
 struct stacktype *newnode;
 newnode=(struct stacktype *)malloc(sizeof(stacktype));
 if (newnode==NULL) return(firstnode);
 strcpy(newnode->info,info);
 if (firstnode==NULL)
 {
  newnode->link=NULL:
  return(newnode);
 }
 else
 {
  newnode->link=firstnode;
  return(newnode);
 }
}

struct stacktype *pop(struct stacktype * firstnode)
{
 struct stacktype *save=firstnode;
 firstnode=firstnode->link;
 cout << "The element popped is ;" << save->info;
 free(save);
 return(firstnode);
}



0
 

Author Comment

by:krn
ID: 1254380
Thanks
That is really what I was having problems with.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1254381
>> Well in that case, I must decrease the points.
You cannot decrease points for a question.  However, you can delete a question that has no pending or rejected answers and submit another one (that is the reason I gave a comment and not an "answer").

This is just for reference since I see you got your answer.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 18
outside company writing a dll in c/c++, I want to call it from C# 2 66
Concatenate two strings Last and First Name 10 59
C hashtable library 3 83
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now