Exemple #1
0
void deleteCurrent(ListRef L) {
    if (L == NULL) {
        printf( "Error: Called deleteCurrent() on NULL ListRef\n");
        //exit(EXIT_FAILURE);
    } else if (isEmpty(L)) {
        printf( "Error: Called deleteCurrent() on empty ListRef\n");
        //exit(EXIT_FAILURE);
    } else if (offEnd(L)) {
        printf( "Error: Called deleteCurrent() on ListRef without current \n");
        //exit(EXIT_FAILURE);
    }
    
    if (L->current == L->first) {
        deleteFirst(L);
    } else if(L->current == L->last) {
        deleteLast(L);
    } else {
        if (L->current->next != NULL) {
            L->current->next->prev = L->current->prev;
        }
        if (L->current->prev != NULL) {
            L->current->prev->next = L->current->next;
        }
        L->current->next = NULL;
        L->current->prev =NULL;
        freeNode(&(L->current));
        L->length--;
    }
    L->current = NULL;
}
Exemple #2
0
void BFS(GraphRef G, int s){
   int i, u, tmp;
   G->source = s;
   for ( i = 1;i <= getOrder(G); i++){
      G->color[i] = 'w';
      G->discover[i] = INF;
      G->parent[i] = NIL;
   }
   G->color[s] = 'g';
   G->discover[s] = 0;
   G->parent[s] = NIL;
   ListRef Q = newList();
   insertFront( Q, s );
   while( !isEmpty(Q) ){
      u = getFront(Q);
      deleteFront(Q);
      moveTo(G->adjacency[u], 0);
      while ( !offEnd(G->adjacency[u]) ){
         tmp = getCurrent(G->adjacency[u]);
         if ( G->color[tmp] == 'w'){
            G->color[tmp] = 'g';
            G->discover[tmp] = G->discover[u] + 1;
            G->parent[tmp] = u;
            insertBack( Q, tmp );
         }
         moveNext(G->adjacency[u]);
      }
      G->color[u] = 'b';
   }
   freeList(&Q);
}
Exemple #3
0
/*Inserts new node before the current pointer 
 * points the new nodes pointer to the previous 
 * next node
 */
