I want to Implement a STACK class using linked structure to store the following information about students:
i. Registration number of the student
ii. Name of the student

I want to Write client code, such that based on the registration number of the students,
Then I want to Retain only the record of the students enrolled in the fall semester and pop out all the student
records enrolled in the spring semester
students enrolled in spring semester have their registration number start from f like f010123 and students enrolled in spring have their registration number start from s.
I know to implement stack using array but not using linked list. Can anybody help me to solve this problem??
Thannnks in Advance

Zaina jee
Deleted Member

Basically, a linked list for a stack will only have one link, pointing to the next element. As such your stack will look something like this:

First->Second->Third->NULL (where -> indicates that the value has a pointer to the value to the right)

In terms of the "standard" linked list struct:

struct LinkedListNode
    string val;//this doesnt have to be a string, it can be literally anything!
    LinkedListNode *next;//this is the key to linked lists

With the above structure the example would look like this:

Your stack object would be First and would contain "First" and a pointer to Second, which would contain "Second" and a pointer to Third, which would contain "Third" and a pointer to NULL.

The key to linked list implementations is that when you add a node you can't do it like this:

void addNode(LinkedListNode *myStack)//this is an epic hint!!!
    LinkedListNode newNode={"New",myStack};

The reason is that newNode will go out of scope. The answer is to use the new operator. IE:

LinkedListNode *newNode=new LinkedListNode;//ta-da!

The new operator makes the new structure "global" (in a way). Other than that a stack is actually pretty easy to implement. If you are stuck on a particular function I would suggest a diagram. Draw each node as a box. In each box put all the values that a node should store, then draw an arrow from the box to whatever its "next" pointer points to. Then using just this diagram implement each of your functions. Whatever you end up doing in your diagram is exactly what you have to do in your program. Most of your functions are going to be similar, in that you will do something with the current node, then change what the current node is. If you need more information than this I would suggest googling for "Linked Lists" as they are a fairly standard topic in computer science.

This article has been dead for over six months. Start a new discussion instead.