cs136 fall 2012 tutorial 5
play

CS136 Fall 2012 - Tutorial 5 CS136 Tutors: - PowerPoint PPT Presentation

CS136 Fall 2012 - Tutorial 5 CS136 Tutors: cs136@student.cs.uwaterloo.ca October 12, 2012 CS136 Tutors: cs136@student.cs.uwaterloo.ca CS136 Fall 2012 - Tutorial 5 Structures and Pointers in C struct node { int key; struct node* next; };


  1. CS136 Fall 2012 - Tutorial 5 CS136 Tutors: cs136@student.cs.uwaterloo.ca October 12, 2012 CS136 Tutors: cs136@student.cs.uwaterloo.ca CS136 Fall 2012 - Tutorial 5

  2. Structures and Pointers in C struct node { int key; struct node* next; }; CS136 Tutors: cs136@student.cs.uwaterloo.ca CS136 Fall 2012 - Tutorial 5

  3. Structures and Pointers in C int main() { struct node *x, y, *z; x = malloc(sizeof(struct node)); z = malloc(sizeof(struct node)); x->key = 15; x->next = z; y.key = 7; (&y)->next = malloc(sizeof(struct node)); (*z).key = 67; z->next = y.next; z->next->key = 12; (*(z->next)).next = y.next; x->next = &y; y.next = &y; } CS136 Tutors: cs136@student.cs.uwaterloo.ca CS136 Fall 2012 - Tutorial 5

  4. Circular Lists A circular list contains a chain of nodes such that each node points to a next node. There is no beginning and no end to a circular list. In other words, by following the next pointer from a node, you will eventually come back to this node. A circular list with only one node is a node whose next pointer points to itself. You may assume that no two nodes contain the same value. We will use the following definition of a node: struct node { int value; struct node* next; }; CS136 Tutors: cs136@student.cs.uwaterloo.ca CS136 Fall 2012 - Tutorial 5

  5. Circular Lists Your goal is to implement the following C functions: struct node* insertNode(int val, struct node* n); struct node* findNode(int val, struct node* n); void printList(struct node* n); void freeCircularList(struct node* n); CS136 Tutors: cs136@student.cs.uwaterloo.ca CS136 Fall 2012 - Tutorial 5

  6. Circular Lists The insertNode function creates a new node with the given value and inserts it right after node n in the circular list. After the insertion, n should point to the newly created node. The function should then return a pointer pointing to the newly inserted node. The findNode function consumes a node in a circular list and a value. If the value exists, then the function returns a pointer to the node containing that value; otherwise, the function returns the NULL pointer. CS136 Tutors: cs136@student.cs.uwaterloo.ca CS136 Fall 2012 - Tutorial 5

  7. Circular Lists The printList function consumes a node in a circular list, and prints the value of each node, separated by a space. After all the values are printed, print a newline. If the circular list is empty, then simply print a newline. The freeCircularList function frees the memory allocated for all the nodes in the circular list. CS136 Tutors: cs136@student.cs.uwaterloo.ca CS136 Fall 2012 - Tutorial 5

  8. The Josephus Ring A classic problem that can be represented with a circular linked list is the Josephus Ring, first documented by the Roman historian Josephus around 100 A.D (See wikipedia for Josephuss rather grim version of the problem). CS136 Tutors: cs136@student.cs.uwaterloo.ca CS136 Fall 2012 - Tutorial 5

  9. The Josephus Ring In our version of the problem, we decide who wins a lottery in the following way: 1 n people will stand in a circle. 2 Starting from the leader, the k th person is removed from the circle, and the circle shrinks. 3 Counting from the new k th person, we now count k people ahead, and remove that person. 4 Repeat step 3 until only one person is left. 5 The remaining person is the winner. CS136 Tutors: cs136@student.cs.uwaterloo.ca CS136 Fall 2012 - Tutorial 5

  10. The Josephus Ring Using the circular linked list from section 2, write a function int solveJosephusRing(int n, int k) which returns the position of the winner for a Josephus Ring of size n with steps of size k. CS136 Tutors: cs136@student.cs.uwaterloo.ca CS136 Fall 2012 - Tutorial 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend