You are on page 1of 12

TECHNICAL UNIVERSITY OF MANAB FACULTY OF INFORMATICS SCIENCE ENGINEERING DEGREE IN COMPUTER SYSTEMS DATA STRUCTURE TASK # 11.

SPECIAL TYPES OF SIMPLE LISTS (BATTERIES AND QUEUES)

AUTHOR:
PAREDES BRAVO JONATHAN ANDRES

PROFESSOR:
MR. CHRISTIAN TORRES.

LEVEL:
SECOND "C"

PERIOD:
SEPTEMBER 2012 - FEBRUARY 2013

FACULTAD DE CIENCIAS INFORMTICAS

Mission:
Be a unit with high academic prestige, efficiency, transparency and quality in education, organized in their activities, protagonists of regional and national progress.

Vision:
Form efficient professionals and innovators in the field of computer science, giving answers to the needs of society with honesty, fairness and solidarity, raising their standard of living.

TECHNICAL UNIVERSITY OF MANAB

Mission:
Train academics, scientists and responsible professionals, humanist, ethical and supportive, committed to the objectives of national development, which will contribute to the solution of the problems of the country as university teaching with research, able to generate and apply new knowledge, fostering the promotion and dissemination of knowledge and cultures, provided for in the Constitution of the Republic of Ecuador.

Vision:
Be referent, leader and university institution of higher education in Ecuador, by promoting the creation, development, transmission and dissemination of science, technique and culture, social recognition and projection, regionally and globally.

QUEUES
The queues are nothing more than linear lists of information which can be accessed from a particular being of type (FIFO) mode which means that the first data in enter is also data point out, queues will not allow random access to any particular element (as example we can imagine a supermarket queue(, the one film,...), inserts for the tails are made at the end of the list. You must bear in mind that the recovery operation is destructive (deletes the item) queue, if it is not stored anywhere else is destroyed. The queues are mainly used in simulations, event planning, and processes of input output buffer. Circular queues: are nothing more than a variant of the above and their difference is that while in the linear queue is necessary to stop the program when it reaches the limit of the array in the circulars, the queue is full only when the index of storage and retrieval index are equal, otherwise the queue still has space to store more data. Its most common use is in operating systems in which the circular queue maintains that reads file and information that is written to file, applications of real time, etc.. SEMANTIC AND SYNTACTIC SPECIFICATION. queue create) Arguments: None. Effect: Returns an empty queue ready to be used.

void destroy (tail C) Arguments: A C. queue Effect: Destroys the C object freeing up resources which maintains that he employed. To use it again will have to create it again. tElemento front (tail C) Arguments: Receives a C non-empty queue. Effect: Returns the value of the first element of the queue C. Can be written based on the primitive operations of the lists as: element (first (C), C). void poner_en_cola (tElemento x, C tail) Arguments:

x: item that you want to insert in the queue. C: tail in which we insert the element x. Effect: Inserts element x at the end of the queue C. Depending on the operations of the lists would be: inserts (end (C), C x).

void quitar_de_cola (tail C) Arguments: A C tail which must be non-empty. Effect: Removes the first element of the queue C. Depending on the operations of lists would be: deletes (first (C), C). empty int (tail C) Arguments: A C. queue Effect: Returns whether the queue C is an empty queue.

Basic operations
Create

: creates the empty queue. Glue (add, enter, insert): adds an element to the queue. Is added at the end of this. Dequeue (out, out, delete): deletes the front element of the queue, that is, the first item that came. Front (front consult): Returns the front element from the queue, i.e. the first element which entered.

SYNTAX
int ContarElementos (tail & that) { int num_ele; Queue; Value x; num_ele = 0; While (! that.)ColaVacia ()) { = which.Dequeue (x); =.Glue (x); num_ele ++; } While (!.)ColaVacia ())

{ =.Dequeue (x); = which.Glue (x); } return num_ele; }

BATTERIES
A stack is the opposite of a queue, since its access is of type LIFO, the last coming is the first coming out, imagine a bunch of books one atop another and to access the second above first it is necessary to take the first, its main use is for software systems, compilers, interpreters. The two basic operations, are the storage and the recovery, which are called push (for storage) and pop (of recovery), to implement a stack are needed two operations mentioned above and an area of memory for use as battery, can use an array, or a zone assigned by using dynamic memory allocation. Just as in queues, recovery function deletes the value from the list, and if this is not stored somewhere, it is destroyed. The variable top is the index of the next free of stack position. When these functions are implemented, the important thing is to avoid stack overflow from both ends, if top = 0 the battery is empty and if top > that last position of storage battery is full.

BASIC OPERATIONS.
A battery has 2 essential operations: stack and pop, are usually ones in modern implementations of batteries add more habitual use.
Created:

creates the empty stack. (constructor) Size: returns the number of elements in the stack. (size) Stack: adds an item to the stack.)push) Pop: eliminates the front element of the stack.)pop) Top: returns the element that is at the top of the stack. (top or peek) Empty: returns true if the stack is empty or false otherwise (empty).

