You are on page 1of 39

Linked lists

Wednesday, 20 May 2015

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

Linked Lists Page 1

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

Linked Lists Page 2

numItems()
Tuesday, 23 June 2015

9:20 PM

int numItems (list l) {

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;
}

Linked Lists Page 3

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;
}

Linked Lists Page 4

showList()
Tuesday, 23 June 2015

10:59 PM

void showList (list l) {


assert (l !=NULL);
// start at the first node
link current = l->head;
while (current != NULL) {
printf ("[%d] -> ", current->value);
current = current->next;
}
printf ("X\n");
}

Linked Lists Page 5

append()
Tuesday, 23 June 2015

10:50 PM

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 {
while (curNode -> next != NULL) {
curNode = curNode -> next;
}
curNode -> next = potato;
}
}

Linked Lists Page 6

search()
Tuesday, 23 June 2015

11:03 PM

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;
}

Linked Lists Page 7

reverse()
Tuesday, 23 June 2015

11:05 PM

// Given a list, reverse it!


// Do recommend you draw it out on a piece of paper =P
void reverse(List l) {
Node tempNode = NULL;
Node prevNode = NULL;
Node curNode = l->head;
while (curNode != NULL) {
tempNode = curNode->next; //backup the next one
curNode->next = prevNode; //makes this one point backwards

//temp is a backup of next node

prevNode = curNode; //leave this one behind


curNode = tempNode; //move along (using backup)
}

Taking canopy

Pac-ket

Linked Lists Page 8

Archives
Tuesday, 23 June 2015

9:25 PM

Linked Lists Page 9

Basics
Tuesday, 23 June 2015

9:25 PM

Linked Lists Page 10

list3-core
Tuesday, 23 June 2015

9:25 PM

.c

.h

tests.c

//functions go here

// a concrete linked list


// list is a pointer to a struct - containing the head ptr

// a concrete linked list

#include <stdio.h>
#include <stdlib.h>
#include "list3-core.h"

typedef struct _node *link;

// print out a list


void showList (list listToPrint);

typedef struct _node {


int value;
link next;
} node;

// inset item at the front of the list


void frontInsert (list l, int item);
// count the number of items in the list
int numItems (list l);
// insert at end of list
void append (list l, int value);

// a link points ot a node

link potato = malloc(sizeof(node)); //make a new node (to be inserted at front)


link tempNode = l -> head;
//backup the original 1st node, we are going to cut the link from head to 1st node

<stdlib.h>
<stdio.h>
<assert.h>
"list3-core.h"

You are awesome\n");

return EXIT_SUCCESS;
}

// print out a list


void showList (list listToPrint);

