Anda di halaman 1dari 29

SBE201 Data Structures and Algorithms

SBE201 – Data Structures and Algorithms


in C
Tutorial 6

Dina S. El-Kholy, Islam S. Badreldin

Biomedical Engineering Department


Faculty of Engineering
Cairo University

April 3, 2010
Copyright

c 2010 Dina S. El-Kholy, Islam S. Badreldin. Some rights reserved.


Copyright

This work is licensed under a Creative Commons


Attribution-Share Alike 3.0 License
Outline

1 Stacks
Stack definition
Stack implementation: Static implementation
Stack implementation: Dynamic implementation

2 Queues
SBE201 Data Structures and Algorithms
Stacks

Outline

1 Stacks
Stack definition
Stack implementation: Static implementation
Stack implementation: Dynamic implementation

2 Queues
SBE201 Data Structures and Algorithms
Stacks
Stack definition

Stack definition

Stack is a data structure where access is restricted to the most


recently inserted item
Stack is known as a LIFO data structure: Last in first out
There must be a pointer called top to point at the top of the stack
SBE201 Data Structures and Algorithms
Stacks
Stack definition

Stack Operations

For the stack data structure, there are a number of operations


that are performed on it:
Push: add an item to the stack.
Pop: remove an item from the stack. The element to be removed is
the last item added.
Isempty: Check if the stack is empty
Top: read the element on the top (can be implemented using one
pop followed by read and push the element again)
DeleteStack: Pop all elements of the stack (can be elemented by
successive calls to pop)
SBE201 Data Structures and Algorithms
Stacks
Stack definition

Here is how the stack will look like after he following operation:
Example
push(16); push(28);
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Static implementation

Stack implementation: Static implementation

If you know the maximum size of the stack before runtime, you
can implement the stack statically using arrays
Steps
First, you need to declare the data type of the item you will carry
in the array

Example

struct stack_item
int x;
char y;
;
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Static implementation

Second, Declare the array that will be used to implement the


stack

Example

maxstacksize=100;
stack_item stackarray[maxstacksize];

Initialize the top of the stack to -1

Example
int top=-1;

Implement the stack basic functions


SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Static implementation

The push function

push function

Void push(struct stack_item newelem){


if(top==maxstacksize-1){
printf (stack is full \n); return();
}
top++;
stackarray[top]=newelem;
}
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Static implementation

The pop function

pop function

struct stack_item pop(){


if(top==-1){
printf (stack is empty \n);
}
else{
top--;
return (stackarray[top+1]);
}
}
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Static implementation

The top function

Example
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Static implementation

ISempty function

Isempty function

char isempty(){
if (top==-1){return (1);} else{return (0);}
}
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Dynamic implementation

Stack implementation: Dynamic implementation

You can let the stack size grows during runtime by implementing
it dynamically using linked lists
Steps:
First we will declare the structure of the elements contained in
the stack:
Example

struct elem{
int dataitem1; char dataitem2;
struct elem* next;
};
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Dynamic implementation

Second,as before our program needs to keep track of the top of


the stack, so we declare an pointer to the stack element:

Example

struct elem* top;

We then need to write the functions that provide the legal


operation performed on the stack data structure
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Dynamic implementation

The push function

push function

Void push(struct elem newelem){


struct elem* new = (node*) malloc( sizeof( struct elem ) );
if(new==NULL) printf(not enough memory\n); return (0);
*new = *newelem; new->next=NULL;
new->next = *top;
top = new;
return(1);
}
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Dynamic implementation

The pop function

pop function

struct elem pop(){


elem tmp;
elem* tmp_ptr=headptr;

if(isempty()){// this function is implemented later


printf (stack is empty \n); return();
}
tmp=*headptr;
top = top->next;
free(tmp_ptr);
return (tmp);
}
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Dynamic implementation

The Isempty and the top functions

pop function

struct elem top(){


if(isempty()){
printf ("stack is empty \n"); return();
}
return (*top);
}

pop function

char isempty(){
if (top==NULL)return (1); elsereturn (0);
}
SBE201 Data Structures and Algorithms
Queues

