1
Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions
Write a function Write program fragment Explain how to do things using stacks,
queues, lists etc
2
Stack
The Stack Metaphor
• A stack is a data structure in which the elements are accessible only in a last-in/first-out order.
• The fundamental operations on a stack are push, which adds a new value to the top of the stack, and pop, which removes and returns the top value.
• One of the most common metaphors for the stack concept is a spring-loaded storage tray for dishes. Adding a new dish to the stack pushes any previous dishes downward. Taking the top dish away allows the dishes to pop back up.
Implementation of Stack#define elements int #define stacksize 100
typedef struct { int top; elements items[stacksize]; } stack;
4
Implementation of Stackint empty ( stack s ); /* postcondition: empty(s) == 1 if s is empty, == 0 otherwise */ int full ( stack s ); /* postcondition: full(s) == 1 if s is full, == 0 otherwise */ elements pop ( stack *s ); /* precondition: not empty(*s); postcondition: push(*s, pop(*s)) == *s */
void push ( stack *s, elements e ); /* precondition: full(*s) == 0; postcondition: push(*s,e); pop(*s) == e */ 5
Implementation of Stack II#define elements int
typedef struct { int top; int size; elements *items; } stack;
6
Implementation of Stack IIstack create ( int n ); /* postcondition: create(n) is a stack which can hold n items */int empty ( stack s ); /* postcondition: empty(s) == 1 if s is empty, == 0 otherwise */ int full ( stack s ); /* postcondition: full(s) == 1 if s is full, == 0 otherwise */ int pop ( stack *s, elements *e ); /* precondition: not empty(*s); postcondition: pop(*s,*e) == 0 if not empty(*s), == 1 otherwise, check = push(*s,*e) restores the stack */
7
8
Queues and Lists
QUEUES Very similar to stacks The only difference between them is in
the order in which elements are processed.
A stack uses a last-in/first-out (LIFO) discipline
A queue adopts a first-in/first-out (FIFO) model that more closely resembles a waiting line.
9
Queue ADTabstract typedef <<elements>> queue;/* a queue is a sequence of elements, FIFO = First In First Out */
abstract int empty ( queue q );postcondition: empty(s) == 1 if queue is empty, == 0 otherwise;
abstract int full ( queue q );postcondition: full(s) == 1 if queue is full, == 0 otherwise;
10
Queue ADT
11
abstract elements delete ( queue q ); /* dequeue */precondition: empty(q) == 0;postcondition: first element is removed from queue q;
abstract void insert ( queue q, elements e ); /* enqueue */precondition: full(q) == 0;postcondition: element e is added to the end of queue q;
Implementation of Queue
12
#define elements int
typedef struct{ int size,front,rear; elements *items;} queue;
Implementation of Queue
13
0 1 2 3 4
0 -1
Front Rear
A B C
0 1 2 3 4
0 2
Front Rear
F C D E
0 1 2 3 4
2 0
Front Rear
C
0 1 2 3 4
2 2
Front Rear
14
Linked List Group of nodes connected by pointers A node consists of
Data Pointer to next node
6 5 3 8Head Null
15
Declaration of a nodestruct node{
int info;struct node *next;
};typedef struct node node;
6 5 3 8Head Null
16
Linked List Structure
6 5 3 8Head
6 5 3 8Head Null
Some addressIn memory
Points back toInternal node
17
Dynamic allocation of a nodenode *ptr;ptr = (node *)malloc(sizeof(node));
?ptr
free(ptr)
18
Inserting at the Head1. Allocate a new node2. Insert new element3. Make new node point to old head4. Update head to point to new node
6 5 3 8Head Null
2 6 5 3 8 NullHead
19
Inserting at the Headnode *inserthead(node *head, int a){ node *ptr; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = head; return(ptr);}
6 5 3 8Head Null
2 6 5 3 8 NullHead
head = inserthead(head,2);
20
Removing at the Head1. Update head to point to next node in the list2. Allow garbage collector to reclaim the former
first node
6 5 3 8Head Null
5 3 8Head Null
21
Inserting at the Tail1. Allocate a new node2. Insert new element3. Have new node point to null4. Have old last node point to new node
6 5 3 8Head Null
6 5 3 8Head Null1
22
Print a linked listvoid printlist (node *head){ while (head !=NULL) { printf("%d ",head->info); head = head->next; } printf("\n");}
6 5 3 8Head Null
printlist(head);
23
Find length of a linked listint length(node *head){ if (head == NULL) return 0; else return 1 + length(head-
>next);}
6 5 3 8Head Null
x=length(head);
Linked List Implementation of Queue
typedef struct{ int size,front,rear; elements *items;} queue;
24
6 5 3 8Head Null
Linked List Implementation of Queue
struct node{ elements info; struct node *next; };
typedef struct node node;
25
6 5 3 8
front
Null
struct queue{ node *front; node *rear; } ;
typedef struct queue queue
rear
Linked List Implementation of Queue
26
6 5 3 8
front
Null
int empty ( queue q );/* postcondition: empty(q) == 1 if queue q is empty, * == 0 otherwise; */elements delete ( queue *q );/* precondition: empty(*q) == 0; * postcondition: first element is removed from queue *q; */
void insert ( queue *q, elements e );/* precondition: full(*q) == 0; * postcondition: element e is inserted to end of queue *q; */
rear
Linked List Implementation of Queue
27
6 5 3 8 Null
queue *q;
q
front rear
struct
28
Doubly Linked List Group of nodes connected by pointers A node consists of
Data Pointer to next node Pointer to previous node
6 5 3 8Head
NullNull
29
Doubly Linked Liststruct cnode{
int info;struct cnode *next;
struct cnode *previous;};typedef struct cnode cnode;
6 5 3 8Head
NullNull
Circular Lists Linked lists have some limitations
Given a pointer p, we can not reach preceding nodes In circular lists, next field of last node points to
first node A node consists of
Data Pointer to next node
6 5 3 8Head Null
6 5 3 8Head
Circular Lists Keep a pointer to the last node How can we add or remove an element from either the front
or the rear of the list? How can we check if the list is empty?
6 5 3 8
list
First node Last node