Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms and data structures Protected by 7.6.2014.

Similar presentations


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

1 Algorithms and data structures Protected by

2 Creative Commons n You are free to: share copy and redistribute the material in any medium or format share copy and redistribute the material in any medium or format adapt remix, transform, and build upon the material adapt remix, transform, and build upon the material n 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. 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. 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. 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. Text copied from / 21Algorithms and data structures, FER 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.

3 Stack

4 Algorithms and data structures, FER4 / Stack 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

5 Algorithms and data structures, FER5 / 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 MAXSTACK=5

6 Algorithms and data structures, FER6 / 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 O(1)

7 Algorithms and data structures, FER7 / 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 O(1)

8 Algorithms and data structures, FER8 / Exercises 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);

9 Algorithms and data structures, FER9 / 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: 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)

10 Lists

11 Algorithms and data structures, FER11 / 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

12 Algorithms and data structures, FER12 / 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

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

14 Algorithms and data structures, FER14 / 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

15 Algorithms and data structures, FER15 / 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

16 Algorithms and data structures, FER16 / 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)

17 Algorithms and data structures, FER17 / 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

18 Algorithms and data structures, FER18 / 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

19 Algorithms and data structures, FER19 / 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)

20 Algorithms and data structures, FER20 / 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


Download ppt "Algorithms and data structures Protected by 7.6.2014."

Similar presentations


Ads by Google