Outline

1 Stacks
Stack definition
Stack implementation: Static implementation
Stack implementation: Dynamic implementation

2 Queues
SBE201 Data Structures and Algorithms
Queues

Introduction

A queue is a special type of an ordered list where its access is


restricted to its two ends only
In a queue, you can only insert elements at the end (rear) of the
queue and you can only delete elements from the front of the
queue
It implements the concept of first come first served OR first in
first out . . . FIFO
therefore, in order to deal with a queue, we need at any point of
time to know its front position and its end position
The two main operation we will define for a queue are:
Insert an element to a queue . . . Enqueue
Delete an element from the queue . . . Dequeue
Also, we need to have two additional functions to let us know
whether the queue is empty of full
SBE201 Data Structures and Algorithms
Queues

Queue Implementation

As we have seen with the stacks, the logical definition of the


queue can be implemented in C using arrays or linked lists
depending on your need of static or dynamic implementation
We will consider the static implementation of the stack and you
have to go and find out how to implement it using linked lists
SBE201 Data Structures and Algorithms
Queues

Queue initialization

First step, we need to declare the array that will hold the
elements of the queue
We will initialize the front and the rear of the queue to -1

Queue initialization

int queue[100]; //the queue will carry int data and


its size is 100
int front=-1;
int rear=-1;
// the queue is still empty
SBE201 Data Structures and Algorithms
Queues

Insert an element to the queue


Now we want to insert an element to our queue . . . remember we
said that we can only insert at the rear of the queue
we will write our enqueue function:
Enqueue function

int enqueue(int data)


{if(rear+1>=100)// we have reached the end of the array
return (0);//return 0:we can’t insert any more
queue[rear+1]=data;Insert the data
rear++;
return(1);//return 1:Successful insert
}

If we are inserting the first element in the queue, the rear= 0 and
the front is still at -1, so the front will always be set to the array
index just before the index of the first element in the queue
SBE201 Data Structures and Algorithms
Queues

Deleting an element from the queue

We want to be able to delete an element from the queue,


remember we are only allowed to delete elements from the front
of the queue
Let’s define the dequeue function:

Dequeue function

int dequeue(){
if (front==rear)//testing if the queue is empty!
return(0);//return 0 if the queue is empty
int data=queue[front+1];
front++;
return(data);
}
SBE201 Data Structures and Algorithms
Queues

Examples
Consider a queue of length=5, let’s see how it will look like after
some enqueue and dequeue operations:
SBE201 Data Structures and Algorithms
Queues

Major problem with our array queue!

In the precious example we have seen a situation where the


array of the queue is totally empty and yet we can’t enqueue
anymore since the rear reached the end of the array!
one solution would be to change the mechanism of dequeue,
everytime we dequeue, we have to shift the array toward one
step up such that the queue first element is always at array index
zero
This solution will solve our problem but it is not efficient at all!
A more smart solution is the Circular Queue . . . when the rear
reaches the end of the array, we will let it rotate and return to the
beginning of the array once more!
SBE201 Data Structures and Algorithms
Queues

Circular Queue
Let’s consider an array of size =5,to implement our circular
queue using the this linear array : instead iof letting the front and
the rear move in a linear direction from left to right, we will let the
two variables wrap around to the beginning of the array when
they reach its ending!
SBE201 Data Structures and Algorithms
Queues

Exercise

Implement the circular queue using C language


Your code should be able to:
Enqueue a new element
Dequeue an element
Know number of empty slots in the queue
Know whether the queue is empty or full
Hints: Check the mod operator [%], it will help you wraparound
the rear and the front from the end of the array back to its
beginning!
SBE201 Data Structures and Algorithms
Appendix
For Further Reading

Further Reading

Robert Lafore.
C Programming Using Turbo C++.
Charles F. Bowman.
Algorithms and Data Structures.
Aaron M. Tenenbaum, Yedidyah Langsam, Moshe J. Augenstein.

Data Structures Using C


Wikibooks Contributors.
C Programming.
Wikibooks, Online: http://en.wikibooks.org/wiki/C Programming

Anda mungkin juga menyukai