void insertBeforeCurrent(ListHndl L, void* data)
{
  NodePtr node = NULL;
  if(L == NULL)
     {
        printf("Error: List has not been created.\n");
        exit(1);
     }
  if(offEnd(L))
     {
        printf("Error: The current is off the end of the list.\n");
        exit(1);
     }
  node = newNode(data);
  if(L->current->prev != NULL)
     {
        
        node->prev = L->current->prev;
        L->current->prev->next = node;
        node->next = L->current;
        L->current->prev = node;
     }
  else
     {
        node->prev = NULL;
        node->next = L->current;
        L->current->prev = node;
        L->first = node;
         
     }
  L->size++;
}
Exemple #4
0
// Inserts new element after current one
void insertAfter(List L, int data) {
	if ( L == NULL ) {
		printf("List Error: insertAfter() called on NULL List\n");
		exit(1);
	}
	if ( isEmpty(L) ) {
		printf("List Error: insertAfter() called on an empty List\n");
		exit(1);
	}
	if ( offEnd(L) ) {
		printf("List Error: insertAfter() called on a NULL pointer\n");
		exit(1);
	}

	Node N = newNode(data);
	if ( L->curr->next == NULL ) {
		L->back->next = N;
		N->prev = L->back;
		L->back = N;
	}else {
		N->prev = L->curr;
		N->next = L->curr->next;
		L->curr->next->prev = N;
		L->curr->next = N;
	}
	++(L->length);
}
Exemple #5
0
void deleteCurrent(ListRef L){
	NodeRef N = NULL;
	if(L==NULL){
		printf("List error: calling deleteCurrent on NULL ListRef\n");
		exit(1);
	}
	if(isEmpty(L)){
		printf("List error: calling deleteCurrent on empty List\n");
		exit(1);
	}
	if(offEnd(L)){
		printf("List error: calling deleteCurrent when current = NULL\n");
		exit(1);
	}
	N = L->current;
	if(getLength(L)>1){
		L->current->prev->next = L->current->next;
		L->current->next->prev = L->current->prev;
		L->current = NULL;
	} else {
		L->front = L->back = L->current = NULL;
	}
	L->length--;
	freeNode(&N);
}
Exemple #6
0
// Inserts new element before current one
void insertBefore(List L, int data) {
	if ( L == NULL ) {
		printf("List Error: insertBefore() called on NULL List\n");
		exit(1);
	}
	if ( isEmpty(L) ) {
		printf("List Error: insertBefore() called on an empty List\n");
		exit(1);
	}
	if ( offEnd(L) ) {
		printf("List Error: insertBefore() called on a NULL pointer\n");
		exit(1);
	}

	Node N = newNode(data);
	if ( L->curr->prev == NULL ) {
		N->next = L->front;
		L->front->prev = N;
		L->front = N;
	}else {
		N->prev = L->curr->prev;
		N->next = L->curr;
		L->curr->prev->next = N;
		L->curr->prev = N;
	}
	++(L->length);
}
Exemple #7
0
void doBFS(GraphRef g, int source){
	ListRef list = newList();
	initGraph(g);
	insertAfterLast(list, source);
	g->color[source]= 1;
	g->distance[source]=0;
	while(!isEmpty(list)){
		int current = getFirst(list);
		if(current > g->numVertices){
			deleteFirst(list);
			 break;
		}
		ListRef edgeList = g->vertices[current];
		moveFirst(edgeList);
		while(!offEnd(edgeList)){
			int edge = getCurrent(edgeList);
			if(g->color[edge]==0){
				g->distance[edge] = g->distance[current]+1;
				g->color[edge] = 1;
				g->parent[edge] = current;
				insertAfterLast(list, edge);
			}
			moveNext(edgeList);
		}
		deleteFirst(list);
		g->color[current] = 2;
	}
	makeEmpty(list);
	freeList(list);
}
Exemple #8
0
// sets the current node to the next one
void moveNext(ListRef L) {
    if(!offEnd(L)) {
        NodeType *node = malloc(sizeof(struct Node));
        node = L->current;
        node = node->next;
        L->current =node;
    }
}
Exemple #9
0
/* above
 */
