Download Stack

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Array data structure wikipedia , lookup

Transcript
Algorithms and data structures
30.4.2017.
Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/
Creative Commons

You are free to:
share — copy and redistribute the material in any medium or format
 adapt — remix, transform, and build upon the material


Under the following terms:



Attribution — You must give appropriate credit, provide a link to the license, and
indicate if changes were made. You may do so in any reasonable manner, but
not in any way that suggests the licensor endorses you or your use.
NonCommercial — You may not use the material for commercial purposes.
ShareAlike — If you remix, transform, or build upon the material, you must
distribute your contributions under the same license as the original.
No additional restrictions — You may not apply legal terms or technological
measures that legally restrict others from doing anything the license permits.
Notices:
You do not have to comply with the license for elements of the material in the public domain or where your use is
permitted by an applicable exception or limitation.
No warranties are given. The license may not give you all of the permissions necessary for your intended use. For
example, other rights such as publicity, privacy, or moral rights may limit how you use the material.
Text copied from http://creativecommons.org/licenses/by-nc-sa/3.0/
Algorithms and data structures, FER
30.4.2017.
2 / 21
Stack
30.4.2017.
Stack


Data structure where the last stored record is processed first
Required operations:
Addition (push) of elements at the top of the stack
 Removing (pop) of elements from the top of the stack
 Initialisation of an empty stack
A single operation push or pop requires an equal execution time, regardless
of the number of already stored elements



The case when the stack is full may require the allocation of
additional memory and repeated execution of the program

An empty stack does not necessarily imply an error
Algorithms and data structures, FER
30.4.2017.
4 / 20
Stack – array implementation

It can be implemented using a static data structure



According to the principle Last In First Out (LIFO), elements are added to or removed from
the one-dimensional array of a given structure
The value marking the top position of the stack is updated MAXSTOG=5
MAXSTACK=5
Initialisation of an empty stack:

4
Resetting the position of the top
#define MAXSTACK 5
typedef struct {
int top, array[MAXSTACK];
} Stack;
void init_stack(Stack *stack){
stack->top = -1;
}
3
2
1
0
stack->top -1
 StogPoljem (StackByArray)
Algorithms and data structures, FER
30.4.2017.
5 / 20
Push an element on the stack
4
3
2
1
0
stack->top
1
-4
7
2
5
int push(int element, Stack *stack) {
if (stack->top>= MAXSTACK-1) return 0;
stack->top++;
stack->array[stack->top] = element;
return 1;
}
Calling program:
Stack stack;
init_stack(&stack);
push(5, &stack);
push(2, &stack);
push(7, &stack);
push(-4, &stack);
push(1, &stack);
push(9, &stack);
-1
Determine the complexity!
O(1)
Algorithms and data structures, FER
30.4.2017.
6 / 20
Pop an element from the stack
stack->top
4
3
2
1
0
1
-4
7
2
5
int pop (int *element, Stack *stack) {
if (stack->top < 0) return 0;
*element = stack->array[stack->top];
stack->top--;
return 1;
}
Calling program:
pop(&element,
pop(&element,
pop(&element,
pop(&element,
pop(&element,
pop(&element,
-1
Determine the complexity!
O(1)
Algorithms and data structures, FER
30.4.2017.
&stack);
&stack);
&stack);
&stack);
&stack);
&stack);
7 / 20
Exercises

Write the function that pops elements from a stack implemented with
a static array. The single element is a floating point number. If the
operation failed, the function returns 0, otherwise 1.

Write the function that pushes elements on a stack imlemented with
a static array with maximum capacity of MAXR records. A record to
be stored on stack consists of an integer and 10 floating point
numbers. If the operation failed, the function returns 0, otherwise 1.
The function prototype is
int push(struct node element, Stack *stack);
Algorithms and data structures, FER
30.4.2017.
8 / 20
Exercises

There is an unformatted file stack.dat on disk, organised as a stack. At
the file beginning it is written the maximum allowed capacity of the stack,
expressed as the number of records (int) and the address of the last element
written on the stack (long). An element on the stack is a record with the
passed examination data for a student:
 ID number (long)
Name and family name (24+1 character)
 Code of the course (int)
 Grade (short)


