Documentos de Académico
Documentos de Profesional
Documentos de Cultura
r Pop
¦emo ing it from the top of the stack is called
it.
r Peek
!ush and !op are the two primary stack operations.
Howe er, it¶s sometimes useful to be able to read the
alue from the top of the stack without remo ing it.
The peek operation does this.
Pushing and popping
ë
M
oid main( )
{
Stack s
s.top = -
...
...
top -
void push( Stack *s, int value )
int is ull( Stack s ) {
s->item [++ s ->top] = value
{
return s.top = = SIZE -
int pop( Stack *s)
{
return s ->item [s ->top --]
int ismpty( Stack s)
{ int peek (Stack s)
{
return s.top = = - return s.item[s.top]
Some uses of stacks
r Stacks are used for:
r pop the top two alues from the number stack and
apply the old operator to them
r push the result on the number stack
M ë
`nitial queue:
After insertion:
After deletion:
M
r Notice how the array contents ³crawl´ to the right
as elements are inserted and deleted
r This will be a problem after a while!
Defining a Oueue
# define S`
typedef struct {
char list[S`[
int front, rear
Oueue
oid main( )
{
Oueue q
q.front =
q.rear =
...
...
int isfull ( Queue q)
{
return q.rear = = SIZE ±
int isempty(Queue q)
{
return q.rear = = q.front
void insert (Queue *q, char *str)
{
q->rear = q->rear +
strcpy(q->list[q->rear], str)
char * delete ( Queue *q)
{
char str[]
q->front = q->front +
strcpy(str, q->list[q->front])
return str
void display (Queue q)
{
int i
for(i= q.front+ i<= q.rear i++)
puts(q.list[i])
ircular Oueue
This is a problem!
ull and empty queues: solutions
r Solution #: Keep an additional ariable
ë
int isempty(Queue q)
{
return q.rear = = q.front
oid insert (Oueue *q, char *str)
{
q->rear = (q->rear + ) % S`
strcpy(q->list[q->rear, str)
³a sequence of nodes´
!reliminaries
r mptions for implementing an ADT List
r Array has a fixed size
r Data must be shifted during insertions and
deletions
r Linked list is able to grow in size as
needed
r Does not require the shifting of items during
insertions and deletions
Anatomy of a linked list
% &
% &
data item
next
Linked List
head
A LL
Tra ersing a SLL (animation)
%
void displaylist(Link *head)
{
Link *current = head
while(current!=NULL)
{
printf("%d",current->item)
current = current->next
`nserting after (animation)
&
%
Doubly-linked lists
r Here is a doubly-linked list (DLL):
)''
%
)''
%