void transposeHelp(GraphRef T, ListRef L, int i){
	int v;
	moveTo(L,0);
	while(!offEnd(L)){
		v = getCurrent(L);
		insertBack(T->adj[v],i);
		moveNext(L);
	}
}
Exemple #10
0
void doBFS(GraphHndl g, int source)
{
	ListHndl tempList = NewList();

	/*Make the lists white, reset distances and parents*/
	for(int i = 0; i < g->vertNums; i++)
	{
		g->parentPtr[i] = -1;
		g->distancePtr[i] = INT_MAX;
		g->colorPtr[i] = 0;
	}

	/*Temporary list holds paths from source*/
	insertAtBack(tempList, source);
	g->colorPtr[source] = 1;
	g->distancePtr[source] = 0;

	/*For every node in the source's adjacency list..*/
	while(!isEmpty(tempList))
	{
		int currNode = getFirst(tempList);

		/*Handle case where we're passed a bigger node*/
		if(currNode > g->vertNums)
		{
			deleteFirst(tempList);
			break;
		}

		/*Second temporary list holds edge path*/
		ListHndl tempList2 = g->vertices[currNode];
		moveFirst(tempList2);

		/* For each of the next node's adjacent nodes .. */
		while(!offEnd(tempList2))
		{
			int tempEdge = getCurrent(tempList2);
			/* If the node was white, increase the distance,
 			 * color it grey and set it's parent to be currnode
 			 * and enqueue this node into the path list */
			if(g->colorPtr[tempEdge] == 0)
			{
				g->distancePtr[tempEdge] = g->distancePtr[currNode]+1;
				g->colorPtr[tempEdge] = 1;
				g->parentPtr[tempEdge] = currNode;
				insertAtBack(tempList, tempEdge);
			}
			moveNext(tempList2);
		}
		/*Dequeue the first element and set black to show we're done.*/
		deleteFirst(tempList);
		g->colorPtr[currNode] = 2;
	}
	makeEmpty(tempList);
	//freeList(tempList);
}
Exemple #11
0
/*This method prints out the IDs of the ListHndl in the Hash.*/
void printIDs(ListHndl L)
{
   moveFirst(L);
   while(!offEnd(L))
   {
      long* bookid = L->current->data;
      printf("%lu ", *bookid);
      moveNext(L);
   }printf("\n");
}
Exemple #12
0
void doBFS(Graph G, int source)
{
    assert(G->numVerts != 0);
    ListHndl L = newList();
    /*printf("start %d\n", source);*/
    insertAtBack(L, source);
    int i = 0;
    for(i = 0; i < G->numVerts; i++)
    {
        G->color[i]= WHITE;
        G->parent[i] = NULL;
        G->distance[i] = INFINITY;
    }
    G->color[source] = GREY;
    G->parent[source] = NULL;
    G->distance[source] = 0;
    while(!isEmpty(L))
    {
        int u = getFirst(L);
        /*printf("u: %d\n", u);*/
        if(u > G->numVerts)
        {
            deleteFirst(L);
            break;
        }
        ListHndl v = G->verts[u];
        int prev;
        moveFirst(v);
        while(!offEnd(v))
        {
            int E = getCurrent(v);
            if(prev == E)
            {
                break;
            }
            /*printf("   v: %d\n", E);*/
            if(G->color[E] == WHITE)
            {
                G->color[E] = GREY;
                G->parent[E] = u;
                if(G->distance[E] < 0)
                {
                    G->distance[E] = 0;
                }
                G->distance[E] = G->distance[u] + 1;
                insertAtBack(L, E);
            }
            prev = E;
            moveNext(v);
        }
        deleteFirst(L);
        G->color[u] = BLACK;
    }
}
Exemple #13
0
/* for debugging purposes*/
void printTable(HashTable H){
    assert (H != NULL);
	printf("HashTable: Size %d\n", H->tableSize);
    for(int i = 0; i < H->tableSize; i++){
        printf("%d:\n",i);
        if(isEmpty(H->array[i])) continue;
        moveFirst(H->array[i]);
        while(!offEnd(H->array[i])){
            HashNode N = getCurrent(H->array[i]);
            printf("-%s\n",N->name);
            moveFirst(N->L);
            while(!offEnd(N->L)){
                ListNode temp = getCurrent(N->L);
                printf("--%d\n",temp->num);
                moveNext(N->L);
            }
            moveNext(H->array[i]);
        }
    }
}
Exemple #14
0
/*** Other operations ***/
void printList(ListRef L){  //prints a list on one line
   NodeRef oldcurr = L->current;
   L->current = L->first;
   while(!offEnd(L)){
      printf("%ld ",L->current->data);
      L->current = L->current->next;
   }
   printf("\n");
   L->current = oldcurr;
   oldcurr = NULL;
}
Exemple #15
0
/*Check if customer already purchased certain book*/
int customerCompare(long id, ListRef *customers, int custNum){
	ListRef l = customers[custNum];
	moveFirst(l);
	while(!offEnd(l)){
		if(getCurrent(l) == id)
			return true;
		if(atLast(l)) break;
		moveNext(l);	
	}
	return false;
}
Exemple #16
0
ListHndl lookup(HashTable H, char* key){
    assert (H != NULL);
    ListHndl slot = H->array[hash((unsigned char*)key) % H->tableSize];
    if(isEmpty(slot)) return NULL;
    else moveFirst(slot);
    while(!offEnd(slot)){
        HashNode temp = getCurrent(slot);
        if(strcmp(key,temp->name) == 0) return temp->L;
        else moveNext(slot);
    }
    return NULL;
}
Exemple #17
0
/*Inserts copurchases into hash*/
void addToHash(HashRef h, long bookId, ListRef * customers, int custNum, BookRef list){
	moveFirst(customers[custNum]);			//add cust previous books to hash of current book
	while(!offEnd(customers[custNum])){
			long current = getCurrent(customers[custNum]);
			if(getId(list)!= current){ //don't add itself to book list
				insertionSort(getBookList(list), current);
			}
			if(atLast(customers[custNum])) break;
				moveNext(customers[custNum]);
		
	}
}
Exemple #18
0
/* Return data of the current node */
long getCurrent(ListRef L) {
    if (L==NULL) {
        printf( "Error: Called getCurrent() on NULL ListRef\n");
        //exit(EXIT_FAILURE);
    } else if (isEmpty(L)) {
        printf( "Error: Called getCurrent() on empty ListRef\n");
        //exit(EXIT_FAILURE);
    } else if (offEnd(L)) {
        printf( "Error: Called getCurrent() on ListRef without current element\n");
        //exit(EXIT_FAILURE);
    }
    return L->current->data;
}
Exemple #19
0
void deleteHashNode(HashTable H, char* name){
    assert (H != NULL);
    ListHndl slot = H->array[hash((unsigned char*)name) % H->tableSize];
    if(isEmpty(slot)) return;
    moveFirst(slot);
    while(!offEnd(slot)){
        HashNode temp = getCurrent(slot);
        if(strcmp(name, temp->name) == 0){
            ListHndl L = temp->L;
            freeList(&L);
            deleteCurrent(slot);
        }else moveNext(slot);
    }
}
Exemple #20
0
/*
 * Returns the book id
 */