void frontInsert (list l, int item) {


//
link curNode = l -> head; //curNode now points to first item in list

#include
#include
#include
#include

// simple unit tests on the list


// a list is represented by a pointer to a struct which contains static void testList (void);
// a pointer to the first node of the list called the "head"
typedef struct _list {
int main (int argc, char *argv[]) {
link head;
printf ("Testing list3-core...\n");
} *list;
testList();
printf ("All list3-core tests passed!

// find the value stored at position i


// i MUST be a valid position in the list
// dont call this on positions outside the list
int lookup (list l, int position);
void showList (list listToPrint) {
link curNode = listToPrint -> head;
if (curNode != NULL) {//safeguard against edge case
while (curNode -> next != NULL) {
printf ("%d\n", curNode -> value);
curNode = curNode -> next;
}
}
}

// list is a pointer to a struct - containing the head ptr


// 18 may 2014 tests expanded. includes tests on append to
// explicitly detect non-null "next" in the appended node.

static void testList (void) {


// create empty lst
list myList = malloc (sizeof (*myList));
myList->head = NULL;
// myList is a struct which has an element head which
// points to the first node of the list aka the head

// inset item at the front of the list


void frontInsert (list l, int item);

// count the number of items in the list


int numItems (list l);

// simple test on showlist and numItems


showList (myList);
assert (numItems (myList) == 0);

// insert at end of list


void append (list l, int value);

// attach one node to the list


link ptrToNewNode = malloc (sizeof (node));
assert (ptrToNewNode != NULL);
ptrToNewNode->value = 1;
ptrToNewNode->next = NULL;

// find the value stored at position i


// i MUST be a valid position in the list
// dont call this on positions outside the list
int lookup (list l, int position);

myList->head = ptrToNewNode;

if (tempNode == NULL) { //edge case


l -> head = potato;
} else {
//link tempNode = l -> head;
//backup the original 1st node, we are going to cut the link from head to 1st node

// simple test on showlist and numItems


showList (myList);
assert (numItems (myList) == 1);
// attach a second node to the list
ptrToNewNode = malloc (sizeof (node));
assert (ptrToNewNode != NULL);

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);

// insert a node containing 0 at te head, ie before


// the 1->2->X list, to produce 0->1->2->X
frontInsert (myList, 0);
assert (myList != NULL);
assert (myList->head != NULL);
assert (myList->head->value == 0);
assert (myList->head->next != NULL);
assert (myList->head->next->value == 1);

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

while (curNode -> next != NULL) {


curNode = curNode -> next;
}
curNode -> next = potato;

(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);

}
}

int lookup (list l, int position) {


//since only valid positions can be used, there is no edge case
link curNode = l -> head;
int pos = 0;
while (pos < position) {
curNode = curNode -> next;
pos++;
}
//
curNode = curNode -> next;
return curNode -> value;
}

Linked Lists Page 11

// 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

// a concrete linked list


// list is a pointer to a struct - containing the head ptr
// create an empty list
typedef struct _node *link;
list newList (void);

// a list is represented by a pointer to a struct which contains


// a pointer to the first node of the list called the "head"
typedef struct _list {
link head;
} *list;

// delete the end node


void deleteLast (list l);

// join two lists


// create an empty list

// 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);

// delete the end node


void deleteLast (list l);

void deleteLast (list l) {


if (l -> head == NULL){ //edge case: empty list
//redundancy whoops
} else {
link curNode = l -> head;
if (curNode -> next == NULL) { //edge case: single node
free(curNode);
l -> head = NULL;
} else {
while (curNode -> next -> next != NULL) { //travel to 2nd last node
curNode = curNode -> next; //move along by one node
}
//we are now at the second last node
//ie curNode -> next -> next == NULL
free (curNode -> next);
curNode -> next = NULL;
}
}
}

void deleteHead (list l) {


if (l -> head == NULL) { //edge case: empty list
//redundancy whoops
} else {
link curNode = l -> head; //travel to first node

if (curNode -> next == NULL) { //edge case: single node


free(curNode);
l -> head = NULL;
} else {
curNode = curNode -> next; //move along by one node
free (l -> head); //after 2nd node is saved to current node, free 1st node
l -> head = curNode;
}

static void testList (void);


int main (int argc, char *argv[]) {
printf ("Testing list3-extraFns...\n");
testList();
printf ("All list3-extraFns tests passed!

You are awesome\n");

return EXIT_SUCCESS;
}

static void testList (void) {


printf ("testing newList()\n");
list testList = newList();
assert (testList->head == NULL);

list newList (void);

// delete the first node


void deleteHead (list l);

list newList (void) {


list newList = malloc(sizeof(list));
newList -> head = NULL;
return newList;
}

<stdio.h>
<stdlib.h>
<assert.h>
"list3-extraFns.h"

// a link points ot a node

typedef struct _node {


int value;
link next;
} node;

// delete the first node


void deleteHead (list l);

#include
#include
#include
#include

printf ("testing deleteHead()\n");


// create a 3 element list
testList->head = malloc (sizeof (node));
testList->head->value = 3;
testList->head->next = malloc (sizeof (node));
testList->head->next->value = 1;
testList->head->next->next = malloc (sizeof (node));
testList->head->next->next->value = 4;
testList->head->next->next->next = NULL;

// join two lists


deleteHead (testList);
// 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);

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);

printf ("testing deleteLast()\n");


// create a 3 element list
testList->head = malloc (sizeof (node));
testList->head->value = 3;
testList->head->next = malloc (sizeof (node));
testList->head->next->value = 1;
testList->head->next->next = malloc (sizeof (node));
testList->head->next->next->value = 4;
testList->head->next->next->next = NULL;
deleteLast (testList);
assert
assert
assert
assert
assert
assert

(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);

void concatenate (list to, list from) {


link curNode = to -> head;
if (curNode == NULL) { //edge case: empty list 1
to -> head = from -> head;
} else {
while (curNode -> next != NULL) { //move to end of 'list to'
curNode = curNode -> next; //move along by one node
}
//now we are at the last node of list 1
curNode -> next = from -> head;
}
from -> head = NULL;
}
int count42s (list l) {
link curNode = l -> head;
int count42 = 0;
if (curNode == NULL) {
//lol
} else {

while (curNode -> next != NULL) { //move to end of 'list to'


if (curNode -> value == 42) {
count42++;
}
curNode = curNode -> next; //move along by one node
}
if (curNode -> value == 42) {
count42++;
}
}
return count42;
}

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);

printf ("testing concatenate()\n");

// (3,2) -> (5,0)


// create a 3 element list
testList->head = malloc (sizeof (node));
testList->head->value = 3;
testList->head->next = malloc (sizeof (node));
testList->head->next->value = 1;
testList->head->next->next = malloc (sizeof (node));
testList->head->next->next->value = 4;
testList->head->next->next->next = NULL;
// create a two element list
list testListB = newList();
testListB->head = malloc (sizeof (node));
testListB->head->value = 1;
testListB->head->next = malloc (sizeof (node));
testListB->head->next->value = 5;
testListB->head->next->next = NULL;

concatenate (testList, testListB);


assert (testListB != NULL);
assert (testListB->head == NULL);
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert

(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);

// (5,0) -> (5,0)


concatenate (testList, testListB);
assert (testListB != NULL);
assert (testListB->head == NULL);
assert (testList != NULL);
assert (testList->head != NULL);
assert (testList->head->value == 3);

Linked Lists Page 12

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);

// (0,5) -> (5,0)


concatenate (testListB, testList);
assert (testList != NULL);
assert (testList->head == NULL);
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert

(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);

printf ("testing count42s()\n");


assert (count42s (testList) == 0);
assert (count42s (testListB) == 0);
testListB->head->value = 42;
assert (count42s (testListB) == 1);
testListB->head->next->value = 42;
assert (count42s (testListB) == 2);
testListB->head->next->next->value = 42;
assert (count42s (testListB) == 3);
testListB->head->next->next->next->value = 42;
assert (count42s (testListB) == 4);
testListB->head->next->next->next->next->value = 42;
assert (count42s (testListB) == 5);

Linked Lists Page 13

Prac prac prac exams


Tuesday, 23 June 2015

10:06 PM

Linked Lists Page 14

Monday
Tuesday, 23 June 2015

10:07 PM

.c

.h

//Written by Michael Simarta


//With the help of Bernice Chen
//list.c file that implements all functions declared in list.h
//implement the median() function here

//Written by Michael Simarta


//With the help of Bernice Chen
//header file for a Linked List structure
//contains functions that can be implemented on a Linked List

#include
#include
#include
#include

typedef struct _node* Node;


typedef struct _list* List;

<stdio.h>
<stdlib.h>
<assert.h>
"List.h"

//returns a new list of length 0


List newList();
//frees everything malloced for the list
void destroy(List l);
//appends a node of value to the end of the list
void append(List l, int value);
//returns the int value of the node at index
//assumes input index is within range of the list's length
int getValue(List l, int index);

#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);

//find the median of the list


//First, you can assume that the sorted list and has an odd number of
elements.
//
//Then, the list is still sorted but may have even or odd number of
elements.
int median(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");
}

//function to test the median() function


void test_median() {
printf("TESTING median()...\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

Linked Lists Page 15

<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);
}

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;
}
//returns a new list of length 0
List newList() {
List l = malloc(sizeof(list));
assert ( l!=NULL );
l->head = NULL;
return l;
}
//frees everything malloced for the list
void destroy(List l) {
Node curNode = l->head;
Node prevNode = NULL;

while (curNode != NULL) {


prevNode = curNode;
curNode = prevNode->next;
free(prevNode);
}
free(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;
}
}

//returns the int value of the node at index


//assumes input index is within range of the list's length
int getValue(List l, int index) {
Node curNode = l->head;
assert (curNode!=NULL);
int counter = 0;
while (counter<index) {
curNode = curNode->next;
counter++;
}
return curNode->value;
}

Linked Lists Page 16

*Tuesday
Tuesday, 23 June 2015

10:07 PM

Linked Lists Page 17

Friday
Tuesday, 23 June 2015

10:06 PM

.c

.h

test.c

//Written by Michael Simarta


//With the help of Bernice Chen
//List3.c file that implements all functions declared in List3.h
//implement the insert() functions here

//Written by Michael Simarta


//With the help of Bernice Chen
//header file for a Linked List structure
//contains functions that can be implemented on a Linked List

// Written by Michael Simarta


// With the help of Bernice Chen.
//
//run this testList3.c file with a compilable List3.c
//that should have the functions declared in List3.h implemented

#include
#include
#include
#include

typedef struct _node* Node;


typedef struct _list* List;

<stdio.h>
<stdlib.h>
<assert.h>
"List3.h"

//returns a new list of length 0


List newList();
//frees everything malloced for the list
void destroy(List l);
//appends a node of value to the end of the list
void append(List l, int value);
//returns the int value of the node at index
//assumes input index is within range of the list's length
int getValue(List l, int index);

typedef struct _node {


int value;
Node next;
Node prev;
} node;
typedef struct _list {
Node head;
Node tail;
} list;
//TODO
//Read List3.h to help you look at the hints
//insert nodes into the list so it is in increasing order from head to tail
//and decreasing order from tail to head (You need to uncomment the commented test lines in testList3.c)
void insert(List l, int value) {
Node new = newNode(value);
Node curNode = l->head;
Node tempNode = NULL;
//we are currently at head node

#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<assert.h>
"List3.h"

typedef struct _node {


int value;
Node next;
Node prev;
} node;

int getValueReverse(List l, int index);

typedef struct _list {


Node head;
Node tail;
} list;

//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);

//makes a new node


Node newNode (int value);

int main( int argc, char * argv[] ) {


printf("......STARTING TESTS...... \n");

//

test_test();
test_insert();
test_median();

printf("ALL TESTS PASSED. YOU ARE AWESOME! \n");


//

//
//

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)

//print List from head to tail


void printList(List l);
//print List from tail to head
void printListReverse(List l);

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);

printf("EXISTING FUNCTIONS WORK FINE. \n");


}

//function to test the median() function


void test_insert() {
printf("TESTING insert()... \n");
// Empty List
//NULL
printf("Doing Test1...\n");
List l = newList();
assert (l->head == NULL);
assert (l->tail == NULL);
destroy(l);
printf("insert Test1 passed. \n");
// 1 Node List
// NULL <-> 5 <-> NULL
printf("Doing Test2...\n");
l = newList();
insert(l, 5);
printf("Expecting [HEAD] ->[5]->[NULL]\n");
printList(l);
assert ( getValue(l, 0) == 5);
assert (l->head->next == NULL);

//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;
}

printf("Expecting [TAIL] ->[5]->[NULL]\n");


printListReverse(l);
//assert ( getValueReverse(l, 0) == 5);
//assert (l->tail->prev == NULL);
destroy(l);
printf("insert Test2 passed. \n");

//frees everything malloced for the list


void destroy(List l) {
Node curNode = l->head;
Node prevNode = NULL;
while (curNode != NULL) {
prevNode = curNode;
curNode = prevNode->next;
free(prevNode);
}

// 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);
}

Node newNode (int value) {


Node n = malloc(sizeof(node));
n->value = value;
n->next = NULL;
n->prev = NULL;
return n;
}
//appends a node of value to the end of the list (only goes forward)
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;
}
}

