Data structure

Data structure - $FILE

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Data Structures What is a data structure? An organization of a data set Several operations to be performed on the data set 1. The general problem of data structure design Input: a data set and specifications of the operations to be supported Design: Come up with an organization (structure) of the data elements, and algorithms for the operations, so that the operations are as fast as possible. 2. When do we need a data structure? 3. Review of Stacks and Queues 4. A stack is a data structure S that supports three operations: Push (S,a) Pop(S) Top(S) 5. The pushing (insertion) and popping (detetion) are done at one and the same end of the stack, namely, its top. 6. One implementation is an array S[1:N] and a global index I pointing to the top of the stack, assuming that S[i] is empty but S[i-1] is full Procedure Push (S,a) begin S[I] := a; I := I+1; end function Pop(S) begin I := I-1; return (S[I]); end function Top(S) begin return (S[I-1]); end 7. A queue is a data structure Q that supports two operations: 8. $FILE http://www.seas.gwu.edu/~ayoussef/cs212/datastructures.html 1 of 12 9/3/2010 4:49 AM
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
enqueue(Q,a): it inserts a new element into the rear end (called tail) of the Queue Q deque(Q): deletes the front element from the head of the Queue. One implementation is a circular array Q[0:N-1] and two global indexes H and T, pointing to the head and tail elements of Q. Q[H] is considered full, Q[T] is considered empty. By circular we mean: the element before Q[0] is Q[N-1], and the element after Q[N-1] is Q[0]. Procedure enqueue(Q,a) begin Q[T] := a; T = T - 1 modulo N; end function dequeue(Q) begin H := H-1 modulo N; return (Q[H+1 modulo N]); end 9. Records: A record is a built-in structure data type. It is an aggregate of several elements called fields, where each field is a variable of a standard type or of a record type. 10. Syntax of a record: record name begin field declarations; field declarations; ... field declarations; end Example: record employee begin char name[1:30] integer SSN; char address[1:100] real salaray; end 11. $FILE http://www.seas.gwu.edu/~ayoussef/cs212/datastructures.html 2 of 12 9/3/2010 4:49 AM
Background image of page 2
Once a record has been defined, it becomes a new data type that can be used in declaring record variables: employee X; employee Y[1:100]; record company begin char name[1:50] char address[1:100] employee Z[1:1000] end to access an individual field F of a record R, use R.F Examples: X.SSN := 123456789; X.salary := X.salary + 1000; Pointers data types A pointer is a data type whose contents are addresses of other variables of a specified type. Examples: charpointer p; intpointer q[1:n]; employeepointer r; charptr p; intptr q[1:n]; employeeptr r; to access the field F of a record pointed to by a record pointer r, use r ---> F 12. A self-referential record is a record that has a pointer field that points to a record of the same type.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 09/03/2010.

Page1 / 12

Data structure - $FILE

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online