Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Stack and Queue

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 16

Stack And Queue

Stack
A stack is a Last in First Out dynamic data structure. It has 2 important operations namely Push and Pop. The push operation adds data to the top of the list, hiding any items already on the stack. The pop operation removes any data from the top of the list. Items are added or removed from the top of the stack. It is a dynamic data structure since the size varies according to the number of elements currently in the stack.

txttop

cmd push cmd pop

txtelement Liststack

The stack is empty. To add data in the stack, the push button is operated. Once the Push button is clicked, an input box appears for the user to enter new data item as displayed below. For example; the user enters John as a new data.

When a new item is entered in the stack, the value for the Top and the value for the Element changes. Once five data has already been inserted in the stack items, a message box is displayed indicating Stack is full when the push button is applied.

When there are five items in the stack. Then to remove data from the stack, the Pop button is applied. On clicking on this button, the last data i.e. Christian is popped out of the stack items. The Top and Element Value becomes Four.

When there are no items in the stack, the value for the Top and the value for the Element becomes zero. Once five data has already been popped out of the stack items, a message box is displayed indicating Stack is empty when the pop button is applied as shown below.

Programming Code For Stack

Option Compare Database Const maxsize = 5 Dim stack(1 To maxsize) As String * 10 Dim Top, Element As Integer

Private Sub Form_Load() Do While (liststack.ListCount > 0) liststack.RemoveItem (0) Loop Element = 0 Top = 0 txttop.Value = Top txt_element = Element End Sub

Private Sub cmdpush_Click() If Top = maxsize Then MsgBox ("Stack is full") Else Top = Top + 1 stack(Top) = InputBox("Enter New Data Item") liststack.AddItem stack(Top) txttop.Value = Top Element = Element + 1 txt_element = Element End If End Sub Private Sub cmdpop_Click() If Top = 0 Then MsgBox ("Stack is empty") Else MsgBox ("Deleted Item =" & stack(Top)) Top = Top - 1 Element = Element - 1 End If End Sub

Queue
A queue is a First in First out data structure. It has 2 functions namely Enqueue and Dequeue. Enqueue adds new elements to the rear of the queue and Dequeue removes elements from the front of the queue. txtF

cmdenqueue txtR cmddequeue

Txt_ no_in_queue

listqueue

To add a new item to the queue, the enqueue operation is used. On clicking on the Enqueue button, an input box appears for the user to enter new data as shown below. For example the user can enter Football as a new data.

The maximum size for queue has been defined as six .Thus only 6 items can be entered in this queue. In case the user clicks on the ENQUEUE button, a message box appears as shown below indicating Queue is full.

10

To remove an item from the queue, the dequeue operation is used. On clicking on the Dequeue button, a message appears indicating the first data has been removed from the queue i.e. Football is deleted.

11

The Front value becomes one, No In Queue becomes six but Rear remains the same as shown below.

When no item is present in the queue and the Dequeue button is applied then a message box appears indicating Queue is empty.

12

Now when we enter data, the Front value becomes six with Rear and No In Queue becoming one.

13

Programming Code For Queue


Option Compare Database Const maxsize = 6 Dim Q(1 To maxsize) As String * 10 Dim Front, Rear, No_In_Queue As Integer Dim RemovedItem As String * 10 Private Sub Form_Load() Front = 0 Rear = 6 No_In_Queue = 6 txtF.Value = Front txtR.Value = Rear txtno_in_queue.Value = No_In_Queue End Sub

14

Private Sub cmdenqueue_Click() If No_In_Queue = maxsize Then MsgBox ("Queue is full") Else If Rear = maxsize Then Rear = 1 Else Rear = Rear + 1 End If txtR.Value = Rear Q(Rear) = InputBox("Enter New Data") Listqueue.AddItem Q(Rear) No_In_Queue = No_In_Queue + 1 txtno_in_queue.Value = No_In_Queue End If End Sub

15

Private Sub cmddequeue_Click()

If No_In_Queue = 0 Then MsgBox ("Queue is empty") Else RemovedItem = Q(Front) Listqueue.RemoveItem (0) MsgBox ("RemovedItem =" & Q(Front)) If Front = 6 Then Front = 1 Else Front = Front + 1 End If txtF.Value = Front No_In_Queue = No_In_Queue - 1 txtno_in_queue.Value = No_In_Queue End If End Sub

16

You might also like