Implementing a stack using linked list is the a very basic application of the linked list data structure. It tests your ability to visualize the usage of a complex data structure (such as a Stack) in terms of a more primitive data structure such as a linked list. Stack is a LIFO (Last In First Out) structure. That means that the last element in is the first element out. In programming terms, that means that you insert new elements at the top of the list and you remove elements from the top too. Simple, right?
First, let's define our linked list data structure. This is the same as in our previous examples:
typedef struct linked_list
{
int data;
struct linked_list *next;
} Node;
For us to be able to test our code, we need to define a way to display our stack. There are multiple ways to display the stack – you can use a loop (do-while) or you can use recursion. Guess what? We have already covered recursion. So let's try recursion to display the stack.
// recursively display the contents
// of the stack
void DisplayStack(Node* currentNode)
{
// recursive termination condition
if (currentNode == NULL)
{
return;
}
// the node is not null
// display the data
printf(" -> %d", currentNode->data);
// recursively call the display to
// display the next element in the stack
DisplayStack(currentNode->next);
}
Now that we have figured out the display, let's check out the code for pushing an element onto the stack.
// push item on the stack
// this is same as adding a node
// at the top of the list
void Push(int dataToAdd)
{
// assumption: head is already defined elsewhere in the program
// 1. create the new node
Node *temp = new Node;
temp->data = dataToAdd;
// 2. insert it at the first position
temp->next = head;
// 3. update the head to point to this new node
head = temp;
}
// pop an element from the stack
// this is same as removing the first element
// from the list
Node* Pop()
{
// check for empty list
if (head == NULL)
{
printf("Stack is empty\n");
return NULL;
}
// get the top node
Node *firstNode = head;
// move the head
head = head->next;
// disconnect the node
// from the list
firstNode->next = NULL;
// return the top node
return firstNode;
}
That's it. You now know how you can implement a stack using linked list. We will discuss another application of stacks (using linked lists) in a future post.
cooooollllll maaaahnnnnn
ReplyDelete;*
damn interview!!!!!!!!
ReplyDeletewot d hell s wrong wt u...:/:/
I'm back to this nice article, Thanks for sharing and keep sharing.
ReplyDeleteerp in chennai | cloud erp software in chennai
I found a lot of interesting information here. A really good post
ReplyDeleteoffice 2010 professional plus key deutsch
I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
ReplyDeleteamazon-web-services-training-institute-in-chennai
Awesome..You have clearly explained.it is very simple to understand.it's very useful for me to know about new things..Keep posting.Thank You...
ReplyDeleteaws online training
aws training in hyderabad
amazon web services(AWS) online training
amazon web services(AWS) training online
the article is good and clarify my doubts very well. thanks for useful article.
ReplyDeleteAWS Training in Chennai
AWS course in Chennai
AWS Certification in Chennai
Best AWS Training in Chennai
thanks for giving a chance to read this.its very very helpful for study.good job
ReplyDeleteAWS Training institute in Chennai
Best devOps Training in Chennai
DevOps Training institutes in Chennai
Data Analytics Courses in Chennai
Big Data Analytics Courses in Chennai
This comment has been removed by the author.
ReplyDeleteThe WB 10th results 2019 are much awaited for release. Students are eager to know their performance in the exam. Students are hereby informed that the results of madhyamik result 2019 was released online.
ReplyDeletekarresults.nic.in SSLC result 2019 will be declared in the month of May 2019. Result will be declared on the official website. Check the Karnataka SSLC Result 2019 Date here.
Madhya Pradesh Board of Secondary Education (MPBSE) declares the results of the mp board 10th result 2019 board examinations in the MP board result website.
Waiting for the declaration of bihar board 10th result 2019 Some good news is coming your way soon. As per the latest buzz, the Bihar School Examination Board (BSEB) is planning to declare the BSEB Matric Result 2019 and Bihar Board Intermediate Result 2019 soon.
This blog is more informative and help ful when i was reading.
ReplyDeleteweb designing course with placement
php course in chennai
magento course in chennai
Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
ReplyDeletemobile application development training online
mobile app development course
mobile application development course
learn mobile application development
mobile app development training
app development training
mobile application development training
mobile app development course online
online mobile application development
Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
ReplyDeleteweb designing classes in chennai | web designing training institute in chennai
web designing and development course in chennai | web designing courses in Chennai
best institute for web designing in chennai | web designing course with placement in chennai
Web Designing Class
web designing course
best institute for web designing
Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
ReplyDeletemobile application development course | mobile app development training | mobile application development training online
"web designing classes in chennai | Web Designing courses in Chennai "
Web Designing Training and Placement | Best Institute for Web Designing
Web Designing and Development Course | Web Designing Training in Chennai
mobile application development course | mobile app development training
mobile application development training online | mobile app development course
mobile application development course | learn mobile application development
app development training | mobile application development training
mobile app development course online | online mobile application development
Its really helpful for the users of this site. I am also searching about these type of sites now a days. So your site really helps me for searching the new and great stuff.
ReplyDeleteaws training in bangalore
mulesoft training in bangalore
salesforce developer training in bangalore
salesforce admin training in bangalore
servicenow training in bangalore
cloud computing training in bangalore
dell boomi training in bangalore
Thanks for this. I really like what you've posted here and wish you the best of luck with this blog and thanks for sharing.
ReplyDeleteoracle training in bangalore
sql server dba training in bangalore
web designing training in bangalore
digital marketing training in bangalore
java training in bangalore
Excellent
ReplyDeleteSAP Training in Chennai
Java Training in Chennai
CCNA Training in Chennai
Pearson Vue Exam Center in Chennai
QTP Training in Chennai
Selenium Training in Chennai
Hardware and Networking Training in Chennai
SAP ABAP Training in Chennai
SAP FICO Training in Chennai
AWS Training in Chennai
Thanks for sharing such a nice information with us...
ReplyDeleteOracle Training in Bangalore
I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
ReplyDeleteit course
This was nice and amazing and the given contents were very useful and the precision has given here is good.
ReplyDeleteApache Spark Training in Pune
Spark Training Institute in Pune
Đại lý vé máy bay Aivivu
ReplyDeletevé máy bay đi Mỹ giá rẻ
đăng ký bay từ mỹ về việt nam
giá vé máy bay từ Vancouver về việt nam
ve may bay vietnam airline tu han quoc ve viet nam
các chuyến bay từ anh về việt nam
chuyến bay từ Pairs về TpHCM
các chuyến bay từ đức về việt nam hôm nay
ve may bay tu nga ve viet nam
thời gian bay từ Việt nam sang Los Angeles
chi phí cách ly khách sạn
Cool stuff you have and you keep overhaul every one of us
ReplyDeletedata science course in delhi
Aivivu chuyên vé rẻ, tham khảo
ReplyDeletegia ve may bay di my
chuyen bay tu my ve vietnam
bay từ anh về việt nam
giá vé máy bay từ pháp về việt nam
Đại lý vé máy bay Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ hạng thương gia
có chuyến bay từ mỹ về việt nam chưa
mua vé máy bay từ đức về việt nam
bay từ nga về việt nam
Nice and very informative blog, glad to learn something through you.
ReplyDeletedata analytics courses delhi
"Very Nice Blog!!!
ReplyDeletePlease have a look about "
data science courses aurangabad
Really Nice Information It's Very Helpful All courses Checkout Here.
ReplyDeletedata scientist courses aurangabad
Good information you shared. keep posting.
ReplyDeletedata science training in aurangabad
It’s my fortune to go to at this blog and realize out my required stuff that is also in the quality.
ReplyDeleteUI UX design firm
informative blog
ReplyDeletedata analytics courses in aurangabad
I agree with what you are introducing. It is important to form a consensus, and I think it is good enough to influence my next post. In the next article, I will try to post related to Cloud Telephony Software
ReplyDelete