Define the data type Stack and write the necessary functions for
handling the stack. As the main program use the main from
StogPoljem.c (StackByArray.c)
Algorithms and data structures, FER
30.4.2017.
9 / 20
Lists
30.4.2017.
Basic terms
Linear list A=(a1,a2,...an)is a data structure
consisting of an ordered sequence of elements, selected
from a data set
 A linear list is empty if it contains n=0 elements
 List elements ai are called atoms


A list can be implemented using the static data structure array
Algorithms and data structures, FER
30.4.2017.
11 / 20
List implementation
The dynamic data structure to implement a list consists of a pointer to the first
list element and of an arbitrary number of atoms
 Each atom consists of a data part and a pointer to the next list element
 For each list atom, the memory is allocated at the moment when needed to
store data, and it is released when data are deleted
 Granulation is of the size atom
atom
atom

struct at {
int element;
struct at *next;
};
typedef struct at atom;
Algorithms and data structures, FER
element
next
30.4.2017.
12 / 20
Empty and non empty list
Empty list
head
Non empty list
head
52
Algorithms and data structures, FER
42
30.4.2017.
13 / 20
Stack – list implementation

Stack, earlier implemented with an array, can also be implemented with a linear
list


Insertion to and deletion from the list is performed on the same end of the list
Head of the list serves as the top of the stack
struct at {
type element;
struct at *next;
};
typedef struct at atom;
typedef struct{
atom *top;
} Stack;
void init_stack(Stack *stack){
stack->top = NULL;
}
stack->top
 StogListom (StackByList)
Algorithms and data structures, FER
30.4.2017.
14 / 20
Stack – list implementation (push element)
int push(type element, Stack *stack) {
atom *new;
if ((new = (atom*) malloc (sizeof (atom))) != NULL) {
new->element = element;
new->next = stack->top;
stack->top = new;
return 1;
}
else return 0;
}
Calling program:
Stack stack;
init_stack (&stack);
push (5, &stack);
Algorithms and data structures, FER
stack->top
new
5
30.4.2017.
15 / 20
Stack – list implementation (push new element)
int push (type element, Stack *stack) {
atom *new;
if ((new = (atom*) malloc (sizeof (atom))) != NULL) {
new->element = element;
new->next = stack->top;
stack->top = new;
return 1;
}
else return 0;
}
Calling program:
Stack stack;
init_stack(&stack);
push (5, &stack);
push (3, &stack);
Algorithms and data structures, FER
stack->top
Complexity?
new
3
5
O(1)
30.4.2017.
16 / 20
Stack – list implementation (pop element) - I
int pop (type *element, Stack *stack) {
atom *aux;
if (stack->top == NULL) return 0;
*element = stack->top->element;
aux = stack->top->next;
/* address of the new top */
free (stack->top);
/* release the old top */
stack->top = aux;
/* set the new top */
return 1;
}
Calling program:
pop (&element, &stack);
aux
stack->top
3
Algorithms and data structures, FER
30.4.2017.
5
17 / 20
Stack – list implementation (pop element) - II
int pop (type *element, Stack *stack) {
atom *aux;
if (stack->top == NULL) return 0;
*element = stack->top->element;
aux = stack->top->next;
/* address of the new top */
free (stack->top);
/* release the old top */
stack->top = aux;
/* set the new top */
return 1;
}
Calling program:
pop (&element, &stack);
pop (&element, &stack);
Algorithms and data structures, FER
aux
stack->top
5
30.4.2017.
18 / 20
Stack – list implementation (pop element from empty stack)
int pop (type *element, Stack *stack) {
atom *aux;
if (stack->top == NULL) return 0;
*element = stack->top->element;
aux = stack->top->next;
/* address of the new top */
free (stack->top);
/* release the old top */
stack->top = aux;
/* set the new top */
return 1;
}
Calling program:
pop (&element, &stack);
pop (&element, &stack);
pop (&element, &stack);
aux
stack->top
Complexity?
Algorithms and data structures, FER
30.4.2017.
O(1)
19 / 20
Memory usage

Representation of stack using a list requires more memory per data
(because a pointer is surplus)




More flexibility is achieved
Multiple stacks can simultaneously use the same memory space
Memory usage is proportional to the size of data on the stack, not
determined by the maximum stack capacity
On the other hand, capacity of a single stack is limited only by the
available memory
Algorithms and data structures, FER
30.4.2017.
20 / 20