Professional Documents
Culture Documents
11:48 AM
When asked to reverse a string, don't tamper with that string, make a new memory location, and a pointer to
it
Char** str3
(pointer to an array of pointers)
You need to malloc everything
Concatenation
Reversing
Eg
Char** str3 = malloc(sizeof(char*) * n);
Str[0] = malloc(sizeof(char) * 10);
Str[1] = malloc(sizeof(char) * 10);
Str[2] = malloc(sizeof(char) * 10);
newNode->data = v;
(*newNode).data = v;
These two lines are the same
LLQs typically always follow the same structure: create pointer to first node, then iterate through
Useful funciton
Tuesday, 23 June 2015
9:15 PM
There are some useful funcitons that you can rote-learn because they may be useful in the exam, so
you can recall them faster:
listLen(): return the length of the list (remember, length = lastIndex + 1)
oddEven(): returns even or odd (uses listLen)
showList(): prints out the list (you may already be given a showList)
search(): search for the first node that contains a certain value and return its index
Also, consider memorising reverse, it involves multiple pointers and can be quite complicated
numItems()
Tuesday, 23 June 2015
9:20 PM
int length = 0;
link current = l->head;
while (current != NULL){
length++;
current = current->next;
}
return length;
}
int listLen(List l) {
int len = 0;
Node curNode = l -> head;
if (curNode == NULL) {//edgecase0
//lol
} else {
len++; //by deduction, len is at least 1
while (curNode -> next != NULL) {
curNode = curNode -> next;
len++;
}
} printf("len%d\n", len);
return len;
}
oddEven()
Tuesday, 23 June 2015
10:54 PM
int oddEven(List l) {
int len = 0;
int oddEven = EVEN;
Node curNode = l -> head;
if (curNode == NULL) {//edgecase0
//lol
} else {
len++; //by deduction, len is at least 1
while (curNode -> next != NULL) {
curNode = curNode -> next;
len++;
}
}
if (len % 2 == 0) {
oddEven = EVEN;
} else {
oddEven = ODD;
}
return oddEven;
}
showList()
Tuesday, 23 June 2015
10:59 PM
append()
Tuesday, 23 June 2015
10:50 PM
search()
Tuesday, 23 June 2015
11:03 PM
reverse()
Tuesday, 23 June 2015
11:05 PM
Taking canopy
Pac-ket
Archives
Tuesday, 23 June 2015
9:25 PM
Basics
Tuesday, 23 June 2015
9:25 PM
list3-core
Tuesday, 23 June 2015
9:25 PM
.c
.h
tests.c
//functions go here
#include <stdio.h>
#include <stdlib.h>
#include "list3-core.h"
<stdlib.h>
<stdio.h>
<assert.h>
"list3-core.h"
return EXIT_SUCCESS;
}
#include
#include
#include
#include
myList->head = ptrToNewNode;
l -> head = potato; //now head points to the new first node, we have severed the ties
potato -> value = item; //feed new node a value
potato -> next = tempNode;//link it back up with the rest of the list
ptrToNewNode->value = 2;
ptrToNewNode->next = NULL;
}
}
myList->head->next = ptrToNewNode;
int numItems (list l) {
int itemCtr = 0;
link curNode = l -> head;
if (curNode != NULL) {//edge case
while (curNode -> next != NULL) {
itemCtr++;
curNode = curNode -> next;
}
itemCtr++;
}
showList (myList);
assert (numItems (myList) == 2);
return itemCtr;
}
showList (myList);
assert (numItems (myList) == 3);
void append (list l, int value) {
link potato = malloc(sizeof(node)); //make a new node (to be appended)
potato -> value = value; //feed new node a value
potato -> next = NULL; //terminate list at potato
link curNode = l -> head;
if (curNode == NULL) { //edge case
l -> head = potato;
} else {
// insert at end
append (myList, 4);
assert
assert
assert
assert
assert
assert
assert
(myList != NULL);
(myList->head != NULL);
(myList->head->next != NULL);
(myList->head->next->next != NULL);
(myList->head->next->next->next != NULL);
(myList->head->next->next->next->value == 4);
(myList->head->next->next->next->next == NULL);
showList (myList);
assert (numItems (myList) == 4);
}
}
// test lookup
assert (lookup
assert (lookup
assert (lookup
assert (lookup
(myList,0)==0);
(myList,1)==1);
(myList,2)==2);
(myList,3)==4);
list3-extraFns
Tuesday, 23 June 2015
9:26 PM
.c
.h
tests.c
//functions go here
#include <stdio.h>
#include <stdlib.h>
#include "list3-extraFns.h"
#include <stdio.h>
#include <stdlib.h>
//teeeeests
// move all the elements from the second list to the end of
// the first list (in order). this leaves the second list
// empty and all the elements in the first list.
void concatenate (list to, list from);
// count the number of nodes containing 42
int count42s (list l);
return EXIT_SUCCESS;
}
<stdio.h>
<stdlib.h>
<assert.h>
"list3-extraFns.h"
#include
#include
#include
#include
assert
assert
assert
assert
assert
assert
(testList != NULL);
(testList->head != NULL);
(testList->head->value == 1);
(testList->head->next != NULL);
(testList->head->next->value == 4);
(testList->head->next->next == NULL);
deleteHead (testList);
assert
assert
assert
assert
(testList != NULL);
(testList->head != NULL);
(testList->head->value == 4);
(testList->head->next == NULL);
deleteHead (testList);
assert (testList != NULL);
assert (testList->head == NULL);
(testList != NULL);
(testList->head != NULL);
(testList->head->value == 3);
(testList->head->next != NULL);
(testList->head->next->value == 1);
(testList->head->next->next == NULL);
}
}
deleteLast (testList);
assert
assert
assert
assert
(testList != NULL);
(testList->head != NULL);
(testList->head->value == 3);
(testList->head->next == NULL);
deleteLast (testList);
assert (testList != NULL);
assert (testList->head == NULL);
(testList != NULL);
(testList->head != NULL);
(testList->head->value == 3);
(testList->head->next != NULL);
(testList->head->next->value == 1);
(testList->head->next->next != NULL);
(testList->head->next->next->value == 4);
(testList->head->next->next->next != NULL);
(testList->head->next->next->next->value == 1);
(testList->head->next->next->next->next != NULL);
(testList->head->next->next->next->next->value == 5);
(testList->head->next->next->next->next->next == NULL);
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
(testList->head->value == 3);
(testList->head->next != NULL);
(testList->head->next->value == 1);
(testList->head->next->next != NULL);
(testList->head->next->next->value == 4);
(testList->head->next->next->next != NULL);
(testList->head->next->next->next->value == 1);
(testList->head->next->next->next->next != NULL);
(testList->head->next->next->next->next->value == 5);
(testList->head->next->next->next->next->next == NULL);
(testListB != NULL);
(testListB->head != NULL);
(testListB->head->value == 3);
(testListB->head->next != NULL);
(testListB->head->next->value == 1);
(testListB->head->next->next != NULL);
(testListB->head->next->next->value == 4);
(testListB->head->next->next->next != NULL);
(testListB->head->next->next->next->value == 1);
(testListB->head->next->next->next->next != NULL);
(testListB->head->next->next->next->next->value == 5);
(testListB->head->next->next->next->next->next == NULL);
10:06 PM
Monday
Tuesday, 23 June 2015
10:07 PM
.c
.h
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<assert.h>
"List.h"
#define ODD 1
#define EVEN 0
typedef struct _node {
int value;
Node next;
} node;
typedef struct _list {
Node head;
} list;
//when the median() function is complete,
//compile with testList.c, type
//"gcc -Wall -Werror -O -o test testList.c List.c"
//to run program, type
//"./test"
//TODO
//Read List.h to help you look at the hints
//find the median value
//given a list X
//(Empty List)
//then median (list);
//it will return 0
//
//given a list 1->X
//then median (list);
//it will return 1
//
//given a list 0->1->2->X
//then median (list);
//it will return 1
//
//given a list 2->3->5->6->X
//then median (list);
//it will return 4
//because the middle is the average of the 2 middle numbers
//(3+5)/2 = 4.
//We will not test where the average will lead to decimal places
//
//given a list 2->3->5->6->1000->X
//then median (list);
//it will return 5
int listLen(List l);
int oddEven(List l);
int getMid(List l);
int getMidTwo(List l);
test.c
// Written by Michael Simarta
// With the help of Bernice Chen.
//
//run this testList.c file with a compilable List.c
//that should have the functions declared in List.h implemented
#include
#include
#include
#include
void test_test();
void test_median();
int main( int argc, char * argv[] ) {
printf("......STARTING TESTS......\n");
test_test();
test_median();
printf("ALL TESTS PASSED. YOU ARE AWESOME!\n");
return EXIT_SUCCESS;
}
void test_test() {
List l = newList();
append(l, 0);
append(l, 1);
append(l, 2);
assert ( getValue(l, 0) == 0);
assert ( getValue(l, 1) == 1);
assert ( getValue(l, 2) == 2);
printf("EXISTING FUNCTIONS WORK FINE.\n");
}
//Empty List
// median is 0
List l = newList();
assert(median(l) == 0);
printf("median Test1 passed.\n");
destroy(l);
//1 Node list
// median is 1
l = newList();
append(l, 1);
assert(median(l) == 1);
printf("median Test2 passed.\n");
// 0->1->2->X
// median(l) = 1
l = newList();
append(l, 0);
append(l, 1);
append(l, 2);
assert ( median(l) == 1 );
destroy(l);
printf("median Test3 passed.\n");
int median(List l) {
/* Plan:
Break this into parts:
Get list length
Find the middle value
If list length is odd, average the two values.
*/
int median = 0;
Node curNode = l -> head;
//edge cases
if (listLen(l) == 0) {
median = 0;
} else if (listLen(l) == 1) {
median = curNode -> value;
} else { //sort begins
if (oddEven(l) == EVEN) {
int mid1 = getValue(l, getMidTwo(l) - 1);
int mid2 = getValue(l, getMidTwo(l));
median = (mid1 + mid2)/2;
printf ("even median %d\n",median);
} else {//ODD
median = getValue(l, getMid(l) - 1);
}
}
return median;
}
int getMid(List l) {//odd
return (listLen(l)/2)+1; //relies on truncation toward zero
}
int getMidTwo(List l) {//even
//returns the lower of the two indices
return listLen(l)/2;
}
int listLen(List l) {
int len = 0;
Node curNode = l -> head;
if (curNode == NULL) {//edgecase0
//lol
} else {
len++; //by deduction, len is at least 1
while (curNode -> next != NULL) {
curNode = curNode -> next;
len++;
}
} printf("len%d\n", len);
return len;
}
int oddEven(List l) {
int len = 0;
int oddEven = EVEN;
Node curNode = l -> head;
if (curNode == NULL) {//edgecase0
//lol
} else {
len++; //by deduction, len is at least 1
<stdio.h>
<stdlib.h>
<assert.h>
"List.h"
// 2->3->5->6->X
// median(l) = 4
l = newList();
append(l, 2);
append(l, 3);
append(l, 5);
append(l, 6);
assert(median(l) == 4);
printf("median Test4 passed.\n");
// 2->3->5->6->1000->X
// median(l) = 5
append(l, 1000);
assert(median(l) == 5);
printf("median Test5 passed.\n");
destroy(l);
}
}
//appends a node of value to the end of the list
void append(List l, int value) {
Node newNode = malloc(sizeof(node));
newNode->value = value;
newNode->next = NULL;
Node curNode = l->head;
if ( curNode==NULL ) {
l->head = newNode;
} else {
while ( curNode->next!=NULL ) {
curNode = curNode->next;
}
curNode->next = newNode;
}
}
*Tuesday
Tuesday, 23 June 2015
10:07 PM
Friday
Tuesday, 23 June 2015
10:06 PM
.c
.h
test.c
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<assert.h>
"List3.h"
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<assert.h>
"List3.h"
//Task 1: Insert so that when printed from the head to the tail, it is in increasing order
//(The above was probably practiced before)
void test_test();
//void test_median();
void test_insert();
//Task 2: Insert so that when printed from the tail to the head, it is in decreasing order
void insert(List l, int value);
//
test_test();
test_insert();
test_median();
//
//
curNode->prev = NULL;
while (curNode->next != NULL) {
//while current node is not at the end yet
tempNode = curNode;
//backup current node
curNode = curNode->next;
//we are now at the next node
curNode->prev = tempNode;
//the backup is now our previous node
}
tempNode = curNode;
curNode->prev = tempNode;
tempNode = curNode;
//backup last node
curNode->next = new;
//append new node to the end
curNode = curNode->next;
//move to the new node at the end
curNode->prev = tempNode;
//backup is now our previous node
curNode->next = NULL;
//set end of list to null
l->tail = curNode;
//makes the tail point to the last node
curNode = l->head;
//back to the start
curNode = curNode->next;
//first node after head
curNode->prev = NULL;
//set prev to null (see diagram)
return EXIT_SUCCESS;
}
void test_test() {
List l = newList();
append(l, 0);
append(l, 1);
append(l, 2);
assert ( getValue(l, 0) == 0);
assert ( getValue(l, 1) == 1);
assert ( getValue(l, 2) == 2);
//assert ( getValueReverse(l, 0) == 2);
//assert ( getValueReverse(l, 1) == 1);
//assert ( getValueReverse(l, 2) == 0);
//TODO
}
//returns a new list of length 0
List newList() {
List l = malloc(sizeof(list));
assert ( l!=NULL );
l->head = NULL;
l->tail = NULL;
return l;
}
// 3 Node List
//NULL <-> 2 <-> 5 <-> 7 <-> NULL
printf("Doing Test3...\n");
l = newList();
insert(l, 5);
insert(l, 2);
insert(l, 7);
printf("Expecting [HEAD] ->[2]->[5]->[7]->[NULL]\n");
printList(l);
assert ( getValue(l, 0) == 2);
assert ( getValue(l, 1) == 5);
assert ( getValue(l, 2) == 7);
assert (l->head->next->next->next == NULL);
free(l);
}
// 4 Node List
//NULL <-> 1 <-> 2 <-> 4 <-> 6 <-> NULL
printf("Doing Test4...\n");
l = newList();
insert(l, 4);
insert(l, 2);
insert(l, 1);
insert(l, 6);
printf("Expecting [HEAD] ->[1]->[2]->[4]->[6]->[NULL]\n");
printList(l);
assert ( getValue(l, 0) == 1);
assert ( getValue(l, 1) == 2);
assert ( getValue(l, 2) == 4);
assert ( getValue(l, 3) == 6);
assert (l->head->next->next->next->next == NULL);
10:07 PM
May 25 (frontBackSplit)
Tuesday, 23 June 2015
10:07 PM
.c
.h
// frontBackSplit.c
//
test.c
// a link points to a node
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "list-ppexam.h"
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "list-ppexam.h"
//simple unit tests on the list
static void testList(void);
void showList (list l);
testList();
printf("All tests passed ! You are awesome\n");
return EXIT_SUCCESS;
}
int length = 0;
link current = l->head;
while (current != NULL){
length++;
current = current->next;
}
return length;
}
int pos = 1;
if (curNode == NULL) { //edgecase0
frontList -> head = NULL;
backList -> head = NULL;
} else if (curNode -> next == NULL) { //edgecase1
frontList -> head = sourceList -> head;
backList -> head = NULL;
} else {
if (numItems(sourceList) % 2 == 1) {//odd cases
while (pos <= numItems(sourceList)/2) { //relies on truncation towards zero
fcurNode = curNode;
if (pos == 1) {frontList -> head = sourceList -> head;}
curNode = curNode -> next;
fcurNode = fcurNode -> next;
pos++;
}
fcurNode = curNode;
printf("\n\nPos%d\n\n", pos);
} else if (numItems(sourceList) % 2 == 0) { //even cases
while (pos < numItems(sourceList)/2) {
//edgecase2 will skip this loop immediately
fcurNode = curNode;
if (pos == 1) {frontList -> head = sourceList -> head;}
curNode = curNode -> next;
fcurNode = fcurNode -> next;
pos++;
}
if (numItems(sourceList) == 2) {frontList -> head = sourceList -> head;}
fcurNode = curNode;
printf("\n\nPos%d\n\n", pos);
}
//we are now at the breakpoint(last node in list 1)
link tmpNode = curNode -> next; //backup the lnk to second half (possible redundancy)
fcurNode -> next = NULL; //terminate front list
curNode = tmpNode;
backList -> head = curNode;
while (curNode -> next != NULL) { //travels to last node, but does not copy it yet
bcurNode = curNode;
curNode = curNode -> next;
bcurNode = bcurNode -> next;
}
bcurNode = curNode;
//curNode -> next is already a null pointer so you don't need to do anything
}
sourceList -> head = NULL; //empty source list
frontList->head = NULL;
backList->head = NULL;
printf ("before split..\n");
printf("SourceList is: ");
showList (sourceList);
frontBackSplit (sourceList, frontList, backList);
printf ("after split..\n");
printf("frontList is: ");
assert
assert
assert
assert
assert
(frontList->head == &nodes[0]);
(frontList->head->value == 1);
(frontList->head->next == &nodes[1]);
(frontList->head->next->value == 11);
(frontList->head->next->next == NULL);
frontList->head = NULL;
backList->head = NULL;
printf ("before split..\n");
printf("SourceList is: ");
showList (sourceList);
frontBackSplit (sourceList, frontList, backList);
printf ("after split..\n");
printf("frontList is: ");
showList (frontList);
printf("backList is: ");
showList (backList);
printf("SourceList is: ");
showList (sourceList);
assert
assert
assert
assert
assert
(frontList->head == &nodes[0]);
(frontList->head->value == 45);
(frontList->head->next == &nodes[1]);
(frontList->head->next->value == 33);
(frontList->head->next->next == NULL);
assert
assert
assert
assert
assert
(backList->head == &nodes[2]);
(backList->head->value == 12);
(backList->head->next == &nodes[3]);
(backList->head->next->value == 444);
(backList->head->next->next == NULL);
frontList->head = NULL;
backList->head = NULL;
printf ("before split..\n");
printf("SourceList is: ");
showList (sourceList);
frontBackSplit (sourceList, frontList, backList);
printf ("after split..\n");
printf("frontList is: ");
showList (frontList);
printf("backList is: ");
showList (backList);
printf("SourceList is: ");
showList (sourceList);
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
(frontList->head == &nodes[0]);
(frontList->head->value == 3);
(frontList->head->next == &nodes[1]);
(frontList->head->next->value == 65);
(frontList->head->next->next == &nodes[2]);
(frontList->head->next->next->value == 72);
(frontList->head->next->next->next == &nodes[3]);
(frontList->head->next->next->next->value == 3);
(frontList->head->next->next->next->next == &nodes[4]);
(frontList->head->next->next->next->next->value == 0);
(frontList->head->next->next->next->next->next == NULL);
assert
assert
assert
assert
assert
assert
assert
assert
assert
(backList->head == &nodes[5]);
(backList->head->value == 232);
(backList->head->next == &nodes[6]);
(backList->head->next->value == 21);
(backList->head->next->next == &nodes[7]);
(backList->head->next->next->value == 7);
(backList->head->next->next->next == &nodes[8]);
(backList->head->next->next->next->value == 99);
(backList->head->next->next->next->next == NULL);
nodes[6].next
nodes[7].next
nodes[8].next
nodes[9].next
=
=
=
=
&nodes[7];
&nodes[8];
&nodes[9];
NULL;
frontList->head = NULL;
backList->head = NULL;
printf ("before split..\n");
printf("SourceList is: ");
showList (sourceList);
frontBackSplit (sourceList, frontList, backList);
printf ("after split..\n");
printf("frontList is: ");
showList (frontList);
printf("backList is: ");
showList (backList);
printf("SourceList is: ");
showList (sourceList);
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
(frontList->head == &nodes[0]);
(frontList->head->value == 66);
(frontList->head->next == &nodes[1]);
(frontList->head->next->value == 808);
(frontList->head->next->next == &nodes[2]);
(frontList->head->next->next->value == 72);
(frontList->head->next->next->next == &nodes[3]);
(frontList->head->next->next->next->value == 2);
(frontList->head->next->next->next->next == &nodes[4]);
(frontList->head->next->next->next->next->value == 1);
(frontList->head->next->next->next->next->next == NULL);
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
(backList->head == &nodes[5]);
(backList->head->value == 2);
(backList->head->next == &nodes[6]);
(backList->head->next->value == 1);
(backList->head->next->next == &nodes[7]);
(backList->head->next->next->value == 66);
(backList->head->next->next->next == &nodes[8]);
(backList->head->next->next->next->value == 99999);
(backList->head->next->next->next->next == &nodes[9]);
(backList->head->next->next->next->next->value == 1003);
(backList->head->next->next->next->next->next == NULL);
*June 10
Tuesday, 23 June 2015
10:11 PM
*2014
Wednesday, 24 June 2015
12:33 AM
*2013
Wednesday, 24 June 2015
12:33 AM
*2013_2
Wednesday, 24 June 2015
12:33 AM
*selectEven
Wednesday, 24 June 2015
12:33 AM
Prac Exams
Tuesday, 23 June 2015
10:08 PM
2014s1 (redacted)
Tuesday, 23 June 2015
10:08 PM
.c
Q1
/*
*
*
*
*
*
*
*
*/
.h
test.c
#include
#include
#include
#include
<stdlib.h>
<stdio.h>
<assert.h>
"list.h"
/*
*
*
*
*
*
*
*/
testQ1.c
comp1917 final prac exam
some unit tests for Q1.c
Richard Buckland: 21 june 2014
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<assert.h>
"list.h"
given a list "l" which does not contain any duplicates, and given
a character "secret", remove from the list the node (if any) which
has the "secret" character in its value field.
Constraints:
don't malloc any new nodes
don't free any nodes, even the nodes you remove from the list
don't change the "value" field of any node, just change links
/*
eg given the list
"F->R->E->D->*"
redacting F would change the list to be
"R->E->D->*"
*/
/*
compile your code using:
gcc -Wall -Werror -std=c99 -o q1 -O q1.c testQ1.c
run it using
./q1
*/
int search (list l, char secret); //returns the index of the node(s) to be
redacted (if any)
void linkup (list l, int index); //uses the index returned from linkup
void redact (list l, char secret) {
//move along list while comparing value to secret: call this search()
//if search returns positive, initiate linkup()
//both functions may require a number of edge cases
int index = search(l, secret);
if (search(l, secret) != UNDEFINED) {
linkup(l, index);
}
}
int search (list l, char secret) {
int pos = 0;
//edgecase0
link curNode = l -> head;
if (curNode == NULL) {
return UNDEFINED;
} else if (curNode -> next == NULL) {//edgecase1
if (curNode -> value == secret) {
return 0;
} else {
return UNDEFINED;
}
} else {
while (curNode -> next != NULL) {
if (curNode -> value == secret) {
return pos;
}
curNode = curNode -> next;
pos++;
}
if (curNode -> value == secret) {
return pos;
}
}
return UNDEFINED;
}
void linkup (list l, int index) {
link curNode = l -> head;
int pos = 0;
if (index == 0) {
if (l -> head -> next == NULL) {
l -> head = NULL;//edgecase1
} else {
link potato = l -> head -> next;//backup
l -> head -> next = NULL;
l -> head = potato;
}
} else {
while (pos + 1 < index) {
curNode = curNode -> next;
pos++;
}
//link temp = curNode -> next;
if (curNode -> next != NULL) {
link temp2 = curNode -> next -> next;
curNode -> next -> next = NULL;
curNode -> next = temp2; //come back for edge case?
}
}
(testList,'e'); // no effect
(testList->head == nodeK);
(nodeK->next == NULL);
("test passed!\n");
printf
redact
assert
printf
(testList,'e'); // no effect
(testList->head == nodeK);
(nodeK->next == nodeI);
(nodeI->next == NULL);
("test passed!\n");
printf
redact
assert
assert
printf
redact
assert
assert
printf
(testList,'k'); // no effect
(testList->head == nodeO);
(nodeO->next == NULL);
("test passed!\n");
(testList,'i'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");
printf
redact
assert
assert
assert
printf
// no effect
redact
assert
assert
assert
printf
(testList,'n'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");
(testList,'e'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == NULL);
("test passed!\n");
(testList,'i'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == nodeC);
(nodeC->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");
printf
redact
assert
assert
assert
assert
printf
redact
assert
assert
assert
assert
printf
(testList,'n'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeC);
(nodeC->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");
(testList,'c'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");
(testList,'e'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == nodeC);
(nodeC->next == NULL);
("test passed!\n");
(testList,'i'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeM);
(nodeM->next == nodeW);
(nodeW->next == nodeO);
(nodeO->next == nodeR);
(nodeR->next == nodeK);
(nodeK->next == NULL);
("test passed!\n");
(testList,'t');
(testList->head
(nodeE->next ==
(nodeA->next ==
(nodeM->next ==
(nodeW->next ==
(nodeO->next ==
(nodeR->next ==
// no effect
== nodeE);
nodeA);
nodeM);
nodeW);
nodeO);
nodeR);
nodeK);
(testList,'m'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeW);
(nodeW->next == nodeO);
(nodeO->next == nodeR);
(nodeR->next == nodeK);
(nodeK->next == NULL);
("test passed!\n");
(testList,'w'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeM);
(nodeM->next == nodeO);
(nodeO->next == nodeR);
(nodeR->next == nodeK);
(nodeK->next == NULL);
("test passed!\n");
(testList,'o'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeM);
(nodeM->next == nodeW);
(nodeW->next == nodeR);
(nodeR->next == nodeK);
(nodeK->next == NULL);
("test passed!\n");
(testList,'r'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeM);
(nodeM->next == nodeW);
(nodeW->next == nodeO);
(nodeO->next == nodeK);
(nodeK->next == NULL);
("test passed!\n");
(testList,'k'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeM);
(nodeM->next == nodeW);
(nodeW->next == nodeO);
(nodeO->next == nodeR);
(nodeR->next == NULL);
("test passed!\n");
(nodeO->value
(nodeN->value
(nodeE->value
(nodeT->value
(nodeC->value
(nodeA->value
(nodeM->value
(nodeW->value
(nodeR->value
==
==
==
==
==
==
==
==
==
'o');
'n');
'e');
't');
'c');
'a');
'm');
'w');
'r');
assert
assert
assert
printf
(nodeR->value == 'r');
(nodeK->value == 'k');
(nodeI->value == 'i');
("test passed!\n");
/*
*
*
*
*
*
*
*
*/
Q2.c
10 marks
comp1917 practical-exam 2014s1
redact on a list which CAN contain duplicates
- same as Q1 except the input list might now contain duplicates
Stub by Richard Buckland on 21 June 2014
#include
#include
#include
#include
testQ2.c
comp1917 final prac exam
some unit tests for Q2.c
Richard Buckland: 21 june 2014
#include
#include
#include
#include
<stdlib.h>
<stdio.h>
<assert.h>
"list.h"
/*
*
*
*
*
*
*
*/
given a list "l" which might or might not contain any duplicates,
and given a character "secret", remove from the list any node
which has the "secret" character in its value field.
Constraints:
don't malloc any new nodes
don't free any nodes, even the nodes you remove from the list
don't change the "value" field of any node, just change links
<stdio.h>
<stdlib.h>
<assert.h>
"list.h"
/*
eg given the list
"F->F->R->E->D->*"
redacting F would change the list to be
"R->E->D->*"
return EXIT_SUCCESS;
}
/*
compile your code using:
gcc -Wall -Werror -std=c99 -o q2 -O q2.c testQ2.c
run it using
./q2
*/
int search (list l, char secret); //returns the index of the node(s) to be
redacted (if any)
void linkup (list l, int index); //uses the index returned from linkup
node
node
node
node
node
node
node
*nodeC1
*nodeE1
*nodeE2
*nodeE3
*nodeE4
*nodeE5
*nodeE6
=
=
=
=
=
=
=
newNode
newNode
newNode
newNode
newNode
newNode
newNode
('c');
('e');
('e');
('e');
('e');
('e');
('e');
node
node
node
node
node
*nodeO1
*nodeO2
*nodeO3
*nodeO4
*nodeO5
=
=
=
=
=
newNode
newNode
newNode
newNode
newNode
('o');
('o');
('o');
('o');
('o');
// remove both cs
}
}
return UNDEFINED;
redact
assert
assert
printf
}
void linkup (list l, int index) {
link curNode = l -> head;
int pos = 0;
if (index == 0) {
if (l -> head -> next == NULL) {
l -> head = NULL;//edgecase1
} else {
link potato = l -> head -> next;//backup
l -> head -> next = NULL;
l -> head = potato;
}
} else {
while (pos + 1 < index) {
curNode = curNode -> next;
pos++;
}
//link temp = curNode -> next;
if (curNode -> next != NULL) {
link temp2 = curNode -> next -> next;
curNode -> next -> next = NULL;
curNode -> next = temp2; //come back for edge case?
}
}
redact
assert
assert
assert
assert
assert
assert
assert
printf
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf
(testList,'e'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeC);
(nodeC->next == nodeC1);
(nodeC1->next == nodeU);
(nodeU->next == nodeP);
(nodeP->next == nodeI);
(nodeI->next == nodeD);
(nodeD->next == NULL);
("test passed!\n");
redact
assert
assert
assert
assert
assert
assert
printf
(testList,'c'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeU);
(nodeU->next == nodeP);
(nodeP->next == nodeI);
(nodeI->next == nodeD);
(nodeD->next == NULL);
("test passed!\n");
redact
redact
redact
assert
assert
assert
printf
(testList,'p'); // no effect
(testList,'u'); // no effect
(testList,'o'); // no effect
(testList->head == nodeI);
(nodeI->next == nodeD);
(nodeD->next == NULL);
("test passed!\n");
assert
assert
assert
assert
assert
assert
assert
(nodeO->value
(nodeC->value
(nodeU->value
(nodeP->value
(nodeI->value
(nodeE->value
(nodeD->value
==
==
==
==
==
==
==
assert
assert
assert
assert
assert
(nodeC1->value
(nodeE1->value
(nodeE2->value
(nodeE3->value
(nodeE4->value
'o');
'c');
'u');
'p');
'i');
'e');
'd');
==
==
==
==
==
'c');
'e');
'e');
'e');
'e');
(nodeO1->value
(nodeO2->value
(nodeO3->value
(nodeO4->value
(nodeO5->value
==
==
==
==
==
'o');
'o');
'o');
'o');
'o');
free
free
free
free
free
(nodeO1);
(nodeO2);
(nodeO3);
(nodeO4);
(nodeO5);
free
free
free
free
free
free
(nodeE1);
(nodeE2);
(nodeE3);
(nodeE4);
(nodeE5);
(nodeE6);
/////////////////////////////////
(testList,'e'); // no effect
(testList->head == nodeK);
(nodeK->next == NULL);
("test passed!\n");
printf
redact
assert
printf
redact
assert
assert
assert
printf
(testList,'e'); // no effect
(testList->head == nodeK);
(nodeK->next == nodeI);
(nodeI->next == NULL);
("test passed!\n");
printf
redact
assert
assert
printf
redact
assert
assert
printf
(testList,'k'); // no effect
(testList->head == nodeO);
(nodeO->next == NULL);
("test passed!\n");
(testList,'i'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");
printf
redact
assert
assert
assert
printf
(testList,'n'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");
(testList,'e'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == NULL);
("test passed!\n");
(testList,'i'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == nodeC);
(nodeC->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");
printf
redact
assert
assert
assert
assert
printf
(testList,'n'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeC);
(nodeC->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");
(testList,'c'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");
redact
assert
assert
assert
assert
printf
(testList,'e'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == nodeC);
(nodeC->next == NULL);
("test passed!\n");
(testList,'i'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeM);
(nodeM->next == nodeW);
(nodeW->next == nodeO);
(nodeO->next == nodeR);
(nodeR->next == nodeK);
(nodeK->next == NULL);
("test passed!\n");
nodeE->next
nodeA->next
nodeM->next
nodeW->next
nodeO->next
nodeR->next
nodeK->next
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf
=
=
=
=
=
=
=
nodeA;
nodeM;
nodeW;
nodeO;
nodeR;
nodeK;
NULL;
(testList,'t'); // no effect
(testList->head == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeM);
(nodeM->next == nodeW);
(nodeW->next == nodeO);
(nodeO->next == nodeR);
(nodeR->next == nodeK);
(nodeK->next == NULL);
("test passed!\n");
(testList,'m'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeW);
(nodeW->next == nodeO);
(nodeO->next == nodeR);
(nodeR->next == nodeK);
(nodeK->next == NULL);
("test passed!\n");
(testList,'w'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeM);
(nodeM->next == nodeO);
(nodeO->next == nodeR);
(nodeR->next == nodeK);
(nodeK->next == NULL);
("test passed!\n");
(testList,'o'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeM);
(nodeM->next == nodeW);
(nodeW->next == nodeR);
(nodeR->next == nodeK);
(nodeK->next == NULL);
("test passed!\n");
(testList,'r'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeM);
(nodeM->next == nodeW);
(nodeW->next == nodeO);
(nodeO->next == nodeK);
(nodeK->next == NULL);
("test passed!\n");
(testList,'k'); // no effect
(testList->head == nodeT);
(nodeT->next == nodeE);
(nodeE->next == nodeA);
(nodeA->next == nodeM);
(nodeM->next == nodeW);
(nodeW->next == nodeO);
(nodeO->next == nodeR);
(nodeR->next == NULL);
("test passed!\n");
(nodeO->value == 'o');
(nodeN->value == 'n');
(nodeE->value == 'e');
(nodeT->value == 't');
(nodeC->value == 'c');
(nodeA->value == 'a');
(nodeM->value == 'm');
(nodeW->value == 'w');
(nodeR->value == 'r');
(nodeK->value == 'k');
(nodeI->value == 'i');
("test passed!\n");
*2013s1
Tuesday, 23 June 2015
11:55 PM