algorithms and data structures protected by 7.6.2014
Post on 30-Mar-2015
215 Views
Preview:
TRANSCRIPT
Algorithms and data structuresAlgorithms and data structures
Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/10.04.23
Creative CommonsCreative Commons
You are free to:You are free to: shareshare — copy and redistribute the material in any medium or format — copy and redistribute the material in any medium or format adaptadapt — remix, transform, and build upon the material — remix, transform, and build upon the material
Under the following terms: Under the following terms: Attribution Attribution — You must give— You must give appropriate credit, appropriate credit, provide a link to the license, and provide a link to the license, and
indicate if changes were madeindicate if changes were made. You may do so in any reasonable manner, but not . You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. in any way that suggests the licensor endorses you or your use.
NonCommercial NonCommercial — You may not use the material for— You may not use the material for commercial purposes commercial purposes. . ShareAlike ShareAlike — If you remix, transform, or build upon the material, you must — If you remix, transform, or build upon the material, you must
distribute your contributions under the same license as the original.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 http://creativecommons.org/licenses/by-nc-sa/3.0/
10.04.23 2 / 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.
10.04.23
StackStack
Algorithms and data structures, FER 4 / 2010.04.23
StackStack
Data structure where the last stored record is processed first Data structure where the last stored record is processed first Required operations:Required operations:
Addition (Addition (pushpush) of elements at the top of the stack) of elements at the top of the stack Removing (Removing (poppop) of elements from the top of the stack) of elements from the top of the stack Initialisation of an empty stackInitialisation of an empty stack
A single operation A single operation pushpush or or poppop requires an equal execution time, regardless requires an equal execution time, regardless of the number of already stored elementsof the number of already stored elements
The case when the stack is full may require the allocation of The case when the stack is full may require the allocation of additional memory and repeated execution of the programadditional memory and repeated execution of the program An empty stack does not necessarily imply an errorAn empty stack does not necessarily imply an error
Algorithms and data structures, FER 5 / 2010.04.23
MAXSTOG=5
Stack Stack – array implementation– array implementation
It can be implemented using a static data structureIt can be implemented using a static data structure According to the principle According to the principle Last In First OutLast In First Out ( (LIFOLIFO), elements are added to or removed from ), elements are added to or removed from
the one-dimthe one-dimenensional array of a given structuresional array of a given structure The value marking the top position of the stack is updatedThe value marking the top position of the stack is updated
Initialisation of an empty stack:Initialisation of an empty stack: Resetting the position of the topResetting 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
-1
MAXSTACK=5
Algorithms and data structures, FER 6 / 2010.04.23
Push an element Push an element on on the stackthe 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
-1
552277-4-411
O(1)
Algorithms and data structures, FER 7 / 2010.04.23
Pop an element from the stackPop 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);
pop(&element, &stack);
pop(&element, &stack);
pop(&element, &stack);
pop(&element, &stack);
pop(&element, &stack);Determine the complexity!
stack->top
0
1
2
3
4
-1
552277-4-411
O(1)
Algorithms and data structures, FER 8 / 2010.04.23
ExercisesExercises
Write the function Write the function that that poppopss elements from a stack elements from a stack implemented implemented with with a a static array. The single element is a floating point number. If the static array. The single element is a floating point number. If the operation failed, the function returns 0, otherwise 1.operation failed, the function returns 0, otherwise 1.
Write the function Write the function that that pushpusheses elements elements on on a stack a stack imlemented imlemented with with a a static array with maximum capacity of static array with maximum capacity of MAXMAXRR records. A record to records. A record to be stored on stack consists of an integer and 10 floating point be stored on stack consists of an integer and 10 floating point numbers. If the operation failed, the function returns numbers. If the operation failed, the function returns 00, otherwise , otherwise 11. . The function prototype isThe function prototype is
int push(struct node element, Stack *stack);int push(struct node element, Stack *stack);
Algorithms and data structures, FER 9 / 2010.04.23
ExercisesExercises
There is an unformatted file There is an unformatted file stack.dat stack.dat on disk, organised as a stack. At on disk, organised as a stack. At the file beginning it is written the maximum allowed capacity of the stack, the file beginning it is written the maximum allowed capacity of the stack, expressed as the number of records (expressed as the number of records (intint) and the address of the last element ) and the address of the last element written on the stack (written on the stack (longlong). ). An element on the stack is a record with the An element on the stack is a record with the passed examination data for a student:passed examination data for a student: ID number (ID number (longlong)) Name and family name (24+1 character)Name and family name (24+1 character) Code of the course (Code of the course (intint)) Grade (Grade (shortshort))
Define the data type Define the data type StackStack and write the necessary functions for and write the necessary functions for handling the stack. As the main program use the main from handling the stack. As the main program use the main from StogPoljem.c (StackByArrayStogPoljem.c (StackByArray.c.c))
10.04.23
ListsLists
10.04.23
Algorithms and data structures, FER 11 / 2010.04.23
Basic termsBasic terms
Linear listLinear list A=(aA=(a11,a,a22,...a,...ann))is a data structure is a data structure consisting of an ordered sequence of elements, selected consisting of an ordered sequence of elements, selected from a data setfrom a data set
A linear list is empty if it contains A linear list is empty if it contains n=0n=0 elements elements List elements List elements aaii are called are called atomsatoms A list can be A list can be implemented implemented using the static data structure - using the static data structure -
arrayarray
Algorithms and data structures, FER 12 / 2010.04.23
List List implementationimplementation
The dynamic data structure to The dynamic data structure to implement implement a lista list consists of a consists of a pointerpointer to the first to the first list element and of an list element and of an arbitrary number of atomsarbitrary number of atoms
Each atom consists of aEach atom consists of a data data part and apart and a pointer pointer to the next list elementto the next list element For each list atom, the memory is allocated at the moment when needed to For each list atom, the memory is allocated at the moment when needed to
store data, and it is released when data are deletedstore data, and it is released when data are deleted Granulation Granulation is is of the of the sizesize atom atom
struct at {
int element;
struct at *next;
};
typedef struct at atom;
next
element
atom atom
Algorithms and data structures, FER 13 / 2010.04.23
Empty and non empty listEmpty and non empty list
Empty list
52 42
Non empty list
head
head
Algorithms and data structures, FER 14 / 2010.04.23
StackStack – list implementation – list implementation
Stack, earlier Stack, earlier implemented implemented with an array, can with an array, can also also bebe implemented implemented with a linear with a linear listlist Insertion to and deletion from the list is performed on the same end of the listInsertion to and deletion from the list is performed on the same end of the list Head of the list serves as the Head of the list serves as the toptop of the stack 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, FER 15 / 2010.04.23
StackStack – list implementation (push element) – 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, FER 16 / 2010.04.23
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
3new
Complexity?O(1)
StackStack – list implementation (push new element) – list implementation (push new element)
Algorithms and data structures, FER 17 / 2010.04.23
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
53
aux
StackStack – list implementation (pop element) - I – list implementation (pop element) - I
Algorithms and data structures, FER 18 / 2010.04.23
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);stack->top
5
aux
StackStack – list implementation (pop element) - II – list implementation (pop element) - II
Algorithms and data structures, FER 19 / 2010.04.23
Stack – list implementation (pop Stack – list implementation (pop element from element from emptyempty stack 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);
stack->top
aux
Complexity? O(1)
Algorithms and data structures, FER 20 / 2010.04.23
Memory usageMemory usage
Representation of stack using a list requires more memory per data Representation of stack using a list requires more memory per data (because a pointer is surplus)(because a pointer is surplus) More flexibility is achievedMore flexibility is achieved
Multiple stacks can simultaneously use the same memory spaceMultiple stacks can simultaneously use the same memory space Memory usage is proportional to the size of data on the stack, not Memory usage is proportional to the size of data on the stack, not
determined by the maximum stack capacitydetermined by the maximum stack capacity On the other hand, capacity of a single stack is limited only by the On the other hand, capacity of a single stack is limited only by the
available memoryavailable memory
top related