Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Martin Aigner <martin@maigner.net>
Christian Barthel <cbarthel@cs.uni-salzburg.at>
Christoph Kirsch <ck@cs.uni-salzburg.at>
Michael Lippautz <michael.lippautz@gmail.com>
Simone Oblasser <simone.oblasser@cs.uni-salzburg.at>
Simone Oblasser <simone.oblasser@cs.uni-salzburg.at>
Armin Lanhgofer <uni@langhofer.at>
79 changes: 79 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#########################################
## Assignment 0: Basic data structures ##
#########################################
https://github.com/cksystemsteaching/AOS-Winter-2015
Author: armin langhofer <uni@langhofer.at>

This is my implementation of a linked list in C*.

To Compile, please follow these steps:
======================================
1. install ubuntu 32bit lts server edition, apt-get install gcc.
2. see https://github.com/mistay/AOS-Winter-2015/tree/selfie-master for how to get selfie up and running
3. git clone repro from https://github.com/mistay/AOS-Winter-2015.git or, if pull request was accepted from
https://github.com/cksystemsteaching/AOS-Winter-2015.git
4. change into cloned directory and do the following:

$ gcc -o selife selfie.c -fno-builtin
$ touch out
$ ./selfie -c < selfie.c
$ mv out selfie.mips1
$ touch out
$ ./selfie -m 32 selfie.mips1 < linkedlist.c
mem 32MB
[OS] Terminated with 0


To Run:
=======
$ ./selfie -m 32 out
mem 32MB
d,c,b,a
d,b,a
done[OS] Terminated with 6
$

Interpretation and Explanation of what happened
===============================================
First, linkedlist.c creates some arbitrary elements:

...
head = create(0, 'a');
head = create(head, 'b');
toberemoved = create(head, 'c');
...

Then, the resulted list is printed to stdout:

d,c,b,a

Now, one element is removed:

...
remove(head, toberemoved);
...

The resulting list is printed again:
d,b,a


Some fixed chars are printed (this is an easter-egg styled 'hello world'):
done

And the application exits with exit code 6: just to clarify this application exits (and not the emulator).
Terminated with 6


Result
======
[OK] must be implemented in C*
[OK] must compile with selfie
[OK] must run on selfie
[OK alloc() ] the list must be dynamically allocated
[OK, alloc() ] every node must be dynamically allocated
[OK, create() ] inserting nodes to the list and removing nodes from the list
[OK, printll()] list iteration
[NG] Bonus: sort the list. Any way you like
[OK, Oct 14] Deadline: Oct 15, end of day


71 changes: 71 additions & 0 deletions linkedlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//erzeugt listenelement
int* create(int* next, int data) {
int* s;
s = (int*)malloc( 2 * 4 );

// next == 0 entspricht dem letzten element, ansonsten pointer aufs naechste
*(s+0) = next;

// payload, daten kommen hier rein, zB 'a'
*(s+1) = data;

return s;
}

// entfernt element von liste
// sucht element in liste rekursiv
void remove(int* head, int* element) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the advantage of recursion in this case? The recursion parameter (next) is, in fact, an iterator. I'm not saying recursion is wrong here... But what is the advantage of recursion instead of iteration?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's just a little easier to read and (thus) more "elegant" - but: of course you can iterate through the list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful with metrics like "easier to read". They are highly subjective and therefore hard to compare. The actual reason why recursion is more elegant here is the list itself. A linked list is a recursive data type. Nice solution!

int* next;
next = *(head+0);

if (next == element) {
// found, remove element. memleak todo: free next element's mem but not impl' yet in c*c
*(head + 0) = *(next + 0);
return;
}

if ((int)next == 0 ) {
// abbruchbedinung: das letzte element wurde gefunden, der next pointer zeigt auf (int)0
return;
}
remove(next,element);


}

// gibt liste auf konsole aus, zB a,b,c,
// ruft sich dabei selbst rekursiv fuer alle elemente auf und bricht bei letztem element ab
void printll( int* element) {
int* next;
putchar(*(element + 1));

next = *(element+0);
if ((int)next == 0 ) {
// abbruchbedinung: das letzte element wurde gefunden, der next pointer zeigt auf (int)0
putchar(10); // newline (at least under *nix)
return;
}
putchar(',');
printll(next);
}

int main() {
int* head;
int* toberemoved;
head = create(0, 'a');
head = create(head, 'b');
toberemoved = create(head, 'c');
head = toberemoved;
head = create(head, 'd');
printll(head);

remove(head, toberemoved);

printll(head);

putchar('d');
putchar('o');
putchar('n');
putchar('e');
exit(6);
}