The document discusses data structures and provides an example program that uses a circular linked list to solve the Josephus problem. It explains how the program includes the CList class, which defines methods like add, remove, next, and get for manipulating the list. The program is able to efficiently solve the problem by using these pre-defined methods rather than implementing the logic directly. The document also discusses benefits of abstract data types and how they allow changing the underlying implementation without affecting programs, and introduces stacks as another important data structure.
YouTube Link: https://youtu.be/Iq9tUK-6J78
** Java Certification Training: https://www.edureka.co/java-j2ee-training-course **
This Edureka PPT on “LinkedList vs ArrayList in Java” will talk about the differences between LinkedList and ArrayList in Java. Through this PPT, you will learn the following topics:
What is a LinkedList?
What is an ArrayList?
Similarities between LinkedList and ArrayList
Differences between LinkedList and ArrayList
Follow us to never miss an update in the future.
YouTube: https://www.youtube.com/user/edurekaIN
Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
Castbox: https://castbox.fm/networks/505?country=in
YouTube Link: https://youtu.be/Iq9tUK-6J78
** Java Certification Training: https://www.edureka.co/java-j2ee-training-course **
This Edureka PPT on “LinkedList vs ArrayList in Java” will talk about the differences between LinkedList and ArrayList in Java. Through this PPT, you will learn the following topics:
What is a LinkedList?
What is an ArrayList?
Similarities between LinkedList and ArrayList
Differences between LinkedList and ArrayList
Follow us to never miss an update in the future.
YouTube: https://www.youtube.com/user/edurekaIN
Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
Castbox: https://castbox.fm/networks/505?country=in
Welcome to my series of articles on Unified Modeling Language. This is "Session 4 – Object Diagram" of the series.
Please view my other documents where I have covered each UML diagram with examples
Object oriented modeling,Object-Oriented Database (OODB), Object-Oriented Database (OODB) FEATURES TO BE CONSIDERED, DATA MODELS TO BE CONSIDERED:
object and object identifier, attributes and methods, class, Class hierarchy and inheritance
RDBMS. Stands for "Relational Database Management System." An RDBMS is a DBMS designed specifically for relational databases. ... A relational database refers to a database that stores data in a structured format, using rows and columns. This makes it easy to locate and access specific values within the database.
1. Perform Linear Search and Binary Search on an array.
Descriptions of the programs:
Read and array of type integer.
Input element from user for searching.
Search the element by passing the array to a function and then returning the position of the element from the function else return -1 if the element is not found.
Display the positions where the element has been found.
2. Implement sparse matrix using array.
Description of program:
Read a 2D array from the user.
Store it in the sparse matrix form, use array of structures.
Print the final array.
3. Create a linked list with nodes having information about a student and perform.
Description of the program:
Insert a new node at specified position.
Delete of a node with the roll number of student specified.
Reversal of that linked list.
4. Create doubly linked list with nodes having information about an employee and perform Insertion at front of doubly linked list and perform deletion at end of that doubly linked list.
5. Create circular linked list having information about a college and perform Insertion at front perform Deletion at end.
6. Create a stack and perform Pop, Push, Traverse operations on the stack using Linear Linked list.
7. Create a Linear Queue using Linked List and implement different operations such as Insert, Delete, and Display the queue elements.
Welcome to my series of articles on Unified Modeling Language. This is "Session 4 – Object Diagram" of the series.
Please view my other documents where I have covered each UML diagram with examples
Object oriented modeling,Object-Oriented Database (OODB), Object-Oriented Database (OODB) FEATURES TO BE CONSIDERED, DATA MODELS TO BE CONSIDERED:
object and object identifier, attributes and methods, class, Class hierarchy and inheritance
RDBMS. Stands for "Relational Database Management System." An RDBMS is a DBMS designed specifically for relational databases. ... A relational database refers to a database that stores data in a structured format, using rows and columns. This makes it easy to locate and access specific values within the database.
1. Perform Linear Search and Binary Search on an array.
Descriptions of the programs:
Read and array of type integer.
Input element from user for searching.
Search the element by passing the array to a function and then returning the position of the element from the function else return -1 if the element is not found.
Display the positions where the element has been found.
2. Implement sparse matrix using array.
Description of program:
Read a 2D array from the user.
Store it in the sparse matrix form, use array of structures.
Print the final array.
3. Create a linked list with nodes having information about a student and perform.
Description of the program:
Insert a new node at specified position.
Delete of a node with the roll number of student specified.
Reversal of that linked list.
4. Create doubly linked list with nodes having information about an employee and perform Insertion at front of doubly linked list and perform deletion at end of that doubly linked list.
5. Create circular linked list having information about a college and perform Insertion at front perform Deletion at end.
6. Create a stack and perform Pop, Push, Traverse operations on the stack using Linear Linked list.
7. Create a Linear Queue using Linked List and implement different operations such as Insert, Delete, and Display the queue elements.
In this educational video, we will provide an introduction to data structures. You will learn what data structures are, including queues, stacks, trees, and binary search. We will explore different types of data structures and specifically dive into priority queues. By watching this ppt, you will gain knowledge and have the opportunity to improve your command of data structures. Don't miss this chance to learn something new and expand your understanding.
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfkarymadelaneyrenne19
Abstract Data Types
(a) Explain briefly what is meant by the term abstract data type (ADT). Give two
reasons why use of ADTs is good programming practice.
(b) Write out a signature, or interface, that defines the operations of a stack ADT.
(c) Consider a string of characters of the form
... (.( ... ).) ...
where ... indicates an arbitrary sequence of characters (except for parentheses),
(.( indicates an arbitrary number (one or more) of opening parentheses, and
similarly ).) indicates an arbitrary number of closing parentheses.
Using only the stack abstraction operations defined above, write pseudocode for
an algorithm that determines, using a stack, whether or not the number of closing
parentheses is the same as the number of opening parentheses.
You may assume the existence of a function read(str,ch) that reads the next character
of string str into ch.
You may also assume that you can invoke a function reportFail, that will cause
termination with failure, and similarly, reportSuccess causes termination with a
success indication.
Further, you may also assume that you can call a function newStack(S) to create
a new empty stack S, and eos(str) that returns false when you reach the end of
the string.
Solution
(a) Explain briefly what is meant by the term abstract data type (ADT). Give two
reasons why use of ADTs is good programming practice.
A data type is a collection of values and a set of operations on those values. That collection and
these operations form a mathematical construct that may be implemented with the use of a
particular hardware or software data structure. The term abstract data type (ADT) refers to the
basic mathematical concept that defines the data type. We have discussed four different
implementations of the list data structure.
In case of implementation of the list with the use of an array, the size of the array gives difficulty
if increased.
To avoid this, we allocate memory dynamically for nodes before connecting these nodes with the
help of pointers.
For this purpose, we made a singly linked list and connected it with the next pointer to make a
chain.
Moving forward is easy but going back is a difficult task.
To overcome this problem, we made a doubly linked list using prev andnext pointers. With the
help of these pointers, we can move forward and backward very easily. Now we face another
problem that the prev pointer of first node and the next pointer of the last node are NULL.
Therefore, we have to be careful in case of NULL pointers. To remove the NULL pointers, we
made the circular link list by connecting the first and last node.
The program employing the list data structure is not concerned with its implementation.
We do not care how the list is being implemented whether through an array, singly linked list,
doubly linked list or circular linked list. It has been witnessed that in these four implementations
of the list, the interface remained the same i.e. it implements the same methods like add, get,
next, start a.
C++ is an enhanced version of the C language. C++ includes everything that is part of C language and adds support for object oriented programming (OOP). With very few, very major exceptions, C++ is a superset of C
good new report fun enjoyThe WiMAX (IEEE 802.16e) standard offers peak data rates of 128Mbps downlink and
56Mbps uplink over 20MHz wide channels whilst the new standard in development, 4G
WiMAN-Advanced (802.16m) is targeting the requirements to be fully 4G using 64Q QAM,
BPSK and MIMO technologies to reach the 1Gbps rate. It is predicted that in an actual
deployment, using 4X2 MIMO in an urban microcell application using a 20 MHz TDD
channel, the 4G WiMAN-Advanced system will be able to support 120Mbps downlink and
60Mbps uplink per site concurrently. WiMAX applications are already in use in many countries
globally but research in 2010 gave results that showed only just over 350 set ups were actually
in use. Many previous WiMAX operators were found to have moved to LTE along with Yota,
who were the largest WiMAX operator in the world.
Introduction: Data Types, Data structures
Types of Data Structures
Operations, ADTs
Algorithms, Comparison of Algorithms, Complexity, Time- space tradeoff.
Recursion: Introduction, format of recursive functions, recursion Vs. Iteration, examples.
Sorting: Quick sort, Merge Sort, Selection Sort
Pdf Covers-- Concept of OOPS, Introduction to Java, Java Features, History of Java, Naming of Java, Java version release, Structure of Java Program, Creating a Simple Java Program, Java Virtual Machine
Accelerate your Kubernetes clusters with Varnish CachingThijs Feryn
A presentation about the usage and availability of Varnish on Kubernetes. This talk explores the capabilities of Varnish caching and shows how to use the Varnish Helm chart to deploy it to Kubernetes.
This presentation was delivered at K8SUG Singapore. See https://feryn.eu/presentations/accelerate-your-kubernetes-clusters-with-varnish-caching-k8sug-singapore-28-2024 for more details.
JMeter webinar - integration with InfluxDB and GrafanaRTTS
Watch this recorded webinar about real-time monitoring of application performance. See how to integrate Apache JMeter, the open-source leader in performance testing, with InfluxDB, the open-source time-series database, and Grafana, the open-source analytics and visualization application.
In this webinar, we will review the benefits of leveraging InfluxDB and Grafana when executing load tests and demonstrate how these tools are used to visualize performance metrics.
Length: 30 minutes
Session Overview
-------------------------------------------
During this webinar, we will cover the following topics while demonstrating the integrations of JMeter, InfluxDB and Grafana:
- What out-of-the-box solutions are available for real-time monitoring JMeter tests?
- What are the benefits of integrating InfluxDB and Grafana into the load testing stack?
- Which features are provided by Grafana?
- Demonstration of InfluxDB and Grafana using a practice web application
To view the webinar recording, go to:
https://www.rttsweb.com/jmeter-integration-webinar
DevOps and Testing slides at DASA ConnectKari Kakkonen
My and Rik Marselis slides at 30.5.2024 DASA Connect conference. We discuss about what is testing, then what is agile testing and finally what is Testing in DevOps. Finally we had lovely workshop with the participants trying to find out different ways to think about quality and testing in different parts of the DevOps infinity loop.
UiPath Test Automation using UiPath Test Suite series, part 4DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 4. In this session, we will cover Test Manager overview along with SAP heatmap.
The UiPath Test Manager overview with SAP heatmap webinar offers a concise yet comprehensive exploration of the role of a Test Manager within SAP environments, coupled with the utilization of heatmaps for effective testing strategies.
Participants will gain insights into the responsibilities, challenges, and best practices associated with test management in SAP projects. Additionally, the webinar delves into the significance of heatmaps as a visual aid for identifying testing priorities, areas of risk, and resource allocation within SAP landscapes. Through this session, attendees can expect to enhance their understanding of test management principles while learning practical approaches to optimize testing processes in SAP environments using heatmap visualization techniques
What will you get from this session?
1. Insights into SAP testing best practices
2. Heatmap utilization for testing
3. Optimization of testing processes
4. Demo
Topics covered:
Execution from the test manager
Orchestrator execution result
Defect reporting
SAP heatmap example with demo
Speaker:
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Jeffrey Haguewood
Sidekick Solutions uses Bonterra Impact Management (fka Social Solutions Apricot) and automation solutions to integrate data for business workflows.
We believe integration and automation are essential to user experience and the promise of efficient work through technology. Automation is the critical ingredient to realizing that full vision. We develop integration products and services for Bonterra Case Management software to support the deployment of automations for a variety of use cases.
This video focuses on the notifications, alerts, and approval requests using Slack for Bonterra Impact Management. The solutions covered in this webinar can also be deployed for Microsoft Teams.
Interested in deploying notification automations for Bonterra Impact Management? Contact us at sales@sidekicksolutionsllc.com to discuss next steps.
Neuro-symbolic is not enough, we need neuro-*semantic*Frank van Harmelen
Neuro-symbolic (NeSy) AI is on the rise. However, simply machine learning on just any symbolic structure is not sufficient to really harvest the gains of NeSy. These will only be gained when the symbolic structures have an actual semantics. I give an operational definition of semantics as “predictable inference”.
All of this illustrated with link prediction over knowledge graphs, but the argument is general.
Search and Society: Reimagining Information Access for Radical FuturesBhaskar Mitra
The field of Information retrieval (IR) is currently undergoing a transformative shift, at least partly due to the emerging applications of generative AI to information access. In this talk, we will deliberate on the sociotechnical implications of generative AI for information access. We will argue that there is both a critical necessity and an exciting opportunity for the IR community to re-center our research agendas on societal needs while dismantling the artificial separation between the work on fairness, accountability, transparency, and ethics in IR and the rest of IR research. Instead of adopting a reactionary strategy of trying to mitigate potential social harms from emerging technologies, the community should aim to proactively set the research agenda for the kinds of systems we should build inspired by diverse explicitly stated sociotechnical imaginaries. The sociotechnical imaginaries that underpin the design and development of information access technologies needs to be explicitly articulated, and we need to develop theories of change in context of these diverse perspectives. Our guiding future imaginaries must be informed by other academic fields, such as democratic theory and critical theory, and should be co-developed with social science scholars, legal scholars, civil rights and social justice activists, and artists, among others.
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
computer notes - Circular list
1. ecomputernotes.com Data Structures Lecture No. 05
___________________________________________________________________
Data Structures
Lecture No. 05
In the previous lecture, we demonstrated the use of the circular list for the resolution
of the Josephus problem. After writing a program with the help of this data structure, a
leader among ten persons was selected. You must have noted many things while trying
to solve the problem. These things will help us to understand the usage of data
structures in C++, thus making the programming easy. The code of the program is
given below.
#include "CList.cpp"
void main(int argc, char *argv[])
{
CList list;
int i, N=10, M=3;
for(i=1; i <= N; i++ ) list.add(i);
list.start();
while( list.length() > 1 ) {
for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl;
list.remove();
}
cout << "leader is: " << list.get() << endl;
}
In the program, we include the file of the class CList and create its object i.e. list. Then
we solve the problem by using the add, start, length, next, remove and get methods of
the class CList.
In the program, we have included already-defined data structure CList. After defining
its different methods, we have an interface of Clist. There is no need to be worry about
the nature of the list i.e. whether it is linked list, doubly linked list or an array. For us,
it is only a list to be manipulated according to our requirement. You will see that a
programmer may use different methods of the list object to solve the problem. We add
elements to the list by a simple call of add method and go to the first element of the list
by start method. Here, the length method is used in the condition of the while loop.
Then we remove elements from the list and use the next, get and remove methods
during this process. We get the current element by using the get method, then remove
it by calling the remove method and then go to the next element by the method next.
This way, all the elements are removed from the list except one element, called the
leader. This one element remains there as we execute the while loop one less than the
length of the list.
Page 1 of 10
2. ecomputernotes.com Data Structures Lecture No. 05
___________________________________________________________________
In singly linked list, the next returns false when it reaches to the last node due to the
fact that the next field of the last node is set to NULL. But in a circularly linked list
there is no NULL. It will be there only when there is no node in the list.
The whole process, which we carried out to solve the Josephus problem, can also be
carried out with functions in C++. While adopting this way (of writing functions), we
have to write these functions whenever we write another program that manipulates a
list. In this method, we define a class of the data structure list and its different methods
for the purpose of manipulation. This way, this class, obviously its methods too, can be
used in any program where the manipulation of a list is needed. Thus there is re-
usability of the code. In a class, we encapsulate the data and its methods. This shows
that we are no longer interested in the internal process of the class. Rather, we simply
use it wherever needed. The circular linked list, earlier used for the resolution of the
Josephus problem, can also be employed in other problems. We have a class CList of
this circular linked list through which any number of objects of data type of circular
linked list can be created. Thus we can assume the class CList as a factory, creating as
many objects of list as needed. This class and its objects in any program can be used to
solve the problems with the help of its interface. The interface of this class consists of
some methods like add, remove, next, back, get and some other simple ones. While
carrying out programming, we will see that these classes (objects) help us very much
to solve different problems.
Benefits of using circular list
While solving the Josephus problem, it was witnessed that the usage of circular linked
list helped us make the solution trivial. We had to just write a code of some lines that
solved the whole problem. In the program, we included the class CList (which is of our
data structure i.e. circular linked list) and used all of its methods according to the
requirements. There was no problem regarding the working of the methods. We just
called these methods and their definition in the class CList worked well.
Now we will see what happens if we solve the Josephus problem by using an array
instead of the class in our program. In this case, we have to define an array and write
code to move back and forth in the array and to remove different elements properly in
a particular order. A programmer needs to be very careful while doing this, to reach
the solution of the problem. Thus our code becomes very complex and difficult for
someone to understand and modify it. Moreover we cannot use this code in some other
problem. Note that here we are talking about the use of an array in the main program,
not in the class that defines the CList data structure. There is no need to be worried
whether an array, singly linked list, doubly linked list is used or circular linked list
being employed internally in implementing the list in defining the class of list data type.
We only want that it should create objects of list. The usage of the class of a data
structure simplifies the code of the program. We can also use this class wherever
needed in other programs. This shows that the choice of appropriate data structures
can simplify an algorithm. It can make the algorithm much faster and efficient. In this
course, we will see that there are different data structures, which makes the algorithms
very easy to solve our problems. Later, we will see how some elegant data structures
lie at the heart of major algorithms. There is also a course dedicated to study different
algorithms and recipes that can be used to solve host of complex problems. Moreover,
Page 2 of 10
3. ecomputernotes.com Data Structures Lecture No. 05
___________________________________________________________________
we will study different data structures in detail and see that with the use of a proper
data structure, we can solve a problem efficiently. A properly constructed data
structure will always help in the solution of problems.
Abstract Data Type
A data type is a collection of values and a set of operations on those values. That
collection and these operations form a mathematical construct that may be
implemented with the use of a particular hardware or software data structure. The term
abstract data type (ADT) refers to the basic mathematical concept that defines the data
type. We have discussed four different implementations of the list data structure. In
case of implementation of the list with the use of an array, the size of the array gives
difficulty if increased. To avoid this, we allocate memory dynamically for nodes before
connecting these nodes with the help of pointers. For this purpose, we made a singly
linked list and connected it with the next pointer to make a chain. Moving forward is
easy but going back is a difficult task. To overcome this problem, we made a doubly
linked list using prev and next pointers. With the help of these pointers, we can move
forward and backward very easily. Now we face another problem that the prev pointer
of first node and the next pointer of the last node are NULL. Therefore, we have to be
careful in case of NULL pointers. To remove the NULL pointers, we made the circular
link list by connecting the first and last node.
The program employing the list data structure is not concerned with its
implementation. We do not care how the list is being implemented whether through an
array, singly linked list, doubly linked list or circular linked list. It has been witnessed
that in these four implementations of the list, the interface remained the same i.e. it
implements the same methods like add, get, next, start and remove etc. This proves
that with this encapsulation attained by making a class, we are not concerned with its
internal implementation. The implementation of these abstract data types can be
changed anytime. These abstract data types are implemented using classes in C++. If
the list is implemented using arrays while not fulfilling the requirements, we can change
the list implementation. It can be implemented with the use of singly-link list or doubly
link list. As long as the interface is same, a programmer can change the internal
implementation of the list and the program using this list will not be affected at all. This
is the abstract data type (ADT). What we care about is the methods that are available
for use, with the List ADT i.e. add, get, and remove etc methods. We have not studied
enough examples to understand all the benefits of abstract data types. We will follow
this theme while developing other ADT. We will publish the interface and keep the
freedom to change the implementation of ADT without effecting users of the ADT.
The C++ classes provide a programmer an ability to create such ADTs. What benefits
can we get with the help of these ADTs and classes? When we develop an ADT or a
class or factory then the users of this factory are independent of how this factory
works internally. Suppose that we have ordered the car factory (car class) to produce a
new car and it replies after a long time. If we ordered the remove method to remove
one node and we are waiting and it keeps on working and working. Then we might
think that its implementation is not correct. Although, we are not concerned with the
internal implementation of this ADT yet it is necessary to see whether this ADT is
useful for solving our problem or not. It should not become a bottleneck for us. If the
method we are using is too much time consuming or it has some problem in terms of
Page 3 of 10
4. ecomputernotes.com Data Structures Lecture No. 05
___________________________________________________________________
algorithm used. On one side, we only use the interfaces provided by these ADTs,
classes, or factories as long as they do what they promise. We are not concerned with
the internal details. On the other hand, we have to be careful that these factories or
methods should not take too much time so that these will not be useful for the
problem.
This distinction will always be there. Sometimes, the source code of classes is not
provided. We will be provided libraries, as standard libraries are available with the
compiler. These classes are in compiled form i.e. are in object form or in binary form.
On opening these files, you will not see the C++ code, rather binary code. When you
read the assembly language code, it will give some idea what this binary code is about.
You can view the interface methods in the .h file. As an application programmer, you
have to see that the ADTs being used are written in a better way. The point to be
remembered here is that you should not worry about the internal implementation of
these ADTs. If we want to change the internal implementation of the ADTs, it can be
done without affecting the users of these ADTs. W hile writing a program, you should
check its performance. If at some point, you feel that it is slow, check the ADTs used
at that point. If some ADT is not working properly, you can ask the writer of the ADT
to change the internal implementation of that ADT to ensure that it works properly.
Stacks
Let s talk about another important data structure. You must have a fair idea of stacks.
Some examples of stacks in real life are stack of books, stack of plates etc. We can add
new items at the top of the stack or remove them from the top. We can only access the
elements of the stack at the top. Following is the definition of stacks.
Stack is a collection of elements arranged in a linear order
Let s see an example to understand this. Suppose we have some video cassettes. We
took one cassette and put it on the table. We get another cassette and put it on the top
of first cassette. Now there are two cassettes on the table- one at the top of other.
Now we take the third cassette and stack it on the two. Take the fourth cassette and
stack it on the three cassettes.
Now if we want to take the cassette, we can get the fourth cassette which is at the top
and remove it from the stack. Now we can remove the third cassette from the stack
and so on. Suppose that we have fifty cassettes stacked on each other and want to
access the first cassette that is at the bottom of the stack. What will happen? All the
cassettes will fell down. It will not happen exactly the same in the computer. There
may be some problem. It does not mean that our data structure is incorrect. As we see
in the above example that the top most cassette will be removed first and the new
cassette will be stacked at the top. The same example can be repeated with the books.
In the daily life, we deal with the stacked goods very carefully.
Now we will discuss how to create a stack data structure or a factory, going to create
stack object for us. What will be the attributes of this object? During the discussion on
the list, we came to know that a programmer adds values in the list, removes values
from the list and moves forward and backward. In case of a stack too, we want to add
Page 4 of 10
5. ecomputernotes.com Data Structures Lecture No. 05
___________________________________________________________________
things and remove things. We will not move forward or backward in the stack. New
items can be added or removed at the top only. We can not suggest the removal of the
middle element of the stack.
Let s talk about the interface methods of the stacks. Some important methods are:
Method Name Description
push(x) Insert x as the top element of the stack
pop() Remove the top element of the stack and return it.
top() Return the top element without removing it from the stack.
The push(x) method will take an element and insert it at the top of the stack. This
element will become top element. The pop() method will remove the top element of the
stock and return it to the calling program. The top() method returns the top-most stack
element but does not remove it from the stack. The interface method names that we
choose has special objective. In case of list, we have used add, remove, get, set as the
suitable names. However, for stack, we are using push, pop and top. We can depict the
activity from the method name like push means that we are placing an element on the
top of the stack and pushing the other elements down.
The example of a hotel s kitchen may help understand the concept of stacks in a
comprehensive manner. In the kitchen, the plates are stacked in a cylinder having a
spring on the bottom. When a waiter picks a plate, the spring moves up the other
plates. This is a stack of plates. You will feel that you are pushing the plates in the
cylinder and when you take a plate from the cylinder it pops the other plates. The top
method is used to get the top- most element without removing it.
When you create classes, interfaces and methods, choose such names which depicts
what these method are doing. These names should be suitable for that class or factory.
Let s discuss the working of stack with the help of a diagram.
Page 5 of 10
6. ecomputernotes.com Data Structures Lecture No. 05
___________________________________________________________________
top 1
top 7 7
top 5 5 5
top 2 2 2 2
push(2) push(5) push(7) push(1)
top 21
top 7 7 top 7
5 5 5 top 5
2 2 2 2 top 2
1 pop() push(21) 21 pop() 7 pop() 5 pop()
At the start, the stack is empty. First of all, we push the value 2 in the stack. As a
result, the number 2 is placed in the stack. We have a top pointer that points at the top
element. Then we said push(5). Now see how 2 and 5 are stacked. The number 5 is
placed at the top of number 2 and the pointer top moves one step upward. Then we
pushed the number 7 which is placed on the top and the number 2 and 5 are below.
Similarly, we push number 1. The last figure in the first row shows the stacked values
of the numbers- 1, 7, 5 and 2.
Let s pop the elements from the stack. The first figure of second row shows the pop
operation. As a result, the number 1 is popped. Than again we push the number 21 on
the stack. The number 7, 5, and 2 are already in the stack and number 21 is pushed at
the top. If we pop now, the number 21 is popped. Now number 7 is at the top. If we
pop again, the number 7 is popped. Pop again and the number 5 is popped and number
2 remains in the stack. Here with the help of this diagram, we are proving that the
values are added at the top and removed at the top in a stack.
The last element to go into the stack is the first to come out. That is why, a stack is
known as LIFO (Last In First Out) structure. We know that the last element pushed in
the stack is at the top which is removed when we call pop. Let s see some other
scenarios. What happens if we call pop() while there is no element? One possible way-
out is that we have isEmpty() function that returns true if stack is empty and false
otherwise. This is a boolean function that returns false if there is no element in the
stack. Otherwise, it will return true. The second option is this that when we call pop on
an empty stack, it throws an exception. This is a concept of advanced C++. Exception
is also a way to convey that some unusual condition has arisen or something has gone
wrong. Suppose, if we have a division method and try to divide some number with
Page 6 of 10
7. ecomputernotes.com Data Structures Lecture No. 05
___________________________________________________________________
zero. This method will throw division by zero exception. Currently we will not throw
an exception but use the isEmpty() method. The user who is employing the stack is
responsible to call the isEmpty() method before calling the pop. Call the pop method if
isEmpty() returns false . Otherwise, there will be a problem.
Stack Implementation using array
Let s discuss the implementation of the stack. Suppose we implement the stack using
the arrays. The stack shown in the above diagram may be considered as an array. Here
the array is shown vertically. We can implement the stack using array. The interface
will remain as push and pop methods. The user of the stack does not need to know
that the stack is internally implemented with the help of array. The worst case for
insertion and deletion from an array may happen when we insert and delete from the
beginning of the array. We have to shift elements to the right for insertion and left for
removal of an element. We face the same problem while implementing the list with the
use of the array. If we push and pop the elements from the start of the array for stack
implementation, this problem will arise. In case of push, we have to shift the stack
elements to the right. However, in case of pop, after removing the element, we have
to shift the elements of stack that are in the array to the left. If we push the element at
the end of the array, there is no need to shift any element. Similarly as the pop method
removes the last element of the stack which is at the end of the array, no element is
shifted. To insert and remove elements at the end of the array we need not to shift its
elements. Best case for insert and delete is at the end of the array where there is no
need to shift any element. We should implement push() and pop() by inserting and
deleting at the end of an array.
top 1
2 5 7 1
7
5 0 1 2 3 4
2 top = 3
In the above diagram, on the left side we have a stack. There are four elements in the
stack i.e. 1, 7, 5 and 2. The element 1 is the extreme-most that means that it is inserted
in the end whereas 7, 5, and 2 have been added before. As this is a LIFO structure so
the element 1 should be popped first. On the right side we have an array with positions
0, 1, 2, 3 and so on. We have inserted the numbers 2, 5, 7 and 1. We have decided that
the elements should be inserted at the end of the array. Therefore the most recent
element i.e. 1 is at position 3. The top is the index representing the position of the most
recent element. Now we will discuss the stack implementation in detail using array.
We have to choose a maximum size for the array. It is possible that the array may fill-
up if we push enough elements. Now more elements cannot be pushed. Now what
should the user of the stack do? Internally, we have implemented the stack using array
which can be full. To avoid this, we write isFull() method that will return a boolean
Page 7 of 10
8. ecomputernotes.com Data Structures Lecture No. 05
___________________________________________________________________
value. If this method returns true, it means that the stack (array) is full and no more
elements can be inserted. Therefore before calling the push(x), the user should call
isFull() method. If isFull() returns false, it will depict that stack is not full and an
element can be inserted. This method has become the part of the stack interface. So we
have two more methods in our interface i.e. isEmpty() and isFull().
Now we will discuss the actual C++ code of these operations. These methods are part
of stack class or stack factory. We have an array named A while current is its index.
The code of pop() method is as:
int pop()
{
return A[current--];
}
In this method, the recent element is returned to the caller, reducing the size of the
array by 1.
The code of push method is:
void push(int x)
{
A[++current] = x;
}
We know that ++current means that add one to the current and then use it. That also
shows that element x should be inserted at current plus one position. Here we are not
testing that this current index has increased from the array size or not. As discussed
earlier that before using the push method, the user must call isFull() method. Similarly
it is the responsibility of the user to call the isEmpty() method before calling the pop
method. Therefore there is no if statement in the push and pop method.
The code of the top() method is:
int top()
{
return A[current];
}
This method returns the element at the current position. We are not changing the value
of current here. We simply want to return the top element.
int isEmpty()
{
return ( current == -1 );
}
This method also tests the value of the current whether it is equal to -1 or not. Initially
when the stack is created, the value of current will be -1. If the user calls the isEmpty()
Page 8 of 10
9. ecomputernotes.com Data Structures Lecture No. 05
___________________________________________________________________
method before pushing any element, it will return true.
int isFull()
{
return ( current == size-1);
}
This method checks that the stack is full or not. The variable size shows the size of the
array. If the current is equal to the size minus one, it means that the stack is full and we
cannot insert any element in it.
We have determined the cost and benefit of all the data structures. Now we will see
how much time these methods take. A quick examination shows that all the five
operations take constant time. In case of list, the find method takes too much time as it
has to traverse the list. Whereas the add and remove methods are relatively quick. The
methods of stack are very simple. There is no complexity involved. We insert element
at one side and also remove from that side not in the middle or some other place.
Therefore we need not to carry out a lot of work. During the usage of the array, the
stack methods push, pop, top, isFull and isEmpty all are constant time operations.
There is not much difference of time between them.
The complete code of the program is:
/* Stack implementation using array */
#include <iostream.h>
/* The Stack class */
class Stack
{
public:
Stack() { size = 10; current = -1;} //constructor
int pop(){ return A[current--];} // The pop function
void push(int x){A[++current] = x;} // The push function
int top(){ return A[current];} // The top function
int isEmpty(){return ( current == -1 );} // Will return true when stack is empty
int isFull(){ return ( current == size-1);} // Will return true when stack is full
private:
int object; // The data element
int current; // Index of the array
int size; // max size of the array
int A[10]; // Array of 10 elements
};
// The main method
int main()
{
Stack stack; // creating a stack object
Page 9 of 10
10. ecomputernotes.com Data Structures Lecture No. 05
___________________________________________________________________
// pushing the 10 elements to the stack
for(int i = 0; i < 12; i++)
{
if(!stack.isFull()) // checking stack is full or not
stack.push(i); // push the element at the top
else
cout <<"n Stack is full, can't insert new element";
}
// pop the elements at the stack
for (int i = 0; i < 12; i++)
{
if(!stack.isEmpty()) // checking stack is empty or not
cout << "n The popped element = " << stack.pop();
else
cout <<"n Stack is empty, can't pop";
}
}
The output of the program is:
Stack is full, can't insert new element
Stack is full, can't insert new element
The popped element = 9
The popped element = 8
The popped element = 7
The popped element = 6
The popped element = 5
The popped element = 4
The popped element = 3
The popped element = 2
The popped element = 1
The popped element = 0
Stack is empty, can't pop
Stack is empty, can't pop
However, a programmer finds the size-related problems in case of an array. What
should we do when the array is full? We can avoid the size limitation of a stack
implemented with an array by using a linked list to hold the stack elements. Further
discussion on this issue will be made in the next lecture.
Page 10 of 10