1 midterm 1 on friday february 12 closed book, closed notes no computer can be used 50 minutes 4...
DESCRIPTION
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.TRANSCRIPT
![Page 1: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/1.jpg)
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
![Page 2: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/2.jpg)
2
Stack
![Page 3: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/3.jpg)
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.
![Page 4: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/4.jpg)
Implementation of Stack#define elements int #define stacksize 100
typedef struct { int top; elements items[stacksize]; } stack;
4
![Page 5: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/5.jpg)
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
![Page 6: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/6.jpg)
Implementation of Stack II#define elements int
typedef struct { int top; int size; elements *items; } stack;
6
![Page 7: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/7.jpg)
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
![Page 8: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/8.jpg)
8
Queues and Lists
![Page 9: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/9.jpg)
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
![Page 10: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/10.jpg)
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
![Page 11: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/11.jpg)
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;
![Page 12: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/12.jpg)
Implementation of Queue
12
#define elements int
typedef struct{ int size,front,rear; elements *items;} queue;
![Page 13: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/13.jpg)
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
![Page 14: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/14.jpg)
14
Linked List Group of nodes connected by pointers A node consists of
Data Pointer to next node
6 5 3 8Head Null
![Page 15: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/15.jpg)
15
Declaration of a nodestruct node{
int info;struct node *next;
};typedef struct node node;
6 5 3 8Head Null
![Page 16: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/16.jpg)
16
Linked List Structure
6 5 3 8Head
6 5 3 8Head Null
Some addressIn memory
Points back toInternal node
![Page 17: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/17.jpg)
17
Dynamic allocation of a nodenode *ptr;ptr = (node *)malloc(sizeof(node));
?ptr
free(ptr)
![Page 18: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/18.jpg)
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
![Page 19: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/19.jpg)
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);
![Page 20: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/20.jpg)
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
![Page 21: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/21.jpg)
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
![Page 22: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/22.jpg)
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);
![Page 23: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/23.jpg)
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);
![Page 24: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/24.jpg)
Linked List Implementation of Queue
typedef struct{ int size,front,rear; elements *items;} queue;
24
6 5 3 8Head Null
![Page 25: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/25.jpg)
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
![Page 26: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/26.jpg)
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
![Page 27: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/27.jpg)
Linked List Implementation of Queue
27
6 5 3 8 Null
queue *q;
q
front rear
struct
![Page 28: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/28.jpg)
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
![Page 29: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/29.jpg)
29
Doubly Linked Liststruct cnode{
int info;struct cnode *next;
struct cnode *previous;};typedef struct cnode cnode;
6 5 3 8Head
NullNull
![Page 30: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/30.jpg)
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
![Page 31: 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](https://reader036.vdocuments.us/reader036/viewer/2022070611/5a4d1bb47f8b9ab0599ce1c8/html5/thumbnails/31.jpg)
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