ENCODING
#include < stdio.h > #include < stdlib.h >
/ * Declaration * / structchar{ intkey; structchar*GIS;

}; / * prototype and implementation * / voidcreate(struct char **battery )); intempty(struct char *battery )); voidstack(struct char *battery, int elem);voidpop(struct char *battery, int *elem )); voidcreate(struct char **battery )) { *stack= (struct char *) ( mallocsizeof(struct char )))); (*stack( )->sig = NULL; } intempty(struct char *battery )){ return (stack->sig == NULL )); } voidstack(struct char *battery, int elem )){ structchar*new; new= (struct char *) ( mallocsizeof(struct char )))); new->key=elem; new->GIS=stack->GIS; stack->GIS=new; } voidpop(struct char *battery, int *elem )){ structchar*aux; aux=stack->GIS; *elem=aux->key; stack->GIS=aux->GIS; free(aux )); } / * test program * / intmainvoid ()) { structchar*the battery; intelem; create(&battery )); If (empty(battery)) printf("nPila empty!" )); stack(battery, 1 )); pop(battery, &elem )); return 0; }

DIFFERENCE BETWEEN BATTERIES AND QUEUES.


A stack is defined as a list or sequence of elements that extends through the placement of new "on top" of the existing elements and is shortened to the Elimination of elements of the upper part of the existing elements. Is an ADT [Abstract data type] with the mathematics of "push" and "pop" operations. A queue is a sequence of elements, which is added to put the new element in the back there and shortened by the Elimination of elements in front of the

queue. It's an ADT [Abstract data type]. No more than these terms understood in programming Java, C + +, Python and so on.

SIMPLE LISTS CODE IN C++


#include <iostream> using namespace std; class Nodo { friend class Lista; public: Nodo(int dato, Nodo *siguientePtr) : dato(0), siguientePtr(NULL) { this->dato = dato; this->siguientePtr = siguientePtr; } int getDato() { return dato; } private: int dato; Nodo *siguientePtr; }; class Lista { typedef Nodo *ptrNodo; public: Lista() : primeroPtr(NULL), ultimoPtr(NULL) { // cuerpo vacio } ~Lista() { if(!estaVacia()) { ptrNodo actualPtr; ptrNodo tempPtr; cout << "Destruyendo nodos..." << endl; actualPtr = primeroPtr; while(actualPtr != NULL) { tempPtr = actualPtr; cout << "-" << tempPtr->dato << endl; actualPtr = actualPtr->siguientePtr; delete tempPtr;

} } cout << "Se destruyeron todos los nodos." << endl; } void insertarAlFrente(const int valor) { ptrNodo nuevoPtr; nuevoPtr = getNuevoNodo(valor); if(estaVacia()) { primeroPtr = ultimoPtr = nuevoPtr; } else { nuevoPtr->siguientePtr = primeroPtr; primeroPtr = nuevoPtr; } } void insertarAlFinal(const int valor) { ptrNodo nuevoPtr; nuevoPtr = getNuevoNodo(valor); if(estaVacia()) { primeroPtr = ultimoPtr = nuevoPtr; } else { ultimoPtr->siguientePtr = nuevoPtr; ultimoPtr = nuevoPtr; } } bool eliminarDelFrente(int valor) { if(estaVacia()) { return false; } else { ptrNodo tempPtr; tempPtr = primeroPtr; if(primeroPtr == ultimoPtr) { primeroPtr = ultimoPtr = NULL; } else { primeroPtr = primeroPtr->siguientePtr; } valor = tempPtr->dato; delete tempPtr; return true; } }

bool eliminarDelFinal(int valor) { if(estaVacia()) { return false; } else { ptrNodo tempPtr; tempPtr = ultimoPtr; if(primeroPtr == ultimoPtr) { primeroPtr = ultimoPtr = NULL; } else { ptrNodo actualPtr; actualPtr = primeroPtr; while(actualPtr->siguientePtr != ultimoPtr) { actualPtr = actualPtr->siguientePtr; } ultimoPtr = actualPtr; actualPtr->siguientePtr = NULL; } valor = tempPtr->dato; delete tempPtr; return true; } } bool estaVacia() { return primeroPtr == NULL; } void imprimir() { if(estaVacia()) { cout << "La lista esta vacia." << endl; } else { ptrNodo actualPtr; actualPtr = primeroPtr; while(actualPtr != NULL) { cout << actualPtr->dato << "-> "; actualPtr = actualPtr->siguientePtr; } cout << endl; } } private: ptrNodo primeroPtr; ptrNodo ultimoPtr; // funcion utilitaria para asignar un nuevo nodo.

Nodo *getNuevoNodo(int valor) { ptrNodo siguientePtr; return new Nodo(valor, siguientePtr = NULL); } }; int main() { Lista lista; lista.imprimir(); cout << endl; cout << "Insertando elementos." << endl; for(int i = 1; i <= 5; i++) { lista.insertarAlFinal(i); lista.imprimir(); } cout << endl; lista.insertarAlFrente(8); cout << "Insertando el 8 al frente" << endl; lista.imprimir(); cout << endl; lista.eliminarDelFinal(5); cout << "Eliminando 5 del final" << endl; lista.imprimir(); cout << endl; lista.insertarAlFinal(9); cout << "Insertando el 9 al final" << endl; lista.imprimir(); cout << endl; lista.eliminarDelFrente(8); cout << "Eliminando 8 del frente" << endl; lista.imprimir(); cout << endl; cin.get(); return 0; }

La lista esta vacia. Insertando elementos. 1-> 1-> 2-> 1-> 2-> 3-> 1-> 2-> 3-> 4-> 1-> 2-> 3-> 4-> 5-> Insertando el 8 al frente 8-> 1-> 2-> 3-> 4-> 5-> Eliminando 5 del final 8-> 1-> 2-> 3-> 4-> Insertando el 9 al final 8-> 1-> 2-> 3-> 4-> 9-> Eliminando 8 del frente 1-> 2-> 3-> 4-> 9->

You might also like