1. Trang chủ
  2. » Công Nghệ Thông Tin

INTRODUCTION TO COMPUTER SCIENCE - PART 4 pdf

6 455 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 6
Dung lượng 285,68 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

In its simplest form, a table might be a linear list of elements, when its relevant structural properties might include the answers to such questions as: Which element is first in the li

Trang 1

HANDOUT #4 THE LIST DATA MODEL

K5 & K6, Computer Science Department, Văn Lang University

Second semester Feb, 2002

Instructor: Trần Đức Quang

Major themes:

1 Basic Concepts

2 Stacks

3 Queues

Reading: Sections 6.2, 6.6, and 6.8.

4.1 BASIC CONCEPTS

Computer programs usually operate on tables of information In most cases these tables are not simply amorphous masses of numerical values; they involve important

structural relationships between the data elements.

In its simplest form, a table might be a linear list of elements, when its relevant

structural properties might include the answers to such questions as: Which element is first in the list? Which is last? Which elements precede and follow a given one? How many elements are in the list? In this handout, we only consider linear lists

Conceptually, a linear list (or simply list) is a finite sequence of zero or more ele-ments We generally expect the elements of a list to be of some one type For example,

if the elements of a list are all of integers, we may call it a list of integers

Character strings are special lists In C, a string is stored in an array ending with a

null marker ’\0’ All the library functions have a test for this marker.

Linked Lists

A list may be implemented as a linked list of elements, each is allocated a cell or node consisting of an area for data and a pointer to the next element in the list, as shown in the following figure See more in Section 6.4 (textbook) and Handout #1

Element 1 Element 2 Element 3 Element 4

Trang 2

24 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #4 THE LIST DATA MODEL

4.2 STACKS

A stack is a special kind of lists in which all operations are performed at one end of the list, which is called the top of the list The term "LIFO (for last-in first-out) list" is a

synonym for stack

Because of this restriction, stacks have two specialized operations: push and pop where push(x) puts the element x on the top of the stack and pop removes the topmost

element from the stack

The figure illustrates how to use a stack with push and pop operations to compute

the value of the postfix expression 3 4 + 2 × The details can be explained in class

Stacks can be implemented by an array or linked list We only discuss an

array-based implementation with elements to be of type int.

SYMBOL PROCESSED

Initial 3 4 +

2

×

ε

3

3, 4 ε

7

7, 2 ε

14

push 3 push 4 pop 4; pop 3

compute 7 = 3 + 4

push 7 push 2 pop 2; pop 7

compute 14 = 7× 2

push 14

a0

a1

a n−

.

.

0 1

n − 1

MAX − 1

Trang 3

A smart way to implement a stack by an array is to create a structure consisting of:

1 An array to hold the elements, and

2 A variable top to keep track of the top of stack.

In the listing, we represent the declaration for an array-based stack of integers and its operations You should read the listing carefully

typedef struct {

int A[MAX];

int top;

} STACK;

BOOLEAN isEmpty(STACK *pS)

{

return (pS->top < 0);

}

BOOLEAN isFull(STACK *pS)

{

return (pS->top >= MAX− −1);

}

BOOLEAN pop(STACK *pS, int *px)

{

if (isEmpty(pS))

return FALSE;

else {

(*px) = pS->A[(pS->top)−− −−];

return TRUE;

}

}

BOOLEAN push(int x, STACK *pS)

{

if (isFull(pS))

return FALSE;

else {

pS->A[++(pS->top)] = x;

return TRUE;

}

}

A very important application of stacks is to implement function calls I recommend you to read this in Section 6.7 (textbook)

Trang 4

26 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #4 THE LIST DATA MODEL

4.3 QUEUES

A queue is a restricted form of list in which elements are inserted at one end, the rear, and removed from the other end, the front The term "FIFO (first-in first-out) list" is a

synonym for queue

The intuitive idea behind a queue is a line at a cashier’s window People enter the line at the rear and receive service once they reach the front Unlike a stack, there is fairness to a queue; people are served in the order in which they enter the line Thus the person who has waited the longest is the one who is served next

Like a stack, there is two specialized operations on queues: enqueue and dequeue;

enqueue(x) adds x to the rear of a queue, dequeue removes the element from the front

of the queue

As with stacks, an array or linked list can be used to implement queues For our purpose, we describe a linked list implementation with the following structure for

cells As usual, elements are of type int.

typedef struct CELL *LIST;

struct CELL {

int element;

LIST next;

};

A queue itself is a structure with two pointers  one to the front cell and another to the rear cell

typedef struct {

LIST front, rear;

} QUEUE;

We also present the listing of operations of queues below You should read it carefully

to capture why those operations can work properly

BOOLEAN isEmpty(QUEUE *pQ)

{

return (pQ->front == NULL);

}

BOOLEAN isFull(QUEUE *pQ)

{

return FALSE;

}

Trang 5

BOOLEAN dequeue(QUEUE *pQ, int *px)

{

if (isEmpty(pQ))

return FALSE;

else {

(*px) = pQ->front->element;

pQ->front = pQ->front->next;

return TRUE;

}

}

BOOLEAN enqueue(int x, QUEUE *pQ)

{

if (isEmpty(pQ)) {

pQ->front = (LIST) malloc(sizeof(struct CELL));

pQ->rear = pQ->front;

}

else {

pQ->rear->next = (LIST) malloc(sizeof(struct CELL));

pQ->rear = pQ->rear->next;

}

pQ->rear->element = x;

pQ->rear->next = NULL;

return TRUE;

}

4.4 GLOSSARY

List: Danh sách See the text.

Linear list: Danh sách tuyến tính.

Linked List: Danh sách liên kết A physical data structure used to implement

high-level data structures such as lists, trees, graphs, etc The counterpart of linked lists

is arrays

Relationship: Mối liên hệ, quan hệ In computer terminology, relationship informally

stands for relation with a little difference

Character string, string: Chuỗi ký tự, xâu ký tự See the text.

Stack: Chồng xếp, ngăn xếp

top: Đỉnh (chồng xếp).

Trang 6

28 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #4 THE LIST DATA MODEL

push: Đẩy (vào chồng xếp).

pop: Nhặt, lấy (ra khỏi chồng xếp).

Postfix expression: Biểu thức hậu vị An expression in which operators are between

their operands

Declaration: Khai báo A statement introducing a name or identifier into a program In

C, every identifier must be declared before use

Call: Lời gọi A statement with the name of a function, and perhaps a list of actual parameters to tranfer control to that function Also function call or procedure call Queue: Hàng đợi See the text.

front: đầu hàng.

rear: cuối hàng.

enqueue: vào hàng.

dequeue: rời hàng.

Ngày đăng: 09/08/2014, 11:21

TỪ KHÓA LIÊN QUAN