Download presentation
Presentation is loading. Please wait.
Published byNoel Patterson Modified over 8 years ago
1
1 Queues and Lists
2
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. 2
3
Queue ADT abstract typedef > 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; 3
4
Queue ADT 4 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;
5
Implementation of Queue 5 01234 B C Front Rear 01234 B C D 01234 B C D 01234 C F
6
Implementation of Queue 6 #define elements int typedef struct { int size,front,rear; elements *items; } queue;
7
Implementation of Queue 7 01234 FrontRear B C 01234 FrontRear C D E 01234 FrontRear C 01234 FrontRear
8
Implementation of Queue 8 queue create ( int n ) { queue q; q.size = n; q.front = -1; /* empty queue has both front and rear == -1 */ q.rear = -1; /* the array of items is viewed as circular */ q.items = (elements*)calloc(n,sizeof(elements)); return q; } int empty ( queue q ) { return (q.rear == -1 ? 1 : 0); }
9
Implementation of Queue 9 int full ( queue q ){ return (q.front == (q.rear+1) % q.size ? 1 : 0); } elements delete ( queue *q ){ elements e; assert(empty(*q)==0); /* enforce precondition */ if (q->front == q->rear) q->rear = -1; /* queue will be empty */ e = q->items[q->front]; if (q->rear == -1) q->front = -1; /* queue is empty */ else q->front = (q->front+1) % q->size; return e; } B C 01234 front rear G C D E 01234 FrontRear
10
Implementation of Queue 10 void insert ( queue *q, elements e ) { assert(full(*q)==0); /* enforce precondition */ q->rear = (q->rear+1) % q->size; q->items[q->rear] = e; if (q->front == -1) /* queue was empty */ q->front = 0; /* queue of one item */ } void clear ( queue *q ) { free(q->items); q->size = 0; q->front = -1; q->rear = -1; } B C 01234 front rear
11
11 Linked List Group of nodes connected by pointers A node consists of Data Pointer to next node 6538 HeadNull
12
12 Insertion into a Linked List Insert 9 after 5 6538 HeadNull 9
13
13 Deletion from a Linked List Delete 3 from the list 6538 HeadNull 9 Free this space
14
14 Declaration of a node struct node { int info; struct node *next; }; typedef struct node node; 6538 HeadNull
15
15 Linked List Structure 6538 Head 6538 Null Some address In memory Points back to Internal node
16
16 Dynamic allocation of a node node *ptr; ptr = (node *)malloc(sizeof(node)); ? ptr free(ptr)
17
17 Dynamic allocation of a node node *ptr; ptr = (node *)malloc(sizeof(node)); free(ptr); ? ? ? ptr Memory Problems
18
Dynamic allocation of a node ==19325== HEAP SUMMARY: ==19325== in use at exit: 32 bytes in 2 blocks ==19325== total heap usage: 3 allocs, 1 frees, 48 bytes allocated ==19325== ==19325== LEAK SUMMARY: ==19325== definitely lost: 32 bytes in 2 blocks ==19325== indirectly lost: 0 bytes in 0 blocks ==19325== possibly lost: 0 bytes in 0 blocks ==19325== still reachable: 0 bytes in 0 blocks ==19325== suppressed: 0 bytes in 0 blocks ==19325== Rerun with --leak-check=full to see details of leaked memory ==19325== ==19325== For counts of detected and suppressed errors, rerun with: -v ==19325== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 18 Fox01>valgrind./memoryleakexample
19
19 Dynamic allocation of a node node *ptr, *ptr2, *ptr3; ptr = (node *)malloc(sizeof(node)); ptr2 = (node *)malloc(sizeof(node)); ptr3 = (node *)malloc(sizeof(node)); ? ptr2 ? ? ptr ptr3
20
20 Dynamic allocation of a node ptr -> info = 3; ptr2 -> info = 5; ptr3 -> info = 7; 5 ptr2 7 3 ptr ptr3
21
21 Dynamic allocation of a node ptr -> next = ptr2; ptr2 -> next = ptr3; ptr3 -> next = NULL; 5 ptr2 7 3 ptr ptr3 NULL
22
22 Dynamic allocation of a node node *ptr; ptr = (node *)malloc(sizeof(node)); ptr-> next = (node *)malloc(sizeof(node)); ptr-> next ->next = (node *)malloc(sizeof(node)); ptr -> next -> next ->next = NULL; ? ? ? ptr NULL
23
23 Inserting at the Head 1. Allocate a new node 2. Insert new element 3. Make new node point to old head 4. Update head to point to new node 6538 HeadNull 2 6538 Head
24
24 Inserting at the Head void inserthead(node *head, int a) { node *ptr; ptr->info = a; ptr->next = head; head = ptr; } 6538 Head Null 2 6538 Head inserthead(head,2); Memory Problems
25
25 Inserting at the Head void inserthead(node *head, int a) { node *ptr; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = head; head = ptr; } 6538 Head Null 2 6538 Head inserthead(head,2); Can not modify head
26
26 Inserting at the Head node *inserthead(node *head, int a) { node *ptr; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = head; return(ptr); } 6538 Head Null 2 6538 Head head = inserthead(head,2);
27
27 Removing at the Head 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node 6538 Head Null 538 Head Null
28
28 Removing at the Head void deletehead(node *head) { head = head->next; return; } 6538 Head Null 538 Head Null deletehead(head); Memory Leak
29
29 Removing at the Head void deletehead(node *head) { node * ptr; ptr = head; head = head->next; free(ptr); return; } 6538 Head Null 538 Head Null deletehead(head); Can not modify head
30
30 Removing at the Head node* deletehead(node *head) { node * ptr; ptr = head; head = head->next; free(ptr); return(head); } 6538 Head Null 538 Head Null head = deletehead(head);
31
31 Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 6538 Head Null 6538 Head Null 1
32
32 Inserting at the Tail node *inserttail(node *head, int a) { node *ptr; node *ptr2 = head; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = NULL; if (head == NULL) return (ptr); else if (head->next == NULL) { head->next = ptr; return (head); } while (head->next != NULL) head = head->next; head->next = ptr; return(ptr2); } 6538 Head Null
33
33 Print a linked list void printlist (node *head) { while (head !=NULL) { printf("%d ",head->info); head = head->next; } printf("\n"); } 6538 HeadNull printlist(head);
34
34 Find length of a linked list int length(node *head) { if (head == NULL) return 0; else return 1 + length(head->next); } 6538 HeadNull x=length(head);
35
Linked List Implementation of Queue typedef struct { int size,front,rear; elements *items; } queue; 35 6538 HeadNull
36
Linked List Implementation of Queue struct node { elements info; struct node *next; }; typedef struct node node; 36 6538 front Null struct queue { node *front; node *rear; } ; typedef struct queue queue rear
37
Linked List Implementation of Queue 37 6538 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
38
Linked List Implementation of Queue 38 6538 Null queue *q; q frontrear struct
39
Linked List Implementation of Queue 39 6538 front Null int empty ( queue q ) { if (q.front == NULL) return (1); else return (0); } rear
40
Linked List Implementation of Queue 40 6538 front Null void insert ( queue *q, elements e ) { node *l; l = (node*)malloc(sizeof(node)); l->info = e; l->next = NULL; rear if (empty(*q) == 1) q->front = l; else q->rear->next = l; q->rear = l; }
41
Linked List Implementation of Queue 41 6538 front Null elements delete ( queue *q ) { elements e; node *l; assert(empty(*q)==0); l = q->front; e = l->info; l = l->next; rear free(q->front); q->front = l; return e; }
42
42 Doubly Linked List Group of nodes connected by pointers A node consists of Data Pointer to next node Pointer to previous node 6538 Head Null
43
43 Doubly Linked List struct cnode { int info; struct cnode *next; struct cnode *previous; }; typedef struct cnode cnode; 6538 Head Null
44
44 Inserting at the Head cnode *inserthead(cnode *head, int a) { cnode *ptr; ptr = (cnode*)malloc(sizeof(cnode)); ptr->info = a; ptr->next = head; head->previous = ptr; ptr->previous = NULL; return(ptr); } 538 Head Null 6538 Head Null
45
45 Delete a given Node void delete(cnode *p, int *px) { cnode *q,r; if (p== NULL) { printf(“void deletion\n”); return; } *px = p->info; q = p->previous; r = p->next; 6538 Null Head Null q->next = r; r->previous = q; free(p); return; } p q r
46
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 6538 HeadNull 6538 Head
47
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? 6538 list First node Last node
48
Queues as Circular Lists #define elements int struct node { elements info; struct node *next; }; typedef struct node node; 6538 list First node Last node node *insert_item ( node *l, elements i ); /* adds the element i to the rear of the list */ node *delete_item ( node *l, elements *i ); /* deletes the first element from the list */
49
Queues as Circular Lists node *insert_item ( node *l, int i ) { node *nl; nl = (node*)malloc(sizeof(node)); nl->info = i; if (l == NULL) nl->next = nl; else 6538 l { nl->next = l->next; l->next = nl; } return nl; }
50
Queues as Circular Lists node *delete_item ( node *l, int *i ) { assert(l != NULL); if (l->next == l) { *i = l->info; free(l); return NULL; } 6538 l else { node *head = l->next; *i = head->info; l->next = head->next; free(head); return l; }
51
Print a Circular List void print ( node *l ) { if (l != NULL) { node *p; for (p = l->next; p != l; p = p->next) printf(" %d", p->info); printf(" %d", l->info); } 6538 list
52
Exercises Write a function to create a copy of a linked list. Write a function to free the nodes of a linked list. Write a function to reverse a singly linked list Write a function to insert a number into a sorted linked list Write a function to remove duplicate elements in a sorted linked list
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.