Examples of C Programming on Stacks and Queues

Only available on StudyMode
  • Topic: Linked list, Data structure, Reference
  • Pages : 58 (7002 words )
  • Download(s) : 278
  • Published : July 12, 2011
Open Document
Text Preview
CONTENTS
* I. ASSUMPTIONS
* II. INTRODUCTION
* III. STACKS
* IV. QUEUES
* V. LINKED LISTS
* VI. STACKS USING LINKED LISTS
* VII. QUEUES USING LINKED LISTS
* VIII. CIRCULAR QUEUES
* IX. BINARY SEARCH TREES
* X. CONTACT ME
I. ASSUMPTIONS

In this Tutorial I assume that all of you have a working knowledge on how to use Classes in C++ because all of my data structures are going to be based on them. I realized that there are a lot of Data Structures Tutorials available but it's rare to find one that uses OOP. So this one will mainly focus on having a data structure incorporated as a class.

CODE IS COMPILED IN BORLAND C++ UNLESS OTHERWISE MENTIONED.

II. INTRODUCTION

We shall cover the following Basic Data Structures in this Tutorial:

1. STACKS
2. QUEUES
3. LINKED LISTS
4. BINARY TREES

We shall also combine data structures together later in this tutorial such as combining a Linked List along with a Stack etc. We shall also learn about Doubly Linked Lists and Circular Linked Lists in this Tutorial.

So let's begin without wasting any time.

III. STACKS

Stacks are commonly used Data Structures while writing code. It's concept is really simple which makes it even simpler to write it in code. Consider this situation. There are a pile of 5 Books on a Table. You want to add one book to the pile. What do you do??? You simply add the book on the TOP of the pile. What  if you want the third book from the new 6 book pile? You then lift each book one by one from the TOP until the third book reaches the top. Then you take the  third book and replace all the others back into the pile by adding them from the  TOP.

If you've noticed I've mentioned the word TOP in Caps. Yes, TOP is the most  important word as far as stacks are concerned. Data is stored in a Stack where  adding of data is permitted only from the top. Removing/Deleting Data is also done from the top. As Simple as That. Now you may ask where Stacks are used? Stacks are in fact used on every Processor. Each processor has a stack where data and addresses are pushed or added to the stack. Again the TOP rule is followed here. The ESP Register adds as a Stack Pointer that refers to the top of the stack in the Processor. Anyway, since the explanation of how the Processor Stack works is beyond the subject of this Tutorial, let's write our Stack Data Structure. Remember some Stack Terminology before continuing. Adding Data to the Stack is known as Pushing and deleting data from the stack is known as Popping.

01| #include <iostream>|
02|  |

03| using namespace std;|
04|  |

05| #define MAX 10        // MAXIMUM STACK CONTENT| 06|  |

07|  |
08| class stack|

09| {|
10|  |

11|   private:|
12|     int arr[MAX];   // Contains all the Data|

13|     int top;        //Contains location of Topmost Data pushed onto Stack| 14|  |

15|   public:|
16|      stack()         //Constructor|

17|      {|
18|         top=-1;      //Sets the Top Location to -1 indicating an empty stack|

19|      }|
20|  |

21|      void push(int a)  // Push ie. Add Value Function| 22|      {|

23|         top++;        // increment to by 1| 24|         if(top<MAX)|

25|          {|
26|             arr[top]=a;  //If Stack is Vacant store Value in Array|

27|          }|
28|          else|

29|          {|
30|             cout<<"STACK FULL!!"<<endl;|

31|             top--;|
32|          }|

33|      }|
34|  |

35|     int pop()                  // Delete Item. Returns the deleted item| 36|     {|

37|         if(top==-1)|
38|         {|

39|             cout<<"STACK IS EMPTY!!!"<<endl;| 40|             return NULL;|

41|         }|
42|         else|

43|         {|
44|             int data=arr[top];     //Set Topmost Value in data|...
tracking img