ListHndl returnid(ListHndl L)
{
   if(offEnd(L))
   {
      printf("Error: At the end of the list, no ID.\n");
      exit(1);
   }
   if(isEmpty(L))
   {
      printf("Error: List is empty.\n");
      exit(1);
   }
   return L->current->bookid;
}
Exemple #21
0
/* Move the current ptr to the next node */
void moveNext(ListRef L) {
    if (L == NULL) {
        printf( "Error: Called moveNext() on NULL ListRef\n");
        //exit(EXIT_FAILURE);
    } else if (isEmpty(L)) {
        printf( "Error: Called moveNext() on empty ListRef\n");
        //exit(EXIT_FAILURE);
    } else if (offEnd(L)) {
        printf( "Error: Called moveNext() on ListRef without current\n");
        //exit(EXIT_FAILURE);
    } else {
        L->current = L->current->next;
    }
}
Exemple #22
0
/*The method deletes the current node of the list
 * sets the pointer to the prev and next node
 * links them together. If current is the first on the list
 * just set the pointers of the next to the next node.
 * If current is the last on the list, just set the 
 * pointers to the prev node
 */
void deleteCurrent(ListHndl L)
{
   NodePtr tmp = NULL;
   if(L == NULL)
     {
        printf("Error: List has not been made yet.\n");
        exit(1);
     }
  if(isEmpty(L))
     {
        printf("Error: No size in the list.\n");
        exit(1);
     }  
  if(offEnd(L))
     {
        printf("Error: Can't delete at the end.\n");
        exit(1);
     }
  tmp = L->current;
  if(L->size > 1)
     {
        if(L->current->prev != NULL && L->current->next != NULL)
           {
              L->current->next->prev = L->current->prev;
              L->current->prev->next = L->current->next;
              L->current->next = NULL;
              L->current->prev = NULL;
              L->current = NULL;
           }
        else if(L->current->prev == NULL)
           {
              L->first = L->current->next;
              L->current->next = NULL;
           }
        else if(L->current->next == NULL)
           {
              L->last = L->current->prev;
              L->current->prev = NULL;
           }
        L->current = NULL;
     }
  else
     {
        L->first = NULL;
        L->last = NULL;
        L->current = NULL;   
     }
  L->size--;
  freenode(&tmp);
}
Exemple #23
0
/*
 *  atBack()
 *  Returns True if curr is pointing at the back of L, otherwise returns false
 *	Pre: !isEmpty(L); !offEnd(L).
 */