printf("Expecting [TAIL] ->[7]->[5]->[2]->[NULL]\n");


printListReverse(l);
//assert ( getValueReverse(l, 0) == 7);
//assert ( getValueReverse(l, 1) == 5);
//assert ( getValueReverse(l, 2) == 2);
//assert (l->tail->prev->prev->prev == NULL);
destroy(l);
printf("insert Test3 passed. \n");

// 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);

//returns the int value of the node at index


//assumes input index is within range of the list's length
int getValue(List l, int index) {
Node curNode = l->head;
assert (curNode!=NULL);
int counter = 0;
while (counter<index) {
curNode = curNode->next;
counter++;
}
return curNode->value;

printf("Expecting [TAIL] ->[6]->[4]->[2]->[1]->[NULL]\n");


printListReverse(l);
assert ( getValueReverse(l, 0) == 6);
assert ( getValueReverse(l, 1) == 4);
assert ( getValueReverse(l, 2) == 2);
assert ( getValueReverse(l, 3) == 1);
assert (l->tail->prev->prev->prev->prev == NULL);
destroy(l);
printf("insert Test4 passed. \n");

int getValueReverse(List l, int index) {


Node curNode = l->tail;
assert (curNode!=NULL);
int counter = 0;
while (counter<index) {
curNode = curNode->prev;
counter++;
}
return curNode->value;

//print List from head to tail


void printList(List l) {
Node curNode = l->head;
printf("[HEAD]->");
while (curNode!=NULL) {
printf ("[%d]->", curNode->value);
curNode = curNode->next;
}
printf("[NULL]\n");
}
//print List from tail to head
void printListReverse(List l) {
Node curNode = l->tail;

Linked Lists Page 18

Node curNode = l->tail;


printf("[TAIL]->");
while (curNode!=NULL) {
printf ("[%d]->", curNode->value);
curNode = curNode->prev;
}
printf("[NULL]\n");
}

Linked Lists Page 19

Prac prac exams


Tuesday, 23 June 2015

10:07 PM

Linked Lists Page 20

May 25 (frontBackSplit)
Tuesday, 23 June 2015

10:07 PM

.c

.h

// frontBackSplit.c
//

typedef struct _node *link;

test.c
// a link points to a node

typedef struct _node {


int value;
link next;
} node;

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "list-ppexam.h"

//
//
//
//

given a list of nodes, split it into two sublists


- one for the front half, and one for the back half.
If the number of elements is odd, the extra element
should go in the front list.

//
//
//
//
//
//
//
//
//
//
//

The first argument (sourceList) is the original list.


The second argument (frontList) which is initally empty
should point to the front half of the list.
The third argument (backList) which is initially empty
should point to the back half of the list.
The original list (sourceList) should be empty after the split.

//
//
//
//
//
//
//
//
//
//
//

e.g. if sourceList is 1->4->6->4->3->X then


after execution of the function frontBackSplit()
frontList should be 1->4->6->X and
backList should be 4->3->X and
sourceList should be X (i.e. empty)

// a list is represented by a pointer to a struct which contains


// a pointer to the first node of the list called the "head"
typedef struct _list {
link head;
} *list;

#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);

int main (int argc, char * argv[]){


int numItems (list l); //implementation of this function is provided
void frontBackSplit (list sourceList, list frontList, list backList);

testList();
printf("All tests passed ! You are awesome\n");
return EXIT_SUCCESS;
}

void testList (void){


Constraints:
don't malloc any new nodes
don't change the "value" field of any node, just change links
don't delete any nodes (i.e. do not call free())

and if sourceList is 3->10->15->25->X then


after execution of the function frontBackSplit()
frontList should be 3->10->X and
backList should be 15->25->X
sourceList should be X (i.e. empty)

// A function to determine the length of a list called numItems()


// is provided. Fell free to use it but do not modify the function.

//function to determine length of a linked list


int numItems (list l) {

int length = 0;
link current = l->head;
while (current != NULL){
length++;
current = current->next;
}
return length;
}

// test a list with no nodes in it


printf ("test an empty list\n");
list sourceList = malloc (sizeof (*sourceList));
assert (sourceList !=NULL);
sourceList->head = NULL;

list frontList = malloc (sizeof (*frontList));


assert (frontList !=NULL);
frontList->head = NULL;

list backList = malloc (sizeof (*backList));


assert (backList !=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 (frontList->head == NULL);


assert (backList->head == NULL);
assert (sourceList->head == NULL);
// create 10 nodes on the stack
node nodes[10];

void frontBackSplit (list sourceList, list frontList, list backList) {


// put your code here
link curNode = sourceList -> head;
link fcurNode = frontList -> head;
link bcurNode = backList -> head;

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

// test a list with one node in it


printf ("\ntest a list containing one node\n");
sourceList->head = &nodes[0];
sourceList->head->value = 42;
sourceList->head->next = 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 (frontList->head == &nodes[0]);


assert (frontList->head->value == 42);
assert (frontList->head->next == NULL);
assert (backList->head == NULL);
assert (sourceList->head == NULL);

// test a list with two nodes in it


printf ("\ntest a list containing two nodes\n");
sourceList->head = &nodes[0];
nodes[0].value = 73;
nodes[1].value = 21;
nodes[0].next = &nodes[1];
nodes[1].next = NULL;
frontList->head = NULL;
backList->head = NULL;
printf ("before split..\n");
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 (frontList->head == &nodes[0]);


assert (frontList->head->value == 73);
assert (frontList->head->next == NULL);

assert (backList->head == &nodes[1]);


assert (backList->head->value == 21);
assert (backList->head->next == NULL);

assert (sourceList->head == NULL);

// test a list with three nodes in it


printf ("\ntest a list containing three nodes\n");
sourceList->head = &nodes[0];
nodes[0].value = 1;
nodes[1].value = 11;
nodes[2].value = 101;
nodes[0].next = &nodes[1];
nodes[1].next = &nodes[2];
nodes[2].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: ");

Linked Lists Page 21

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 == 1);
(frontList->head->next == &nodes[1]);
(frontList->head->next->value == 11);
(frontList->head->next->next == NULL);

assert (backList->head == &nodes[2]);


assert (backList->head->value == 101);
assert (backList->head->next == NULL);

assert (sourceList->head == NULL);

// test a list with four nodes in it


printf ("\ntest a list containing four nodes\n");
sourceList->head = &nodes[0];
nodes[0].value = 45;
nodes[1].value = 33;
nodes[2].value = 12;
nodes[3].value = 444;
nodes[0].next = &nodes[1];
nodes[1].next = &nodes[2];
nodes[2].next = &nodes[3];
nodes[3].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);

assert (sourceList->head == NULL);

// test a list with nine nodes in it


printf ("\ntest a list containing nine nodes\n");
sourceList->head = &nodes[0];
nodes[0].value = 3;
nodes[1].value = 65;
nodes[2].value = 72;
nodes[3].value = 3;
nodes[4].value = 0;
nodes[5].value = 232;
nodes[6].value = 21;
nodes[7].value = 7;
nodes[8].value = 99;
nodes[0].next = &nodes[1];
nodes[1].next = &nodes[2];
nodes[2].next = &nodes[3];
nodes[3].next = &nodes[4];
nodes[4].next = &nodes[5];
nodes[5].next = &nodes[6];
nodes[6].next = &nodes[7];
nodes[7].next = &nodes[8];
nodes[8].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);

assert (sourceList->head == NULL);


// test a list with ten nodes in it
printf ("\ntest a list containing ten nodes\n");
sourceList->head = &nodes[0];
nodes[0].value = 66;
nodes[1].value = 808;
nodes[2].value = 72;
nodes[3].value = 2;
nodes[4].value = 1;
nodes[5].value = 2;
nodes[6].value = 1;
nodes[7].value = 66;
nodes[8].value = 99999;
nodes[9].value = 1003;
nodes[0].next = &nodes[1];
nodes[1].next = &nodes[2];
nodes[2].next = &nodes[3];
nodes[3].next = &nodes[4];
nodes[4].next = &nodes[5];
nodes[5].next = &nodes[6];
nodes[6].next = &nodes[7];

Linked Lists Page 22

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);

assert (sourceList->head == NULL);

void showList (list l) {


assert (l !=NULL);
// start at the first node
link current = l->head;
while (current != NULL) {
printf ("[%d] -> ", current->value);
current = current->next;
}
printf ("X\n");
}

Linked Lists Page 23

*June 10
Tuesday, 23 June 2015

10:11 PM

Linked Lists Page 24

*2014
Wednesday, 24 June 2015

12:33 AM

Linked Lists Page 25

*2013
Wednesday, 24 June 2015

12:33 AM

Linked Lists Page 26

*2013_2
Wednesday, 24 June 2015

12:33 AM

Linked Lists Page 27

*selectEven
Wednesday, 24 June 2015

12:33 AM

Linked Lists Page 28

Prac Exams
Tuesday, 23 June 2015

10:08 PM

Linked Lists Page 29

2014s1 (redacted)
Tuesday, 23 June 2015

10:08 PM

.c
Q1

/*
*
*
*
*
*
*
*
*/

.h

test.c

// don't alter this file


Q1.c
10 marks
comp1917 practical-exam 2014s1
redact on a list which does not contain duplicates

Stub by Richard Buckland on 21 June 2014

#include
#include
#include
#include

<stdlib.h>
<stdio.h>
<assert.h>
"list.h"

typedef struct _node *link;

// a link points ot a node

typedef struct _node {


int value;
link next;
} node;
// a list is represented by a pointer to a struct containing
// a pointer to the first node in it
typedef struct _list {
link head;
} *list;

/*
*
*
*
*
*
*
*/

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"

static void testQ1 (void);

#define UNDEFINED -999999


void redact (list l, char secret);
//
//
//
//
//
//
//
//

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.

static node *newNode (int item);

int main (int argc, const char * argv[]) {

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->*"

printf ("About to test Q1.c the FIRST redact function\n");


testQ1();
printf ("\nAll Q1.c redact tests passed! You are Awesome!\n\n");
printf ("1. Submit your Q1.c solution\n");
printf (" and then \n");
printf ("2. Implement the modified redact #2 function in Q2.c\n");
return EXIT_SUCCESS;
}

void testQ1 (void) {


eg given the list
"F->R->E->D->*"
redacting D would change the list to be
"F->R->E->*"
eg given the list
"F->R->E->D->*"
redacting M would leave the list unchanged
"F->R->E->D->*"

// create 10 nodes, names indicate their value, next=NULL


node *nodeO = newNode ('o');
node *nodeN = newNode ('n');
node *nodeE = newNode ('e');
node *nodeT = newNode ('t');
node *nodeC = newNode ('c');
node *nodeA = newNode ('a');
node *nodeM = newNode ('m');
node *nodeW = newNode ('w');
node *nodeR = newNode ('r');
node *nodeK = newNode ('k');
node *nodeI = newNode ('i');

see the test file for more examples


printf ("testing
list testList =
assert (testList
testList->head =

*/

/*
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?
}
}

redact on empty list \n");


malloc (sizeof (list*));
!= NULL);
NULL;

redact (testList, 'e'); // == NULL


assert (testList->head == NULL);
printf ("test passed!\n");
printf ("testing a one letter list - redact nothing\n");
testList->head = nodeK;
nodeK->next = NULL;
redact
assert
assert
printf

(testList,'e'); // no effect
(testList->head == nodeK);
(nodeK->next == NULL);
("test passed!\n");

printf
redact
assert
printf

("testing a one letter list - redact one\n");


(testList,'k'); // no effect
(testList->head == NULL);
("test passed!\n");

printf ("testing a two letter list - redact nothing\n");


testList->head = nodeK;
nodeK->next = nodeI;
nodeI->next = NULL;
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

("testing a two letter list - redact first\n");


(testList,'k'); // no effect
(testList->head == nodeI);
(nodeI->next == NULL);
("test passed!\n");

printf ("testing a two letter list - redact last\n");


testList->head = nodeO;
nodeO->next = nodeK;
nodeK->next = NULL;

redact
assert
assert
printf

(testList,'k'); // no effect
(testList->head == nodeO);
(nodeO->next == NULL);
("test passed!\n");

printf ("testing a three letter list - redact nothing\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeE;
nodeE->next = NULL;
redact
assert
assert
assert
assert
printf

(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

("testing a three letter list - redact first\n");


(testList,'o'); // no effect
(testList->head == nodeN);
(nodeN->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");

printf ("testing a three letter list - redact middle\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeE;
nodeE->next = NULL;
redact (testList,'n');
Linked Lists Page 30

// no effect

redact
assert
assert
assert
printf

(testList,'n'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");

printf ("testing a three letter list - redact last\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeE;
nodeE->next = NULL;
redact
assert
assert
assert
printf

(testList,'e'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == NULL);
("test passed!\n");

printf ("testing a four letter list - redact nothing\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeC;
nodeC->next = nodeE;
nodeE->next = NULL;
redact
assert
assert
assert
assert
assert
printf

(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

("testing a four letter list - redact first\n");


(testList,'o'); // no effect
(testList->head == nodeN);
(nodeN->next == nodeC);
(nodeC->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");

printf ("testing a four letter list - redact second\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeC;
nodeC->next = nodeE;
nodeE->next = NULL;

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");

printf ("testing a four letter list - redact third\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeC;
nodeC->next = nodeE;
nodeE->next = NULL;
redact
assert
assert
assert
assert
printf

(testList,'c'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");

printf ("testing a four letter list - redact last\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeC;
nodeC->next = nodeE;
nodeE->next = NULL;
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");

printf ("testing a long list - redact none\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing a long list - redact first\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert

Linked Lists Page 31

(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);

assert (nodeR->next == nodeK);


assert (nodeK->next == NULL);
printf ("test passed!\n");

printf ("testing a long list - redact m\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing a long list - redact w\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing a long list - redact o\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing a long list - redact r\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing a long list - redact last\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing node values have not changed\n");


assert
assert
assert
assert
assert
assert
assert
assert
assert

Linked Lists Page 32

(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");

printf ("testing that no nodes have been freed\n...");


// free the nodes - not needed in subsequent tests
// might fail if students have inadventantly freed nodes
// already
free (nodeO);
free (nodeN);
free (nodeE);
free (nodeT);
free (nodeC);
free (nodeA);
free (nodeM);
free (nodeW);
free (nodeR);
free (nodeK);
free (nodeI);
printf ("test passed!\n");
}

static node *newNode (int item) {


node *new = malloc (sizeof (node));
assert (new != NULL);
new->value = item;
new->next = NULL;
return new;
}
Q2

/*
*
*
*
*
*
*
*
*/

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"

#define UNDEFINED -999999


//
//
//
//
//
//
//

/*
*
*
*
*
*
*
*/

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"

static void testQ2unique (void);


static void testQ2duplicates (void);

static node *newNode (int item);

int main (int argc, const char * argv[]) {


printf ("About to test the Q2 redact function\n");
printf ("(testing on lists which have no duplicates)\n");
testQ2unique();
printf ("All no dups tests passed - well done.\n");
printf ("About to test the Q2 redact function\n");
printf ("(testing on lists which have duplicates)\n");
testQ2duplicates();
printf ("All Q2 redact tests passed - well done.\n");
printf ("You are Awesome!\n\n");

/*
eg given the list
"F->F->R->E->D->*"
redacting F would change the list to be
"R->E->D->*"

return EXIT_SUCCESS;
}

eg given the list


"F->E->R->E->D->*"
redacting E would change the list to be
"F->R->D->*"

eg given the list


"F->R->E->D->D->D*"
redacting M would leave the list unchanged
"F->R->E->D->D->D*"

see the test file for more examples


*/

/*
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

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
while (search(l, secret) != UNDEFINED) {
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;
}

Linked Lists Page 33

void testQ2duplicates (void) {


// create test
node *nodeO =
node *nodeC =
node *nodeU =
node *nodeP =
node *nodeI =
node *nodeE =
node *nodeD =

nodes, names indicate their value, next=NULL


newNode ('o');
newNode ('c');
newNode ('u');
newNode ('p');
newNode ('i');
newNode ('e');
newNode ('d');

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');

list testList = malloc (sizeof (list*));


assert (testList != NULL);

printf ("testing a two letter list - redact both\n");


testList->head = nodeC;
nodeC->next = nodeC1;
nodeC1->next = NULL;

redact (testList,'c'); // remove all nodes


assert (testList->head == NULL);
printf ("test passed!\n");

printf ("testing a three letter list - leave first\n");


testList->head = nodeO;
nodeO->next = nodeC;
nodeC->next = nodeC1;
nodeC1->next = NULL;
redact
assert
assert
printf

(testList,'c'); // remove both cs


(testList->head == nodeO);
(nodeO->next == NULL);
("test passed!\n");

printf ("testing a three letter list - leave middle\n");


testList->head = nodeC;
nodeC->next = nodeO;
nodeO->next = nodeC1;
nodeC1->next = NULL;
redact (testList,'c');

// 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?
}
}

printf ("testing a three letter list - leave last\n");


testList->head = nodeC;
nodeC->next = nodeC1;
nodeC1->next = nodeO;
nodeO->next = NULL;
redact
assert
assert
printf

(testList,'c'); // remove both cs


(testList->head == nodeO);
(nodeO->next == NULL);
("test passed!\n");

printf ("testing an alternating list - redact odd places\n");


testList->head = nodeE1;
nodeE1->next = nodeO1;
nodeO1->next = nodeE2;
nodeE2->next = nodeO2;
nodeO2->next = nodeE3;
nodeE3->next = nodeO3;
nodeO3->next = nodeE4;
nodeE4->next = nodeO4;
nodeO4->next = nodeE5;
nodeE5->next = nodeO5;
nodeO5->next = nodeE6;
nodeE6->next = NULL;

redact
assert
assert
assert
assert
assert
assert
assert
printf

(testList,'c'); // remove both cs


(testList->head == nodeO);
(nodeO->next == NULL);
("test passed!\n");

(testList,'o'); // remove odd places


(testList->head == nodeE1);
(nodeE1->next == nodeE2);
(nodeE2->next == nodeE3);
(nodeE3->next == nodeE4);
(nodeE4->next == nodeE5);
(nodeE5->next == nodeE6);
(nodeE6->next == NULL);
("test passed!\n");

printf ("testing an alternating list - redact even places\n");


testList->head = nodeE1;
nodeE1->next = nodeO1;
nodeO1->next = nodeE2;
nodeE2->next = nodeO2;
nodeO2->next = nodeE3;
nodeE3->next = nodeO3;
nodeO3->next = nodeE4;
nodeE4->next = nodeO4;
nodeO4->next = nodeE5;
nodeE5->next = nodeO5;
nodeO5->next = nodeE6;
nodeE6->next = NULL;
redact
assert
assert
assert
assert
assert
assert
printf

(testList,'e'); // remove odd places


(testList->head == nodeO1);
(nodeO1->next == nodeO2);
(nodeO2->next == nodeO3);
(nodeO3->next == nodeO4);
(nodeO4->next == nodeO5);
(nodeO5->next == NULL);
("test passed!\n");

printf ("redacting a long list with duplicates 1\n");


testList->head = nodeO;
nodeO->next = nodeC;
nodeC->next = nodeC1;
nodeC1->next = nodeU;
nodeU->next = nodeP;
nodeP->next = nodeI;
nodeI->next = nodeE;
nodeE->next = nodeD;
nodeD->next = nodeE1;
nodeE1->next = nodeE2;
nodeE2->next = nodeE3;
nodeE3->next = NULL;

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");

printf ("redacting a long list with duplicates 2\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");

printf ("redacting a long list with duplicates 3\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");

printf ("testing node values have not changed\n");

Linked Lists Page 34

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');

assert (nodeE4->value == 'e');


assert (nodeE5->value == 'e');
assert (nodeE6->value == 'e');
assert
assert
assert
assert
assert

(nodeO1->value
(nodeO2->value
(nodeO3->value
(nodeO4->value
(nodeO5->value

==
==
==
==
==

'o');
'o');
'o');
'o');
'o');

printf ("test passed!\n");

printf ("testing that no nodes have been freed\n...");


// free the nodes - not needed in subsequent tests
// might fail if students have inadventantly freed nodes
// already
free (nodeO);
free (nodeC);
free (nodeU);
free (nodeP);
free (nodeI);
free (nodeE);
free (nodeD);
free (nodeC1);

free
free
free
free
free

(nodeO1);
(nodeO2);
(nodeO3);
(nodeO4);
(nodeO5);

free
free
free
free
free
free

(nodeE1);
(nodeE2);
(nodeE3);
(nodeE4);
(nodeE5);
(nodeE6);

printf ("test passed!\n");


}

/////////////////////////////////

void testQ2unique (void) {


// create test nodes,
node *nodeO = newNode
node *nodeN = newNode
node *nodeE = newNode
node *nodeT = newNode
node *nodeC = newNode
node *nodeA = newNode
node *nodeM = newNode
node *nodeW = newNode
node *nodeR = newNode
node *nodeK = newNode
node *nodeI = newNode
printf ("testing
list testList =
assert (testList
testList->head =

names indicate their value, next=NULL


('o');
('n');
('e');
('t');
('c');
('a');
('m');
('w');
('r');
('k');
('i');

redact on empty list \n");


malloc (sizeof (list*));
!= NULL);
NULL;

redact (testList, 'e'); // == NULL


assert (testList->head == NULL);
printf ("test passed!\n");
printf ("testing a one letter list - redact nothing\n");
testList->head = nodeK;
nodeK->next = NULL;
redact
assert
assert
printf

(testList,'e'); // no effect
(testList->head == nodeK);
(nodeK->next == NULL);
("test passed!\n");

printf
redact
assert
printf

("testing a one letter list - redact one\n");


(testList,'k'); // no effect
(testList->head == NULL);
("test passed!\n");

printf ("testing a two letter list - redact nothing\n");


testList->head = nodeK;
nodeK->next = nodeI;
nodeI->next = NULL;

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

("testing a two letter list - redact first\n");


(testList,'k'); // no effect
(testList->head == nodeI);
(nodeI->next == NULL);
("test passed!\n");

printf ("testing a two letter list - redact last\n");


testList->head = nodeO;
nodeO->next = nodeK;
nodeK->next = NULL;

redact
assert
assert
printf

(testList,'k'); // no effect
(testList->head == nodeO);
(nodeO->next == NULL);
("test passed!\n");

printf ("testing a three letter list - redact nothing\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeE;
nodeE->next = NULL;
redact
assert
assert
assert
assert
printf

Linked Lists Page 35

(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

("testing a three letter list - redact first\n");


(testList,'o'); // no effect
(testList->head == nodeN);
(nodeN->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");

printf ("testing a three letter list - redact middle\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeE;
nodeE->next = NULL;
redact
assert
assert
assert
printf

(testList,'n'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");

printf ("testing a three letter list - redact last\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeE;
nodeE->next = NULL;
redact
assert
assert
assert
printf

(testList,'e'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == NULL);
("test passed!\n");

printf ("testing a four letter list - redact nothing\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeC;
nodeC->next = nodeE;
nodeE->next = NULL;
redact
assert
assert
assert
assert
assert
printf

(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

("testing a four letter list - redact first\n");


(testList,'o'); // no effect
(testList->head == nodeN);
(nodeN->next == nodeC);
(nodeC->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");

printf ("testing a four letter list - redact second\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeC;
nodeC->next = nodeE;
nodeE->next = NULL;
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");

printf ("testing a four letter list - redact third\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeC;
nodeC->next = nodeE;
nodeE->next = NULL;
redact
assert
assert
assert
assert
printf

(testList,'c'); // no effect
(testList->head == nodeO);
(nodeO->next == nodeN);
(nodeN->next == nodeE);
(nodeE->next == NULL);
("test passed!\n");

printf ("testing a four letter list - redact last\n");


testList->head = nodeO;
nodeO->next = nodeN;
nodeN->next = nodeC;
nodeC->next = nodeE;
nodeE->next = NULL;

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");

printf ("testing a long list - redact none\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing a long list - redact first\n");


testList->head = nodeT;
nodeT->next = nodeE;
nodeE->next = nodeA;

Linked Lists Page 36

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");

printf ("testing a long list - redact m\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing a long list - redact w\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing a long list - redact o\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing a long list - redact r\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing a long list - redact last\n");


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;
redact
assert
assert
assert
assert
assert
assert
assert
assert
printf

Linked Lists Page 37

(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");

printf ("testing node values have not changed\n");


assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
assert
printf

(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");

printf ("testing that no nodes have been freed\n...");


// free the nodes - not needed in subsequent tests
// might fail if students have inadventantly freed nodes
// already
free (nodeO);
free (nodeN);
free (nodeE);
free (nodeT);
free (nodeC);
free (nodeA);
free (nodeM);
free (nodeW);
free (nodeR);
free (nodeK);
free (nodeI);

printf ("test passed!\n");


}

static node *newNode (int item) {


node *new = malloc (sizeof (node));
assert (new != NULL);
new->value = item;
new->next = NULL;
return new;

Linked Lists Page 38

*2013s1
Tuesday, 23 June 2015

11:55 PM

Linked Lists Page 39

You might also like