Skip to content

Commit

Permalink
add the material of week2 and 3
Browse files Browse the repository at this point in the history
  • Loading branch information
z5141448 committed Jul 25, 2017
1 parent 0fece20 commit f19323c
Show file tree
Hide file tree
Showing 11 changed files with 1,631 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# COMP2521
# COMP2521
Some material and my own exercise anser about COMP2521, which is an algrithm class. DON'T COPY IT DIRECTLY.
629 changes: 629 additions & 0 deletions week2/COMP2521 17s2 - Week 02 Lab Exercise.html

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions week2/COMP2521 17s2 - Week 02 Lab Exercise_files/course.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
body {font-size:11pt;font-family:arial,sans-serif;margin:10px 20px;}
a {color:#7788DD;text-decoration:none;}
a.active {color:#7788DD;text-decoration:none;}
a.inactive {color:#BBBBDD;text-decoration:none;}
a[href]:hover {color:#CC0000;background:#FFFFCC;}
small {font-size:75%}
pre {background:#EEEEFF;border:thin solid #9999CC;margin:5px 20px 5px 20px;padding:4px 8px;}
tt {font-size:12pt;}
span.toggle {font-size:67%;}
span.notice {font-size:110%;}
span.tiny {font-size:75%;font-weight:normal;}
span.heading {font-size:18pt;color:#7788DD;}
span.subheading {font-size:16pt;color:#000000;font-weight:normal;}
span.marks {font-weight:normal;font-size:75%;}
span.identifier {color:#993300;font-size:110%;font-weight:bold;font-family:courier;}
span.contentLink {font-weight:bold;font-size:110%}
span.contentNote {font-weight:normal;font-size:85%;}
tr.heading {font-size:large;font-weight:bold;background-color:#DDAA77;}
table.note {border:thin solid black;padding:2pt;width:75%}
div.note {border:thin solid black;font-size:90%;margin:5px 60px 5px 60px;padding:4px 8px;}
dt.item {font-weight:bold;}
h2 {font-size:16pt;font-weight:300;color:#7788DD;}
h3 {font-size:14pt;font-weight:300;color:#7788DD;}
.h2tt {font-size:17pt;font-weight:300;color:#7788DD;font-family:courier;}
.h3tt {font-size:15pt;font-weight:300;color:#7788DD;font-family:courier;}
.red {color:#BB0000;}
.green {color:#006600;}
.brown {color:#993300;}
.grey {color:#AAAAAA;}
.comment {font-size:90%;color:#666666}
.important {font-weight:bold;color:#CC0000;}
.bigQ {font-size:13pt;font-style:italic;}
206 changes: 206 additions & 0 deletions week2/IntList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// IntList.c - Lists of integers
// Written by John Shepherd, July 2008

#include <stdlib.h>
#include <stdio.h>
#include "assert.h"
#include "IntList.h"

// data structures representing IntList

struct IntListNode {
int data; // value of this list item
struct IntListNode *next;
// pointer to node containing next element
};

struct IntListRep {
int size; // number of elements in list
struct IntListNode *first;
// node containing first value
struct IntListNode *last;
// node containing last value
};

// create a new empty IntList
IntList newIntList()
{
struct IntListRep *L;

L = malloc(sizeof (struct IntListRep));
assert (L != NULL);
L->size = 0;
L->first = NULL;
L->last = NULL;
return L;
}

// free up all space associated with list
void freeIntList(IntList L)
{
// does nothing ...
}

// display list as one integer per line on stdout
void showIntList(IntList L)
{
IntListPrint(stdout, L);
}

// create an IntList by reading values from a file
// assume that the file is open for reading
IntList getIntList(FILE *inf)
{
IntList L;
int v;

L = newIntList();
while (fscanf(inf,"%d",&v) != EOF)
IntListInsert(L,v);
return L;
}

// create a new IntListNode with value v
// (this function is local to this ADT)
static struct IntListNode *newIntListNode(int v)
{
struct IntListNode *n;

n = malloc(sizeof (struct IntListNode));
assert(n != NULL);
n->data = v;
n->next = NULL;
return n;
}

// apppend one integer to the end of a list
void IntListInsert(IntList L, int v)
{
struct IntListNode *n;

assert(L != NULL);
n = newIntListNode(v);
if (L->first == NULL)
L->first = L->last = n;
else {
L->last->next = n;
L->last = n;
}
L->size++;
}

// insert an integer into correct place in a sorted list
void IntListInsertInOrder(IntList L, int v)
{
// This is INCORRECT
IntListInsert(L, v);
}

// delete first occurrence of v from a list
// if v does not occur in List, no effect
void IntListDelete(IntList L, int v)
{
struct IntListNode *curr, *prev;

assert(L != NULL);

// find where v occurs in list
prev = NULL; curr = L->first;
while (curr != NULL && curr->data != v) {
prev = curr;
curr = curr->next;
}
// not found; give up
if (curr == NULL) return;
// unlink curr
if (prev == NULL)
L->first = curr->next;
else
prev->next = curr->next;
if (L->last == curr)
L->last = prev;
L->size--;
// remove curr
free(curr);
}

// return number of elements in a list
int IntListLength(IntList L)
{
assert(L != NULL);
return L->size;
}

// make a physical copy of a list
// new list looks identical to original list
IntList IntListCopy(IntList L)
{
struct IntListRep *Lnew;
struct IntListNode *curr;

Lnew = newIntList();
for (curr = L->first; curr != NULL; curr = curr->next)
IntListInsert(Lnew, curr->data);
return Lnew;
}

// make a sorted physical copy of a list
IntList IntListSortedCopy(IntList L)
{
struct IntListRep *Lnew;
struct IntListNode *curr;

Lnew = newIntList();
for (curr = L->first; curr != NULL; curr = curr->next)
IntListInsertInOrder(Lnew, curr->data);
return Lnew;
}

// check whether a list is sorted in ascending order
// returns 0 if list is not sorted, returns non-zero if it is
int IntListIsSorted(IntList L)
{
struct IntListNode *curr;

assert(L != NULL);
// trivial cases, 0 or 1 items
if (L->size < 2)
return 1;
// scan list, looking for out-of-order pair
for (curr = L->first; curr->next != NULL; curr = curr->next) {
if (curr->next->data < curr->data)
return 0;
}
// nothing out-of-order, must be sorted
return 1;
}

// check sanity of an IntList (for debugging)
int IntListOK(IntList L)
{
struct IntListNode *p;
int count;

if (L == NULL)
return 1;
if (L->size == 0)
return (L->first == NULL && L->last == NULL);

// scan to (but not past) last node
count = 1; // at least one node
for (p = L->first; p->next != NULL; p = p->next)
count++;

return (count == L->size && p == L->last);
}

// display list as one integer per line to a file
// assume that the file is open for writing
void IntListPrint(FILE *outf, IntList L)
{
struct IntListNode *curr;

assert(L != NULL);
for (curr = L->first; curr != NULL; curr = curr->next)
printf("%d\n", curr->data);
}
58 changes: 58 additions & 0 deletions week2/IntList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// IntList.h - Lists of integers (interface)
// Written by John Shepherd, July 2008

#ifndef INTLIST_H
#define INTLIST_H

#include <stdio.h>

// External view of IntList
// Implementation given in intList.c

typedef struct IntListRep *IntList;

// create a new empty IntList
IntList newIntList();

// free up all space associated with list
void freeIntList(IntList);

// create an IntList by reading values from a file
// assume that the file is open for reading
IntList getIntList(FILE *);

// display list as one integer per line on stdout
void showIntList(IntList);

// apppend one integer to the end of a list
void IntListInsert(IntList, int);

// insert an integer into correct place in a sorted list
void IntListInsertInOrder(IntList, int);

// delete first occurrence of v from a list
// if v does not occur in List, no effect
void IntListDelete(IntList, int);

// return number of elements in a list
int IntListLength(IntList);

// make a physical copy of a list
// new list looks identical to original list
IntList IntListCopy(IntList);

// make a sorted physical copy of a list
IntList IntListSortedCopy(IntList);

// check whether a list is sorted in ascending order
// returns 0 if list is not sorted, returns non-zero if it is
int IntListIsSorted(IntList);

// check sanity of an IntList
int IntListOK(IntList);

// display list as one integer per line to a file
// assume that the file is open for writing
void IntListPrint(FILE *, IntList);

#endif
21 changes: 21 additions & 0 deletions week2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# COMP1927 13s1 Week 02 Lab

CC=gcc
CFLAGS=-Wall -Werror

all: usel randl

usel: useIntList.o IntList.o
$(CC) -o usel IntList.o useIntList.o

useIntList.o: useIntList.c IntList.h
$(CC) -c $(CFLAGS) useIntList.c

IntList.o: IntList.c IntList.h
$(CC) -c $(CFLAGS) IntList.c

randl: randList.c
$(CC) $(CFLAGS) -o randl randList.c

clean:
rm -f usel IntList.o useIntList.o randl
35 changes: 35 additions & 0 deletions week2/randList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// randList.c - generate a list of random integers
// Written by John Shepherd, July 2008
//

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

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

if (argc < 2) {
fprintf(stderr, "Usage: %s #values [seed]\n", argv[0]);
exit(EXIT_FAILURE);
}
max = atoi(argv[1]);
if (max < 1) {
fprintf(stderr, "%s: too few values\n", argv[0]);
exit(EXIT_FAILURE);
}
if (max > 1000000) {
fprintf(stderr, "%s: too many values\n", argv[0]);
exit(EXIT_FAILURE);
}
if (argc == 3)
srand(atoi(argv[2]));
else
srand(time(NULL)); // really random

for (i = 0; i < max; i++)
printf("%d\n",1+rand()%(max*10));

return 0;
}
14 changes: 14 additions & 0 deletions week2/timing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Input Initial Has Number AvgTime AvgTime
Size Order Dups of runs forusel forsort
5000 random no N T1sec T2sec
5000 sorted no N T1sec T2sec
5000 reverse no N T1sec T2sec
5000 random yes N T1sec T2sec
5000 sorted yes N T1sec T2sec
5000 reverse yes N T1sec T2sec
10000 random no N T1sec T2sec
10000 sorted no N T1sec T2sec
10000 reverse no N T1sec T2sec
10000 random yes N T1sec T2sec
10000 sorted yes N T1sec T2sec
10000 reverse yes N T1sec T2sec
Loading

0 comments on commit f19323c

Please sign in to comment.