0% found this document useful (0 votes)
53 views8 pages

Stack Using Array

This document contains C++ code to implement a stack data structure using both arrays and linked lists. The array implementation uses a fixed-size array to store stack elements and tracks the top index. The linked list implementation uses a Node class to represent each element, with pointers to link nodes together into a chain and track the head/top node. Both implementations support common stack operations like Push, Pop, Peek/Top, and checks for empty/full.

Uploaded by

anon_244273367
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views8 pages

Stack Using Array

This document contains C++ code to implement a stack data structure using both arrays and linked lists. The array implementation uses a fixed-size array to store stack elements and tracks the top index. The linked list implementation uses a Node class to represent each element, with pointers to link nodes together into a chain and track the head/top node. Both implementations support common stack operations like Push, Pop, Peek/Top, and checks for empty/full.

Uploaded by

anon_244273367
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Stack using array

#include <iostream>

using namespace std;

template <class t>

class Stack

private:

int *arr;

int length;

int top_element;

public:

Stack(int l)

length=((l>0)?l:10);

top_element=-1;

arr = new int[length];

void Push(t val)

if(!Is_Full())

arr[++top_element]=val;

}
else

cout<<"Stack is Full, You can't push more elements"<<endl;

t Pop()

if(!Is_Empty())

return arr[top_element--];

else

cout<<"Stack is Empty, You can't pop more elements"<<endl;

t Top()

return arr[top_element];

bool Is_Full()

if(top_element==9)

return true;
else

return false;

bool Is_Empty()

if(top_element==-1)

return true;

else

return false;

};

Stack using linked list


#include<iostream>

using namespace std;

template <class t>

class Node

private:

t data;

Node <t> *next;

Node <t> *prev;

public:

Node(t d)
{

data = d;

next = NULL;

void SetData(t d)

data = d;

t GetData()

return data;

void SetNext(Node<t> *ptr)

next = ptr;

Node<t> *GetNext()

return next;

void SetPrev(Node<t> *ptr)

prev = ptr;

Node<t> *GetPrev()
{

return prev;

};

#include <iostream>

#include "TemplateNode.h"

using namespace std;

template <class t>

class Stack

private:

Node<t> *head;

public:

Stack()

head = NULL;

void Push(t val)

Node<t> *ptr = new Node<t>(val);

ptr ->SetNext(head);

head = ptr;
}

t Pop()

if(!Is_Empty())

int val = head->GetData();

Node<t> *ptr = head;

head = head->GetNext();

delete ptr;

return val;

else

cout<<"Stack is Empty, You can't pop more elements";

t Top()

return head->GetData();

bool Is_Empty()

if(head == NULL)

return true;
else

return false;

~Stack()

Node<t> *ptr;

ptr=head;

while(ptr!=NULL)

head=head->GetNext();

delete ptr;

head=ptr;

};

#include <iostream>

#include "TemplateStackUsingArray.h"

//#include "TemplateStackUsingLinkedList.h"

using namespace std;

int main()

Stack<int> s1(10);
//Stack s;

s1.Push(3);

s1.Push(5);

cout<<s1.Pop();

cout<<s1.Pop();

Stack<char> s2(10);

s2.Push('+');

s2.Push('*');

cout<<s2.Pop();

cout<<s2.Pop();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy