In Computer Science

Published on February 2017 | Categories: Documents | Downloads: 91 | Comments: 0 | Views: 735
of 3
Download PDF   Embed   Report

Comments

Content

 

 

a stack 

In computer science, a stack is a last in, first out (LIFO) abstract data type and dat structure. A stack can have any abstract data type as an element, but is characterized by only two fundamental operations:  push and and pop  pop.. The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the list, and returns this value to the caller. A  pop either reveals previously concealed items, or results in an empty list.

Example in C #include<stdio.h>   int main() { int a[100], i; printf("To pop enter -1\n"); for(i = 0;;) { printf("Push "); scanf("%d", &a[i]); if(a[i] == -1) { if(i == 0) { printf("Underflow\n"); } else { printf("pop = %d\n", a[--i]); } } else { i++; }}}

 

algorithem: 1.push operation: if top of the stack is greater than equal to maximum number of entries into the stack, then print "Stack is already full.Cannot add more items." top of stack = t

increment the top of the stack  2.pop operation: decrement top of the stack  if top <0,then print "Stack is already empty.Noitems to read."

3.call these push & pop functions declared void in the main function using the switch statement.

4.The variable top & symbolic constant stack maximum items entry number has to be declared.

Procedure:  PUSH (STACK, TOP, MAXSTK, ITEM) This procedure pushes an ITEM onto a stack. 1. [Stack already filled?] If TOP = MAXSTK, then: Print: OVERFLOW, and Return. 2. Set TOP: =TOP+1. [Increases TOP by 1.] 3. Set ST ACK [TOP]: = ITEM. [Inserts ITEM in new TOP position 4.return Procedure: POP (STACK, TOP, ITEM) This procedure deletes the top element of STACK and assigns it to the variable ITEM. 1. [Stack has an item to be removed?] If TOP = 0, then: Print: UNDERFLOW, and Return. 2. Set ITEM: = STACK [TOP]. [Assigns TOP element to ITEM.] 3. Set TOP: = TOP - 1. [Decreases TOP by 1.] 4. Return

 

Selection Sort (one of the simplest sorting algorithms) 3 4 10 6 8 9 7 2 1 5 Search through the array, find the largest value, (10) and exchange it with the value stored in the last array location (5)

Void SelectionSort (Item A[], int left, int right) { int i, j; for (i = left; i < right; i++) { int min = i; for (j = i+1; j <= right; j++) if (less(A[j], A[min]) min = j; exch(A[i], A[min]); } }

selection sort Algorithm Selection sort(A,N) 1. Repeat steps2 and3 for K=1,2,«.,N-1: 2. Call MIN(A,K,N,LOC) MIN(A,K,N,LOC) 3. Interchange A[K] and A[LOC] set TEMP=A[K],A[K]=A[LOC],A[L TEMP=A[K ],A[K]=A[LOC],A[LOC]=TEMP OC]=TEMP end of step 1 loop 4.Exit

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close