Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Bonus Lab PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

http://www.comp.nus.edu.

sg/~cs1010e/labs/Lab6/

for Week 13, Wednesday, 10th November 2010


Back to index page

This is a bonus lab. This lab is outside of examinable curriculum for CS1010e. While it is an achievement to be able to complete this lab, do
not worry if you are unable to solve the questions.
If you have any questions, you may post your queries in the IVLE forum of your respective lecture group.

Instructions
1. There is only one task.
2. Submit all tasks to CodeCrunch. You are given 20 submissions for each task.
3. You may need to refer to the C online reference here.

There is no deadline.

1 of 12

11/9/2010 7:20 AM

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

In this task you are given an input containing n lines, each is a string of length n where 1 <= n <= 20. This input corresponds to a maze, with
'#' being the walls and '.' being traversable spaces. Your task is to find the path, given a start coordinate and the end coordinate. The maze
traveler may only move 4 directions: up, down, left and right.
We use the "keep your left hand on the wall" method. The algorithm to complete solve the maze problem is given as follows:
while destination is not reached,
if you can turn left, then turn left,
otherwise if you can go forward, then stay put
otherwise if you can turn right, then turn right
otherwise turn back.
move forward for one step
The first line of a input is a single number n, which indicates the size of the grid.
The next n lines contains a string of length n each. Each character in the string is either '.' or '#', where '#' represent the walls and '.'
represent traversable spaces.
The next line contains 4 numbers. The first two numbers are the row and column numbers of the start position. The next two numbers are the
row and column numbers of the end position.
Your output should the original grid with the '+' character indicating the path taken.

2 of 12

11/9/2010 7:20 AM

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

Sample run using interactive input (user's input shown in blue; output shown in bold purple. Note the space after every
number including the last number.
10
##########
#.####...#
#....#.#.#
####.#.#.#
#......#.#
#.##.#.#.#
#....#.#.#
######.#.#
######....
##########
1 1 8 9
##########
#+####+++#
#++++#+#+#
####+#+#+#
#...+++#+#
#.##.#.#+#
#....#.#+#
######.#+#
######..++
##########
Sample run 2:
10
##########
#.####...#
#....#.#.#
####.#.#.#
#......#.#
#.##.#.#.#

3 of 12

11/9/2010 7:20 AM

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

#....#.#.#
#.####.#.#
#........#
##########
1 1 7 1
##########
#+####+++#
#++++#+#+#
####+#+#+#
#...+++#+#
#.##.#.#+#
#....#.#+#
#+####.#+#
#++++++++#
##########
Sample run 3:
10
##########
#.####...#
#....#.#.#
####.#.#.#
#......#.#
#.##.#.#.#
#....#.#.#
######.#.#
######...#
##########
8 8 7 6
##########
#.####...#
#....#.#.#
####.#.#.#
#......#.#
#.##.#.#.#
#....#.#.#

4 of 12

11/9/2010 7:20 AM

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

######+#.#
######+++#
##########
Sample run 4:
10
##########
#.####...#
#....#.#.#
####.#.#.#
#......#.#
#.##.#.#.#
#....#.#.#
######.#.#
######...#
##########
8 8 6 1
##########
#.####...#
#....#.#.#
####.#.#.#
#...+++#.#
#.##+#+#.#
#++++#+#.#
######+#.#
######+++#
##########
Sample run 5:
10
##########
#.####...#
#....#.#.#
####.#.#.#
#......#.#

5 of 12

11/9/2010 7:20 AM

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

#.##.#.#.#
#....#.#.#
######.#.#
######...#
##########
8 8 1 1
##########
#+####...#
#++++#.#.#
####+#.#.#
#++++++#.#
#+##+#+#.#
#++++#+#.#
######+#.#
######+++#
##########

You should write your code in the file Maze.c

Submit your program through CodeCrunch.

Warning: This section introduces new concepts such as dynamic memory allocation and pointer to pointer to struct. The program requires a
deep understanding of pointers and is a challenge to even A+ students. If you do not want your brain to explode in a spectacular manner,
leave this page and go to here. In your lecturer's time (which is not too long ago), LinkedList was taught in the last lecture of the CS1010E
equivalent module.
In computer science, a linked list is a data structure that consists of a sequence of data records such that in each record there is a field that
contains a reference (i.e., a link) to the next record in the sequence.
6 of 12

11/9/2010 7:20 AM

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

Linked lists are among the simplest and most common data structures; they provide an easy implementation for several important abstract
data structures, including stacks, queues, associative arrays, and symbolic expressions.
The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data
items are stored in memory or on disk. For that reason, linked lists allow insertion and removal of nodes at any point in the list, with a
constant number of operations. This is not true in an array, where we have to perform a shift on all items to the right if we were to insert an
item at any point in the array.
For this task, you will be implementing a simple Linked List data structure to store a sequence of numbers. You will be implementing only a
single function which is listed below:
insert (num, i) : Insert an the number num at position i.
(Optional) You may wish to also implement this function:
delete (i) : deletes the item at position i.
8 of the test cases will test a series of insert operations. 2 of the test cases will test a series of insert AND delete operations. If you did not
implement the optional part of this question, you will fail 2 test cases, so don't be surprised.
A working sample code is given to you. It will require extensive modifications before you can use it to to complete your task. You should code
modularly (implement the functions insert and delete), otherwise it would defeat the purpose of attempting this exercise.

We will begin to explain the details of this problem. The section below is a summary of what you need to know.

7 of 12

11/9/2010 7:20 AM

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

List Nodes
Before we start, we need to understand the concept of a Linked List. A Linked List is a chain of Nodes. Each node is connected to a single
node (as seen in the diagram above) in a chain. In this task, a Node need only store two information: its payload (which is the value it is
storing) and a pointer to the next node in the list. For example in the diagram above, the leftmost node is storing the value 12, and have a
pointer pointing to the node containing the value 99.
Naturally we would use a struct to represent a node. We do this with the struct definition below:
struct ListNode
{
struct ListNode *next;
int value;
};
For example to construct the list in the diagram above, we can simply call the following statements:
struct ListNode a, b, c;
c.value = 37;
c.next = NULL;
//This means that c does not have a next element
b.value = 99;
b.next = &c;

//This means that the next element of b is a pointer to c

a.value = 12;
a.next = &b;

//This means that the next element of a is a pointer to b

The head of the list is at the ListNode a. We can see that a has a pointer to b, b has a pointer to c and c does not point to anything.

Printing out the Nodes


When printing out a String, we can simply iterate through all the characters until we reach the null character and halt.
This is similar when we try to print out the contents of a List. We iterate until we see that the next pointer is not pointing to anything.
void printList(struct ListNode *curr)
{
while(curr != NULL)

8 of 12

11/9/2010 7:20 AM

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

{
printf(?d ? curr->value);
curr = curr->next;
}
printf(\n?;
}

Dynamic Memory Allocation


Unlike an array where the memory space is allocated during compilation and not runtime, the memory used by a Linked List increases
dynamically during runtime when items are added into the list. As such we need to perform dynamic memory allocation by calling the
function malloc whenever we need to add a new item to the list. The function malloc returns a pointer to the memory allocated.
The function malloc requires a parameter - which is the size in bytes to allocate the memory. We can easily obtain the size required by our
ListNode struct by using the function sizeof as follows:
sizeof(struct ListNode)

//Try doing the following: printf("%d %d", sizeof(int), sizeof(double));

Therefore to request the OS to allocate a memory location to our ListNode, we do the following:
malloc(sizeof(struct ListNode));
Malloc returns a void pointer to the memory allocated. To avoid compilation warnings, we should cast it to the appropriate type. Naturally we
should assign this memory space to a pointer so that we can access it later on.
struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));
Now that we have introduced the basic concepts, we can start working on our program!

