# Algorithms and data structures Protected by 7.6.2014.

## Presentation on theme: "Algorithms and data structures Protected by 7.6.2014."— Presentation transcript:

Algorithms and data structures Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/ 7.6.2014

7.6.2014 Stack

Algorithms and data structures, FER4 / 20 7.6.2014Stack n Data structure where the last stored record is processed first n Required operations: Addition ( push ) of elements at the top of the stack Addition ( push ) of elements at the top of the stack Removing ( pop ) of elements from the top of the stack Removing ( pop ) of elements from the top of the stack Initialisation of an empty 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 A single operation push or pop requires an equal execution time, regardless of the number of already stored elements n 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 An empty stack does not necessarily imply an error

Algorithms and data structures, FER5 / 20 7.6.2014 MAXSTOG=5 Stack – array implementation n 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 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 The value marking the top position of the stack is updated n Initialisation of an empty stack: Resetting the position of the top Resetting the position of the top StogPoljem (StackByArray) #define MAXSTACK 5 typedef struct { int top, array[MAXSTACK]; } Stack; void init_stack(Stack *stack){ stack->top = -1; } stack->top 0 1 2 3 4 MAXSTACK=5

Algorithms and data structures, FER6 / 20 7.6.2014 Push an element on the stack int push(int element, Stack *stack) { if (stack->top>= MAXSTACK-1) return 0; stack->top++; stack->array[stack->top] = element; return 1; } Determine the complexity! 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); stack->top 0 1 2 3 4 5 2 7 -4 1 O(1)

Algorithms and data structures, FER7 / 20 7.6.2014 Pop an element from the stack 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, &stack); Determine the complexity! stack->top 0 1 2 3 4 5 2 7 -4 1 O(1)

Algorithms and data structures, FER8 / 20 7.6.2014Exercises n 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 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, FER9 / 20 7.6.2014Exercises 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: 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 ) ID number ( long ) Name and family name (24+1 character) Name and family name (24+1 character) Code of the course ( int ) Code of the course ( int ) Grade ( short ) 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) 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)

7.6.2014 Lists

Algorithms and data structures, FER11 / 20 7.6.2014 Basic terms Linear list A=(a 1,a 2,...a n ) is a data structure consisting of an ordered sequence of elements, selected from a data set Linear list A=(a 1,a 2,...a n ) 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 A linear list is empty if it contains n=0 elements List elements a i are called atoms List elements a i are called atoms n A list can be implemented using the static data structure - array

Algorithms and data structures, FER12 / 20 7.6.2014 List implementation n The dynamic data structure to implement a list consists of a pointer to the first list element and of an arbitrary number of atoms n Each atom consists of a data part and a pointer to the next list element n For each list atom, the memory is allocated at the moment when needed to store data, and it is released when data are deleted n Granulation is of the size atom struct at { int element; struct at *next; }; typedef struct at atom; next element atom

Algorithms and data structures, FER13 / 20 7.6.2014 Empty and non empty list Empty list 5242 Non empty list head

Algorithms and data structures, FER14 / 20 7.6.2014 Stack – list implementation n 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 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 Head of the list serves as the top of the stack StogListom (StackByList) 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

Algorithms and data structures, FER15 / 20 7.6.2014 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); stack->top 5 new

Algorithms and data structures, FER16 / 20 7.6.2014 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); stack->top 5 3 new Complexity? O(1) Stack – list implementation (push new element)

Algorithms and data structures, FER17 / 20 7.6.2014 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); stack->top 5 3 aux Stack – list implementation (pop element) - I

Algorithms and data structures, FER18 / 20 7.6.2014 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); stack->top 5 aux Stack – list implementation (pop element) - II

Algorithms and data structures, FER19 / 20 7.6.2014 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); stack->top aux Complexity? O(1)

Algorithms and data structures, FER20 / 20 7.6.2014 Memory usage n Representation of stack using a list requires more memory per data (because a pointer is surplus) More flexibility is achieved More flexibility is achieved n Multiple stacks can simultaneously use the same memory space n Memory usage is proportional to the size of data on the stack, not determined by the maximum stack capacity n On the other hand, capacity of a single stack is limited only by the available memory