Skip to content

Commit

Permalink
finished this weeks
Browse files Browse the repository at this point in the history
  • Loading branch information
tecty committed Aug 10, 2017
1 parent ab44a56 commit 61247c4
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 27 deletions.
Empty file removed week3/%
Empty file.
54 changes: 28 additions & 26 deletions week3/DLList.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,7 @@
#include <assert.h>
#include "DLList.h"

// data structures representing DLList

typedef struct DLListNode {
char *value; // value of this list item (string)
struct DLListNode *prev;
// pointer previous node in list
struct DLListNode *next;
// pointer to next node in list
} DLListNode;

typedef struct DLListRep {
int nitems; // count of items in list
DLListNode *first; // first node in list
DLListNode *curr; // current node in list
DLListNode *last; // last node in list
} DLListRep;

// create a new DLListNode (private function)
static DLListNode *newDLListNode(char *it)
Expand Down Expand Up @@ -216,17 +201,29 @@ void DLListBefore(DLList L, char *it)
struct DLListNode *new = newDLListNode(it);

/* insert this node */
if (L->curr == L->first) {
/* the current node is the first node */
L->first = new;
if (L->nitems == 0) {
/* the list is empty */
L->curr = L->last = L->first = new;
}
else{
if (L->curr == L->first) {
/* the current node is the first node */
L->first = new;
}
else{
printf("imhere\n" );
// set up the link before the curr
L->curr->prev->next = new;
new ->prev = L->curr->prev;
}
// set the link
L->curr->prev = new;
new->next= L->curr;
// set the curr to the added item
L->curr = new;
// increament the counter
L->nitems ++;
}
// set the link
L->curr->prev = new;
new->next= L->curr;
// set the curr to the added item
L->curr = new;
// increament the counter
L->nitems ++;

}

Expand All @@ -240,9 +237,14 @@ void DLListAfter(DLList L, char *it)

/* insert this node */
if (L->curr == L->last) {
/* the current node is the first node */
/* the current node is the last node */
L->last = new;
}
else{
// set up the link for the next items
new->next = L->curr->next;
L->curr->next->prev= new;
}
// set the link
L->curr->next = new;
new->prev= L->curr;
Expand Down
17 changes: 17 additions & 0 deletions week3/DLList.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@
// Implementation given in DLList.c
// Implements a DLList of strings (i.e. items are strings)

// data structures representing DLList

typedef struct DLListNode {
char *value; // value of this list item (string)
struct DLListNode *prev;
// pointer previous node in list
struct DLListNode *next;
// pointer to next node in list
} DLListNode;

typedef struct DLListRep {
int nitems; // count of items in list
DLListNode *first; // first node in list
DLListNode *curr; // current node in list
DLListNode *last; // last node in list
} DLListRep;

typedef struct DLListRep *DLList;

// create a new empty DLList
Expand Down
Empty file removed week3/i
Empty file.
Binary file modified week3/myed
Binary file not shown.
Empty file removed week3/n
Empty file.
Empty file removed week3/q
Empty file.
Binary file modified week3/testL
Binary file not shown.
108 changes: 107 additions & 1 deletion week3/testList.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,119 @@
#include <stdio.h>
#include <assert.h>
#include "DLList.h"
void hr() {
/* code */
printf("\n<-----> \t<-----> \n" );
}
void showState(DLList L) {
/* show the current state*/
if (validDLList(L)) {
/* code */
printf("List is still valid\n" );
if (DLListLength(L)) {
/* the list have items, so show the current item */
// show the number of items
printf("now list have:%d items\n",DLListLength(L) );
// print the rep info
printf("Current node has value %s\n",L->curr->value );
printf("first node is %s\n",L->first->value );
printf("last node is %s\n",L->last->value );
}
}

}
int main(int argc, char *argv[])
{
DLList myList;
myList = getDLList(stdin);


printf("inputed this was:\n" );
putDLList(stdout,myList);

assert(validDLList(myList));
// TODO: more tests needed here

hr();
printf("inserting 10 items into list before the current item;\n" );
showState(myList);

for (int n = 0; n < 10; n++) {
/* adding node in the list */
char this_string[2];
sprintf(this_string, "%d",n);
DLListBefore(myList, this_string);
}

showState(myList);
printf("the list now is:\n" );
putDLList(stdout,myList);

hr();
printf("move the curr pointer to 3rd item\n" );
// swtich it to the first item
DLListMoveTo(myList,3);
showState(myList);


hr();
printf("trying to move outside the boundry\n" );
showState(myList);
printf("\nmove To\n" );
DLListMoveTo(myList,333333);
showState(myList);
printf("\nmove afront\n" );
DLListMove(myList,1233333);
showState(myList);
printf("\nmove backward\n" );
DLListMove(myList,-12233333);
showState(myList);

printf("now the list is:\n" );
putDLList(stdout,myList);

hr();
DLListMoveTo(myList,3);
printf("insert 3 items after the 3rd node\n" );
for (int i = 80; i < 101; i+=7) {
/* inserting items */
//construct the insert string
char this_string[10];
sprintf(this_string,"%d",i);
DLListAfter(myList,this_string);
}
showState(myList);
printf("current list now is\n" );
putDLList(stdout, myList);

hr();
DLListMoveTo(myList,3);
printf("insert 3 items before third item\n" );
showState(myList);
for (int i = 56; i < 81; i+=7) {
/* inserting items */
//construct the insert string
char this_string[10];
sprintf(this_string,"%d",i);
DLListBefore(myList,this_string);
}
showState(myList);
printf("current list now is\n" );
putDLList(stdout, myList);

hr();
printf("Delete 4 items in the list\n" );
showState(myList);
for (int i = 0; i < 4; i++) {
/* Delete the node and show statuts */
// delete the node;
DLListDelete(myList);
showState(myList);
}
printf("current list now is\n" );
putDLList(stdout, myList);

hr();
printf("end of the test\n" );

return 0;
}
Empty file removed week3/w
Empty file.

0 comments on commit 61247c4

Please sign in to comment.