Adding an item
To help you out, we demonstrate how to add an item to the beginning of the list. The idea is that we shift the current head node to the
RIGHT. To do this, we set the new item that we are going to add as the new head node, and set the next element of this new head node to
be pointing to the old head element.
int main()
{
struct ListNode *head = NULL, *temp;

9 of 12

11/9/2010 7:20 AM

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

int n;
do
{
scanf(?d? &n);
if(n > 0)
{
temp = malloc(sizeof(struct ListNode);
temp->value = n;
if(head == NULL)
head = temp;
else
{
temp->next = head;
head = temp;
}
}
} while(n > 0);
printList(head);
}
For clarity, it is recommended that you look at the slides to understand how this piece of code works. Naturally, we want to have modular
code. We rewrite the code above to obtain the following:
int main()
{
struct ListNode *head = NULL, *temp;
int n;
do
{
scanf(?d? &n);
if(n > 0)
{
addHead(&head);
}
} while(n > 0);
printList(head);

10 of 12

11/9/2010 7:20 AM

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

}
void addHead(struct ListNode **head)
{
struct ListNode *curr = malloc(sizeof(struct ListNode));
curr->value = num;
if(*head == NULL)
*head = curr;
else
{
curr->next = *head;
*head = curr;
}
}
You may notice that the fuction addHead takes a pointer to a pointer as an argument. Why do we need to do that? This is because, with
every insertion, the head node will change. If we call the function addHead with just the pointer to the struct as an argument, any changes to
the value of the pointer itself will not apply to the head variable in the main function!

Input and Output


The input contains n lines. Each line consists of a String command, followed by the parameters required by the command. There are three
possible commands
insert
delete
halt
If the command is insert, there will be two integers num and i following the command. This means to insert the number num at position i. You
may assume that the position is always valid (if there are only two items in the list, valid positions would be 0, 1 and 2).
If the command is delete, there will be one integer i following the command. This means to delete the number at position i. Again you may
assume that the position specified is always valid.
If the command is halt, you should stop asking for user input and print the list.

11 of 12

11/9/2010 7:20 AM

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

Sample run using interactive input (user's input shown in blue; output shown in bold purple).
insert 1 0
insert 2 0
insert 3 0
halt
3 2 1
Sample run 2:
insert 1 0
insert 2 1
insert 3 2
halt
1 2 3
Sample run 3:
insert 1 0
insert 2 0
insert 3 0
insert 4 3
halt
3 2 1 4

Download the skeleton file LinkedList.c here. You will need to make significant modifications to this code to solve this task.

Submit your program through CodeCrunch.

12 of 12

11/9/2010 7:20 AM

You might also like