int atBack(ListRef L) {
	if ( L == NULL ){
		printf("List Error: calling atBack() on NULL ListRef\n");
		exit(1);
	}
	if ( isEmpty(L) ) {
		printf("List Error: calling atBack() on an empty List\n");
		exit(1);
	}
	if ( offEnd(L) ) {
		printf("List Error: calling atBack() on NULL pointer\n");
		exit(1);
	}
	return (L->curr->next == NULL);
}
Exemple #24
0
// inserts a node in a list after the current node
// the new node must have a pointer prev to current
// and point the previous to the next of the current of the node
void insertAfterCurrent(ListRef L, long data) {
    NodeType *node = malloc(sizeof(struct Node));
    node ->data = data;
    if(!offEnd(L)) {
        if(L->current!=L->last) {
            node->prev = L->current;
            node->next= L->current->next;
            L->current->next->prev =node;
            L->current->next = node;
            L->current = node;
        }
        else insertAfterLast(L, data);

    }
}
Exemple #25
0
// return the current element pre: !offEnd()
int getElement(List L) {
	if ( L == NULL ) {
		printf("List Error: getElement() called on NULL List\n");
		exit(1);
	}
	if ( isEmpty(L) ) {
		printf("List Error: getElement() called on empty list\n");
		exit(1);
	}
	if ( offEnd(L) ) {
		printf("List Error: getElement() called list with undefined cursor\n");
		exit(1);
	}
	return (L->curr->data);
}
Exemple #26
0
// set current marker one step forward
void moveNext(List L) {
	if ( L == NULL ) {
		printf("List Error: moveNext() called on NULL List\n");
		exit(1);
	}
	if ( isEmpty(L) ) {
		printf("List Error: moveNext() called on an empty list\n");
		exit(1);
	}
	if ( offEnd(L) ) {
		printf("List Error: moveNext() called on a NULL pointer\n");
		exit(1);
	}
	L->curr = L->curr->next;
}
Exemple #27
0
/*
 *  getCurrent()
 *  Returns the value pointed at by curr in L.
 *  Pre: !isEmpty(L); !offEnd(L).
 */
int getCurrent(ListRef L) {
	if ( L == NULL ) {
		printf("List Error: calling getCurrent() on NULL ListRef\n");
		exit(1);
	}
	if ( isEmpty(L) ) {
		printf("List Error: calling getCurrent() on an empty List\n");
		exit(1);
	}
	if ( offEnd(L) ) {
		printf("List Error: calling getCurrent() on NULL pointer\n");
		exit(1);
	}
	return (L->curr->data);
}
Exemple #28
0
long getCurrent(ListRef L){
	if(L == NULL){
		printf("List error: calling getCurrent on NULL ListRef\n");
		exit(1);
	}
	if( isEmpty(L)){
		printf("List error: calling getCurrent on empty ListRef\n");
		exit(1);
	}
	if( offEnd(L)){
		printf("List error: calling getCurrent when current isnt defined\n");
		exit(1);
	}
	return(L->current->data);
}
Exemple #29
0
/*
 *  moveNext()
 *  Moves current marker one step toward last element.
 *	Pre: !isEmpty(L); !offEnd(L).
 */
void moveNext(ListRef L) {
	if ( L == NULL ) {
		printf("List Error: calling moveNext() on NULL ListRef\n");
		exit(1);
	}
	if ( isEmpty(L) ) {
		printf("List Error: calling moveNext() on an empty List\n");
		exit(1);
	}
	if ( offEnd(L) ) {
		printf("List Error: calling moveNext() on a NULL pointer\n");
		exit(1);
	}
	L->curr = L->curr->next;
}
Exemple #30
0
/*This method prints out the IDs of the ListHndl in the Hash.*/
void printList(ListHndl L)
{
   moveFirst(L);
   while(!offEnd(L))
   {
      if(!atLast(L))
      {
         printf("%d -> ", L->current->data);
      }else{
         printf("%d \n", L->current->data);
      }
      moveNext(L);
   }
   printf("\n");
}