コード例 #1
0
/*
	- The page number is searched for in the cache
	- If it is found in the cache then it is returned that the page is found in the cache and detach the node and bring it to the front end of the queue
	- If it is not found then it is added to the front end of the queue and also
	- Add the page number to the hash table
	- If it is not there in the cache and the cache is full delete the node at the end and then add the page number to the front end of the queue and update the hash


*/
node_t reference_page(node_t* hash, node_t head, int page_number)
{
	int search_result = search(page_number, head);

	if(search_result != -1)
	{
		//The page is found in the cache
		printf("There has been a cache hit \n");
		head = remove_and_bring_front(head, page_number);
	}

	else
	{
		printf("There is no cache hit \n");
		//The page is not found in the cache
		if(head -> ele == LRU_SIZE)
		{
			printf("The cache is full \n");
			head = delete_rear(head);
			head = add_front(head, page_number);
		}

		else
		{
			//Add the page to the front end of the queue and
			printf("Adding the page to the front end of the queue \n");
			head = add_front(head, page_number);
		}

	}

	return head;
}
コード例 #2
0
int main(int argc, char const *argv[])
{
	list_t *list = (list_t*)malloc(sizeof (list_t));
	init(list);

	//make a node
	add_at(list, (void*)2, 0);
	assert(list->head && list->tail);
	assert((int) get(list, 0) == 2);
	//do more adding
	int i = 3;
	for(; i < 15; ++i){
		add_front(list, (void*)i);
		assert(list->head && list->tail);
		assert((int) get(list, 0) == i);
	}
	assert((int) get(list, list->size - 1) == 2);
	assert((int) fold_left(list, add_list, (void*)0) == 104);
	assert((int) fold_left(list, add_list, (void*)0) == (int)fold_right(list, add_list, (void*)0));
	add_back(list, (void*)0);
	assert(list->tail->data == (void*)0);
	assert((int) get(list, list->size - 1) == 0);
	add_front(list, (void*)2);
	add_front(list, (void*)2);
	assert((int) fold_left(list, count_2, (void*)0) == 3);
	remove_data(list, (void*)2, is_2);
	return 0;
}
コード例 #3
0
/**
 * Find possible actions down on a column
 *
 * @param state a game state
 * @param col the column to check
 * @return a list of actions
 */
static struct List * actions_down( const struct State * state, int col )
{
    int idx = 0;
    struct List * actions = new_list();

    for(idx = 0; (idx < SIZE) && ((idx + 2) < SIZE); idx++){
      if(  (state->board[ idx ][col] == state->player )
	    && (state->board[ idx + 1 ][ col ] == opposite_player( state->player ))
	    && (state->board[ idx + 2 ][ col ] == 'O')){
	   /* check for moves  - increment to optimize move*/
	   int end_row = idx + 2;
	   /* add move to action list for single jump  */
	   add_front( &actions, new_move( idx, col, end_row, col) );
	   while( ((end_row + 2) < SIZE )
	        && (state->board[ end_row ][ col ] == 'O' )
	        && (state->board[ end_row + 1 ][ col ] == opposite_player( state->player ))
	        && (state->board[ end_row + 2 ][ col ] == 'O') ){
	     end_row += 2;
	     /* add optimized moves to action list */
	     add_front( &actions, new_move( idx, col, end_row, col) );
	   }
      }
    }
    
    return actions;
}
コード例 #4
0
/**
 * Find possible actions left on a row
 *
 * @param state a game state
 * @param row the row to check
 * @return a list of actions
 */
static struct List * actions_left( const struct State * state, int row )
{
    int idx = SIZE-1;
    struct List * actions = new_list();

     for(idx = SIZE - 1; (idx > 0) && ((idx - 2) >= 0); idx--){
       if( (state->board[ row ][idx] == state->player )
	    && (state->board[ row ][ idx - 1 ] == opposite_player( state->player ))
	    && (state->board[ row ][ idx - 2 ] == 'O')){
	   /* check for moves  - increment to optimize move*/
	   int end_col = idx - 2;
	   /* add move to action list for single jump  */
	   add_front( &actions, new_move( row, idx, row, end_col) );
	   while( ((end_col - 2) > 0 )
	        && (state->board[ row ][ end_col ] == 'O' )
	        && (state->board[ row ][ end_col - 1 ] == opposite_player( state->player ))
	        && (state->board[ row ][ end_col - 2 ] == 'O') ){
	     end_col -= 2;
	     /* add optimized moves to action list */ 
	     add_front( &actions, new_move( row, idx, row, end_col) );
	   }
       }
     }

    return actions;
}
コード例 #5
0
main()
{
	DequeType deque;

	init(&deque);
	add_front(&deque, 10);
	add_front(&deque, 20);
	add_rear(&deque, 30);
	display(&deque);

	delete_front(&deque);
	delete_rear(&deque);
	display(&deque);
}
コード例 #6
0
ファイル: 2_5.cpp プロジェクト: RudraNilBasu/C-CPP-Codes
int main()
{
        LinkedList num_1;
        num_1.add_element_end(7);
        num_1.add_element_end(1);
        num_1.add_element_end(6);

        LinkedList num_2;
        num_2.add_element_end(5);
        num_2.add_element_end(9);
        num_2.add_element_end(2);

        add_front(num_1, num_2);

        LinkedList num_3;
        num_3.add_element_end(1);
        num_3.add_element_end(2);
        num_3.add_element_end(2);

        LinkedList num_4;
        num_4.add_element_end(9);

        pad_extra_digits(num_3, num_4);
        return 0;
}
コード例 #7
0
ファイル: posting.c プロジェクト: jeng/invertedisam
void append_unique_posting_list(FILE *fp, Posting **listp, int num_postings, int offset, int use_doc_freq){
    int sz = sizeof(PostingData);
    PostingData pd;
    Posting *list;
    int n, i;

    fseek(fp, offset, SEEK_SET);

    for (i = 0; i < num_postings; i++){
        if ((n = fread(&pd,sz,1,fp)) != 1){
            int errcode = ferror(fp);
            fprintf(stderr,"%s Could not read record at offset %d. Error code %d\n", 
                    progname(), offset, errcode);
            exit(1);
        }
        else {
            Posting *p;
            /* Don't add duplicates. But if we find one then update
               it's frequency. */
            list = *listp;
            for (; list != NULL; list = list->next){
                if (list->data.docid == pd.docid){
                    list->data.frequency += use_doc_freq ? pd.frequency : 1;
                    break;
                }
            }
            if (list == NULL){
                p = emalloc(sizeof(*p));
                p->data.frequency = use_doc_freq ? pd.frequency : 1;
                p->data.docid = pd.docid;
                *listp = add_front(*listp,p);
            }
        }
    }
}
コード例 #8
0
ファイル: DOUBLELILIST11.C プロジェクト: 1we/DSLab
insert_before()
{
  int key;
  if(first==NULL){
  printf("\nlist is empty");
  return;
  }
printf("\nenter the key");
scanf("%d",&key);
if(first->info==key){
  add_front();
  return;
  }
cur=first;
while(cur!=NULL){
  if(cur->info==key){
    temp=(NODE*)malloc(sizeof(NODE));
    printf("\nenter item to be inserted");
    scanf("%d",&temp->info);
    prev=cur->llink;
    prev->rlink=temp;
    temp->llink=prev;
    temp->rlink=cur;
    cur->llink=temp;
    return;
    }
cur=cur->rlink;
}
printf("key not found");
}
コード例 #9
0
ファイル: hmap.c プロジェクト: ibrahiem96/School-Projects
void * hmap_set(HMAP_PTR map, char *key, void *val){
unsigned h;
int idx;
NODE *p, **pp;

  pp = get_node_pred(map, key);
  p = *pp;

  if(p == NULL) {  // key not present
     char *key_clone;

     key_clone = malloc( (strlen(key) + 1)*sizeof(char));
     strcpy(key_clone, key);

     map->n++;
     if(map->n > map->max_n) 
	resize(map);
     h = map->hfunc(key);
     idx = h % map->tsize;

     p = malloc(sizeof(NODE));

     p->key = key_clone;
     p->val = val;
     p->hval = h;

     add_front(&(map->tbl[idx]), p);
     return NULL;  // no old value to return
  }
  else {  // key already in map
     void *tmp = p->val;
     p->val = val;
     return tmp;  // return old value
  }
}
コード例 #10
0
ファイル: posting.c プロジェクト: jeng/invertedisam
/* get_posting_list: Read a number of posting from a certain offset
   from the beginning of a file. */
Posting *get_posting_list(FILE *fp, int num_postings, int offset){
    int sz = sizeof(PostingData);
    PostingData pd;
    Posting *list;
    int n,i;

    list = NULL;

    fseek(fp, offset, SEEK_SET);

    for (i = 0; i < num_postings; i++){
        if ((n = fread(&pd,sz,1,fp)) != 1){
            int errcode = ferror(fp);
            fprintf(stderr,"%s Could not read record at offset %d. Error code %d\n", 
                    progname(), offset, errcode);
            exit(1);
        }
        else {
            Posting *p;
            p = emalloc(sizeof(*p));
            p->data.frequency = pd.frequency;
            p->data.docid = pd.docid;
            list = add_front(list,p);
        }
    }
    return list;
}
コード例 #11
0
ファイル: hmap.c プロジェクト: ibrahiem96/School-Projects
static void resize(HMAP_PTR map) {
int ntsize;
TBL_ENTRY *ntbl;
NODE *nxt, *p;
unsigned h;
int i, idx;

  map->tsize_idx++;
  ntsize = map->tsize_tbl[map->tsize_idx];
  ntbl = create_tbl_array(ntsize);

  for(i=0; i<map->tsize; i++) {
    p = map->tbl[i].members;
    while(p != NULL) {
	nxt = p->next;
  	// h = map->hfunc(key);  // no need to recompute
  	idx = p->hval % ntsize;
	add_front(&ntbl[idx], p);
	p = nxt;
    }
  }
  free(map->tbl);
  map->tbl = ntbl;
  map->tsize = ntsize;
  map->max_n = (int)(ntsize * map->lfactor);

}
コード例 #12
0
ファイル: hashtable.c プロジェクト: KTRosenberg/Hash_Table_C
/*
    put_record
        given a key and its non-negative value,
        if the key does not yet exist in the hash table,
            adds a key-value record,
        if the key already exists in the hash table,
            increments the value associated with the key by the value
            passed as an argument
    param:  
        char* key (key to add)
        uint64_t value (value to assign or update)
        hash_table_t* hash_table (the hash table to search)
    return: 
        upon success, returns a pointer to the newly-created key-value record
        upon error returns NULL
*/
record_t* put_record(char* key, uint64_t value, hash_table_t* hash_table)
{
    if(!key || !hash_table)return NULL;

    //calculate the hash value using the hash function
    //set for the current hash table, (save value in case of resize)
    uint64_t hash_val = (*(hash_table->hash_function))(key);
    //find the appropriate index in the hash table's lists array
    uint64_t table_index = hash_val%(hash_table->table_size);
    
    //set a double record pointer to the start of the correct list in
    //the hash table's lists array to begin traversing the list
    record_t** link_ptr = &(hash_table->lists[table_index]);
    
    //traverse the list until the chosen key is encountered or
    //the end of the list is reached
    while(*link_ptr && strcmp(key, ((*link_ptr)->key)) != 0)
    {
        //repoint the double pointer record to the next record pointer
        link_ptr = &(*link_ptr)->next_link;
    }

    //if the record does not exist,
    //add a new key-value record to the current list,
    //return a pointer to the key-value new record
    if(!(*link_ptr))
    {
        //check whether load factor threshold will be exceeded after the next record is added
        //if so, resize table, rehash record keys and place them in the larger table,
        //then proceed to hash the new record, then place it in the appropriate list in the larger table
        if(NEEDTORESIZE)
        {
            if((resize_table(hash_table) != 0))return NULL;

            //now that the table should have been resized,
            //find the correct index for the new record based on the new size
            table_index = hash_val%(hash_table->table_size);
        }

        //add the new record to the hash table by
        //adding it to the front of the appropriate list in the lists array
        record_t* new_record = NULL;
        if(!(new_record = add_front(key, value, &(hash_table->lists[table_index]))))return NULL;
        {
            //update the total number of records in the hash table,
            //return a pointer to the new key-value record
            hash_table->num_records++;
            return new_record;
        }
    }
    //if the key already exists in the table,
    //update its old value (increment the old value by the new value),
    //return a pointer to the key-value record
    else
    {
        (*link_ptr)->value += value;
        return *link_ptr;
    }
}
コード例 #13
0
int main()
{
	
	node_t head = NULL;

	int choice;
	int ele;

	while(1)
	{
		printf("-------------------------------------------------------------------------------------------\n");
		printf("1. Add a node on the front end of the list \n");
		printf("2. Display \n");
		printf("3. Exit \n");
		printf("4. Add a node  on the rear end of the list \n");
		printf("5. Delete a node in the front end of the list \n");
		printf("6. Delete a node in the rear end of the list \n");
		printf("-------------------------------------------------------------------------------------------\n");

		printf("Enter the choice \n");
		scanf("%d", &choice);


		switch(choice)
		{
			case 1:
					printf("Enter the element to be inserted \n");
					scanf("%d", &ele);
					head = add_front(head, ele);
					break;

			case 2:
					display(head);
					break;

			case 3:
					exit(0);
					break;

			case 4:
					printf("Enter the element to be inserted \n");
					scanf("%d", &ele);
					head = add_end(head, ele);
					break;

			case 5:
					head = delete_front(head);
					break;

			case 6:
					head = delete_rear(head);
					break;

			default:
					printf("The choice you entered is wrong \n");
					break;
		}
	}
}
コード例 #14
0
/**
 * Find possible actions up on a column
 *
 * @param state a game state
 * @param col the column to check
 * @return a list of actions
 */
static struct List * actions_up( const struct State * state, int col )
{
    int idx = SIZE - 1;
    struct List * actions = new_list();

    /*for( idx = SIZE; idx >= 0; idx-- )
    {
      // find next piece on board
        for( ; state->board[ idx ][ col ] != state->player && 
             idx > 0; --idx );

        if( idx < 0 )
            continue;

        // check for moves
        for( int i = 1; idx - i >= 0; i++ )
        {
            if( state->board[ idx - i ][ col ] == opposite_player( state->player ) )
            {
                if( state->board[ idx - (i+1) ][ col ] == 'O' ||
                    state->board[ idx - (i+1) ][ col ] == '0' )
                {
		  // has move
                    add_front( &actions, new_move( idx, col, idx - (i+1), col ) );

                    // check for more moves 
                    continue;
                }
                break;
            }
        }
	}*/

    for(idx = SIZE - 1; (idx > 0) && ((idx - 2) >= 0); idx--){
      if(  (state->board[ idx ][col] == state->player )
	    && (state->board[ idx - 1 ][ col ] == opposite_player( state->player ))
	    && (state->board[ idx - 2 ][ col ] == 'O')){
	   /* check for moves  - increment to optimize move*/
	   int end_row = idx - 2;
	   while( ((end_row - 2) > 0 )
	        && (state->board[ end_row ][ col ] == 'O' )
	        && (state->board[ end_row - 1 ][ col ] == opposite_player( state->player ))
	        && (state->board[ end_row - 2 ][ col ] == 'O') ){
	     end_row -= 2;
	   }
	   /* add move to action list */
	   add_front( &actions, new_move( idx, col, end_row, col) );
      }
    }

    return actions;
}
コード例 #15
0
/**
 * Find possible actions left on a row
 *
 * @param state a game state
 * @param row the row to check
 * @return a list of actions
 */
static struct List * actions_left( const struct State * state, int row )
{
    int idx = SIZE-1;
    struct List * actions = new_list();

    /*for( idx = SIZE - 1; idx >= 0; idx-- )
    {
      // find next piece on board
        for( ; state->board[ row ][ idx ] != state->player && idx > 0; idx-- );

        if( idx < 0 )
            continue;
        
        // check for moves
        for( int i = idx - 1; i >= 0; i-- )
        {
            if( state->board[ row ][ i ] == opposite_player( state->player ) )
            {
                if( state->board[ row ][ i - 1 ] == 'O' ||
                    state->board[ row ][ i - 1 ] == '0' )
                {
                   
                    //printf( "(%d,%d), (%d, %d)\n", row, idx, row, i - 1 );
                    add_front( &actions, new_move( row, idx, row, i - 1 ) );

                    // check for more moves
                    continue;
                }
                break;
            }
        }
	}*/

     for(idx = SIZE - 1; (idx > 0) && ((idx - 2) >= 0); idx--){
       if( (state->board[ row ][idx] == state->player )
	    && (state->board[ row ][ idx - 1 ] == opposite_player( state->player ))
	    && (state->board[ row ][ idx - 2 ] == 'O')){
	   /* check for moves  - increment to optimize move*/
	   int end_col = idx - 2;
	   while( ((end_col - 2) > 0 )
	        && (state->board[ row ][ end_col ] == 'O' )
	        && (state->board[ row ][ end_col - 1 ] == opposite_player( state->player ))
	        && (state->board[ row ][ end_col - 2 ] == 'O') ){
	     end_col -= 2;
	   }
	   /* add move to action list */
	   add_front( &actions, new_move( row, idx, row, end_col) );
       }
     }

    return actions;
}
コード例 #16
0
/**
 * Find possible actions down on a column
 *
 * @param state a game state
 * @param col the column to check
 * @return a list of actions
 */
static struct List * actions_down( const struct State * state, int col )
{
    int idx = 0;
    struct List * actions = new_list();

    /*for( idx = 0; idx < SIZE; idx++ )
    {
      // find next piece on board
        for( ; state->board[ idx ][ col ] != state->player && 
             idx < SIZE - 1; ++idx );

        if( idx > SIZE - 1 )
            continue;

        // check for moves
        for( int i = 1; i + idx < SIZE; i++ )
        {
            if( state->board[ idx + i ][ col ] == opposite_player( state->player ) )
            {
                if( state->board[ idx + i + 1 ][ col ] == 'O' ||
                    state->board[ idx + i + 1 ][ col ] == '0' )
                {
		  // has move
                    //printf( "(%d,%d), (%d, %d)\n", row, idx, row, idx + i + 1 );
                    add_front( &actions, new_move( idx, col, idx + i + 1, col  ));

                    // check for more moves
                    continue;
                }
                break;
            }
        }
	}*/
    for(idx = 0; (idx < SIZE) && ((idx + 2) < SIZE); idx++){
      if(  (state->board[ idx ][col] == state->player )
	    && (state->board[ idx + 1 ][ col ] == opposite_player( state->player ))
	    && (state->board[ idx + 2 ][ col ] == 'O')){
	   /* check for moves  - increment to optimize move*/
	   int end_row = idx + 2;
	   while( ((end_row + 2) < SIZE )
	        && (state->board[ end_row ][ col ] == 'O' )
	        && (state->board[ end_row + 1 ][ col ] == opposite_player( state->player ))
	        && (state->board[ end_row + 2 ][ col ] == 'O') ){
	     end_row += 2;
	   }
	   /* add move to action list */
	   add_front( &actions, new_move( idx, col, end_row, col) );
      }
    }
    
    return actions;
}
コード例 #17
0
/**
 * Find possible actions right on a row
 *
 * @param state a game state
 * @param row the row to check
 * @return a list of actions
 */
static struct List * actions_right( const struct State * state, int row )
{
    int idx = 0;
    struct List * actions = new_list();
    
    /*while( idx < SIZE - 1 )
    {
      // find next piece
        for( ; ( state->board[ row ][ idx ] != state->player ) &&
               ( idx < SIZE - 1 ); idx++ );

        //check if overbounds
        if( idx < SIZE - 1 )
        {
	  //find all moves
            for( int i = idx; i + 2 < SIZE; i++ )
                if( state->board[ row ][ i + 1 ] == opposite_player( state->player ) )
                    if( state->board[ row ][ i + 2 ] == 'O' )
                    {
                        add_front( &actions, new_move( row, idx, row, i + 2 ) );
                    }
                    else
                    {
                        break;
                    }
        }
        idx++;
	}*/
    for(idx = 0; (idx < SIZE) && ((idx + 2) < SIZE); idx++){
      if( (state->board[ row ][idx] == state->player )
	    && (state->board[ row ][ idx + 1 ] == opposite_player( state->player ))
	    && (state->board[ row ][ idx + 2 ] == 'O')){
	   /* check for moves  - increment to optimize move*/
	   int end_col = idx + 2;
	   while( ((end_col + 2) < SIZE )
	        && (state->board[ row ][ end_col ] == 'O' )
	        && (state->board[ row ][ end_col + 1 ] == opposite_player( state->player ))
	        && (state->board[ row ][ end_col + 2 ] == 'O') ){
	     end_col += 2;
	   }
	   /* add move to action list */
	   add_front( &actions, new_move( row, idx, row, end_col) );
      }
    }
    
    return actions;
}
コード例 #18
0
ファイル: playlist.c プロジェクト: mks65/tunez
void shuffle(){
	printf("shuffle \n");
	song_node *temp = 0;
	int i = 0;
	while (i++ < 26){
		song_node *a = table[i];
		while(a){
			temp = add_front(temp, a->song, a->artist);
			a = a->next;
		}
	}
	while (temp){
		song_node *rand = find_random(temp);
		printf("%s - %s\n", rand->artist, rand->song);
		temp = remove_song(temp, rand->song, rand->artist);
	}
  	printf("\n");
}
コード例 #19
0
ファイル: DOUBLELILIST11.C プロジェクト: 1we/DSLab
void main()
{
int ch;
clrscr();
while(1){
  clrscr();
  printf(".....double link list.....");
  printf("\n1.insert front\n2.insert before\n3.delete all occurence\n4.display list\n5.exit\n");
  printf("enter your choice\n");
  scanf("%d",&ch);
  switch(ch){
     case 1:add_front();break;
     case 2:insert_before();break;
     case 3:del();break;
     case 4:display();break;
     case 5:exit(0);
     }
getch();
  }
}
コード例 #20
0
void reverse(int fd) {

	int read_n, last_buffer_size;
	int buffer_count = 0;

	List* file_contents_list = (List*) malloc(sizeof(List));

	do{ /* read at least once */
		char * buffer = (char *) malloc(BUFFER_SIZE_IN_B);

		read_n = read(fd, buffer, BUFFER_SIZE_IN_B);

		if(read_n < 0){
			perror("Error reading file");
			exit(1);
		}
		
		if(read_n){ /* something was read */
			++buffer_count;
			last_buffer_size = read_n;

			add_front(file_contents_list, reversed_buffer(buffer, last_buffer_size));
		}else{ /* buffer is empty */
			free(buffer);
		}

	}while(!(read_n < BUFFER_SIZE_IN_B));

	close(fd);

	if(buffer_count) /* file wasn't empty */
		print_reversed(file_contents_list, buffer_count, last_buffer_size);

	/* do nothing if nothing was read */
	if(read_n)
		free_buffer_list(file_contents_list, buffer_count);

	free(file_contents_list);
}
コード例 #21
0
ファイル: address_book.c プロジェクト: b3h3moth/L-ASD
int main(void) {
    AddressBook *myabook;

    myabook = new_item("*****@*****.**", "foo", 1);
    
    /* Con la chiamata alla funzione add_front(), il nuovo elemento andra'
    in testa alla lista */
    myabook = add_front(myabook, new_item("*****@*****.**", "bar", 2));

    /* Con la chiamata alla funzione add_end(), il nuovo elemento andra'
    in coda alla lista */
    myabook = add_end(myabook, new_item("*****@*****.**", "baz", 3));

    // Stampa ciascun elemento della rubrica
    apply(myabook, print_values, "%d: <%s> \'%s\'\n");

    // Quanti elementi contiene la rubrica
    int n = 0;
    apply(myabook, count_elements, &n);
    printf("AddressBook contains \'%d\' elements\n", n);
    
    return(EXIT_SUCCESS);
}
コード例 #22
0
int main()
{
	node_t head = NULL;
	node_t first = NULL;
	node_t second = NULL;
	node_t merged = NULL;
	int choice = 0;
	int ele;
	int pos;
	node_t node_pointer = NULL;
	int n;
	int is_cycle = 0;
	node_t list_cycle = NULL;
	int isPalindrome = 0;
	int intersection = 0;
	node_t resultant_of_addition = NULL;
	int k = 0;
	int m;


	while(1)
	{
		printf("--------------------------------------------------------------------------------------------------------\n");
		printf("1. Add node in the front end \n");
		printf("2. Display the list \n");
		printf("3. Exit \n");
		printf("4. Add node at the rear end of the list \n");
		printf("5. Delete a node at the front end of the list \n");
		printf("6. Delete a node from the rear end of the list \n");
		printf("7. Insert a node in order to the list \n");
		printf("8. Merge two ordered linked lists \n");
		printf("9. Search for an item in the list \n");
		printf("10. Delete a node whose value is specified \n");
		printf("11. Delete a node at the specified position \n");
		printf("12. Reverse  list wihtout creating extra nodes \n");
		printf("13. Delete a node given only a pointer to the node \n");
		printf("14. Print middle element of the list \n");
		printf("15. Print the nth last element in the list \n");
		printf("16. Delete the entire list \n");
		printf("17. Detect a loop in the list \n");
		printf("18. Check whether a list is a palindrome \n");
		printf("19. Find the intersection of two lists \n");
		printf("20. Print reverse recursively \n");
		printf("21. Remove duplicates in a sorted linked list \n");
		printf("22. Move the last node in the list to the first node \n");
		printf("23. Reverse the list pairwise \n");
		printf("24. Find the intersection of two lists recursively \n");
		printf("25. Delete alternate nodes in the list \n");
		printf("26. Alternating split of the list \n");
		printf("27. Delete nodes whose neighbours value is greater \n");
		printf("28. Sepearate into even and odd in that order \n");
		printf("29. Add two lists and give the resultant list \n");
		printf("30. Rotate the list by k elements \n");
		printf("31. Separate into 0s and 1s \n");
		printf("32. Delete n nodes after the first m nodes \n");
		printf("-------------------------------------------------------------------------------------------------------\n");

		printf("Enter the choice\n");
		scanf("%d", &choice);

		switch(choice)
		{
			case 1:

					printf("Enter the element to enter to the front end of the list \n");
					scanf("%d", &ele);
					head = add_front(head, ele);
					break;

			case 2:
					display(head);
					break;

			case 3:
					exit(0);

			case 4:
					printf("Enter an element to be added to the end of the list \n");
					scanf("%d", &ele);
					head = add_end(head, ele);
					break;

			case 5:
					head = delete_front(head);
					break;

			case 6:
					head = delete_rear(head);
					break;

			case 7:
					printf("Enter the element to be inserted \n");
					scanf("%d", &ele);
					head = insert_in_order(head, ele);
					break;

			case 8:

					first = insert_in_order(first, 92);
					first = insert_in_order(first, 42);
					first = insert_in_order(first, 35);

					second = insert_in_order(second, 100);
					second = insert_in_order(second, 432);
					second = insert_in_order(second, 90);
					second = insert_in_order(second, 10);


					printf("The elements of the first list are: \n");
					display(first);

					printf("The elements of the second list are \n");
					display(second);

					merged = merge_ordered_lists(first, second);
					printf("The ordered list is: \n");
					display(merged);

			case 9:
					printf("Enter the element of the list to be searched for \n");
					scanf("%d", &ele);
					int pos = search(head, ele);
					if(pos != -1)
					{
						printf("The element is found at %d: \n", pos);
					}
					else
					{
						printf("The element is not found in the list \n");
					}

					break;

			case 10:
					printf("Enter the element of the list to be deleted: \n");
					scanf("%d", &ele);
					head = delete_with_info(head, ele);

					if(head == (node_t)NULL)
					{
						printf("The list is empty or the element u specified is not found: \n");
					}
					break;

			case 11:
					printf("Enter the position with the first node as position 1 \n");
					scanf("%d", &ele);
					head = delete_with_pos(head, ele);

					if(head == (node_t)NULL)
					{
						printf("Either the list is empty or the position specified is not appropriate \n");
					}

					break;

			case 12:

					head = reverse(head);
					break;

			case 13:
					node_pointer = head -> link -> link;
					delete_node_given_pointer(node_pointer);
					break;

			case 14:
					 print_middle(head);
					 break;

			case 15:
					printf("Enter the value of n \n");
					scanf("%d",&n);
					print_nth_last(head, n);
					break;

			case 16:
					head = delete_list(head);
					break;

			case 17:

					list_cycle = add_end(list_cycle,1);
					list_cycle = add_end(list_cycle,2);
					list_cycle = add_end(list_cycle,3);
					list_cycle = add_end(list_cycle,4);
					list_cycle = add_end(list_cycle,5);
					list_cycle -> link -> link -> link -> link -> link = list_cycle -> link;
					is_cycle = find_cycle(list_cycle);
					if(is_cycle)
					{
						printf("There is a cycle in the list \n");

					}

					else
					{
						printf("There is no cycle in the list \n");
					}



					break;

			case 18:

					isPalindrome = is_palindrome(&head, head);
					if(isPalindrome)
					{
						printf("The list is a palindrome \n");
					}

					else
					{
						printf("The list is not a palindrome \n");
					}
					break;

			case 19:

					first = add_end(first,10);
					first = add_end(first,20);
					


					second = add_end(second,43);
					second = add_end(second,3);
					second = add_end(second,34);
					second = add_end(second,44);

					first -> link -> link = second -> link;


					intersection = find_intersection(first, second);
					printf("The intersection point of the two lists are %d \n", intersection);
					break;

			case 20:
					print_reverse_recursively(head);
					printf("\n");
					break;

			case 21:

					remove_duplicates(head);
					break;

			case 22:

					head = move_last_to_first(head);
					break;

			case 23:

					head = pairwise_reverse(head);
					break;

			case 24:

					first = add_end(first, 10);
					first = add_end(first, 30);
					first = add_end(first, 40);
					first = add_end(first, 50);
					first = add_end(first, 60);


					second = add_end(second, 10);
					second = add_end(second, 20);
					second = add_end(second, 30);


					find_common_recursively(first, second);
					break;

			case 25:
					head = delete_alternate(head);
					break;

			case 26:
					 alternating_split_v2(head);
					 break;

			case 27:
					head = delete_node_when_neigbour_higher(head);
					break;

			case 28:
					head = separate_into_even_odd_v2(head);
					break;

			case 29:
						first = add_front(first, 2);
						first = add_front(first, 4);
						first = add_front(first, 8);


						second = add_front(second,2);
						second = add_front(second,4);
						second = add_front(second,5);
						second = add_front(second,3);

						resultant_of_addition = add_two_lists(first, second);

						printf("The resultant list is as follows \n");
						display(resultant_of_addition);

						break;	

			case 30:
					printf("Enter the value of k \n");
					scanf("%d",&k);
					head = rotate_by_k(head, k);
					break;

			case 31:
					head = separate_into_zeroes_ones(head);
					break;

			case 32:
					printf("Enter the value of m \n");
					scanf("%d", &m);
					printf("Enter the value of n \n");
					scanf("%d", &n);
					head = retain_m_delte_n(head, m , n);
					break;



			default:
					printf("Invalid choice... Please try again \n");
					break;

		}

	}
}
コード例 #23
0
ファイル: qualified_id.hpp プロジェクト: alekratz/matlab2r
 qualified_id(qualified_id_item_p initial_item)
     { add_front(initial_item); }
コード例 #24
0
ファイル: test.c プロジェクト: will-wang/portfolio
int main(void) {
  llist* list = create_list();
  lnode* new_node;
  lnode* found_node;
  point* p = create_point(0, 0);
  list->head = NULL;

  /* test case 1 - what does an empty list contain? */
  printf("TEST CASE 1\n");
  traverse(list, print_point_node);
  printf("\n");

  /* test case 2 - what happens when you add a node to the
     front of an empty list? */
  printf("TEST CASE 2\n");
  new_node = create_node(create_point(2,5));
  add_front(list, new_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 3 - what happens when you add a node to the front
     of a list with one node? */
  printf("TEST CASE 3\n");
  new_node = create_node(create_point(3,7));
  add_front(list, new_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 4 - what happens when you add a node to the front
     of a list with more than one node? */
  printf("TEST CASE 4\n");
  new_node = create_node(create_point(1,4));
  add_front(list, new_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 5 - what happens when you remove a node from the front
     of a list with more than one node? */
  printf("TEST CASE 5\n");
  remove_front(list, free_point_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 6 - what happens when you remove a node from the front
     of a list with one node? */
  printf("TEST CASE 6\n");
  remove_front(list, free_point_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 7 - what happens when you remove a node from the front
     of an empty list? */
  printf("TEST CASE 7\n");
  remove_front(list, free_point_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 8 - what happens when you add a node to the
     back of an empty list? */
  printf("TEST CASE 8\n");
  new_node = create_node(create_point(2,5));
  add_back(list, new_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 9 - what happens when you add a node to the back
     of a list with one node? */
  printf("TEST CASE 9\n");
  new_node = create_node(create_point(3,7));
  add_back(list, new_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 10 - what happens when you add a node to the back
     of a list with more than one node? */
  printf("TEST CASE 10\n");
  new_node = create_node(create_point(1,4));
  add_back(list, new_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 11 - what happens when you remove a node from the back
     of a list with more than one node? */
  printf("TEST CASE 11\n");
  remove_back(list, free_point_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 12 - what happens when you remove a node from the back
     of a list with one node? */
  printf("TEST CASE 12\n");
  remove_back(list, free_point_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 13 - what happens when you remove a node from the back
     of an empty list? */
  printf("TEST CASE 13\n");
  remove_back(list, free_point_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 14 - what happens when you try to find an occurrence
     of a given data point in an empty list? */
  printf("TEST CASE 14\n");
  p->x = 3;
  p->y = 7;
  found_node = find_occurrence(list, p, compare_point_data);
  print_point_node(found_node);
  printf("\n");

  /* test case 15 - what happens when you try to find an occurrence
     of a given data point in a list of that one node? */
  printf("TEST CASE 15\n");
  p->x = 3;
  p->y = 7;
  new_node = create_node(create_point(3, 7));
  add_front(list, new_node);
  found_node = find_occurrence(list, p, compare_point_data);
  print_point_node(found_node);
  printf("\n");
  
  /* test case 16 - what happens when you try to find an occurrence
     of a given data point in a list of one different node? */
  printf("TEST CASE 16\n");
  p->x = 5;
  p->y = 14;
  found_node = find_occurrence(list, p, compare_point_data);
  print_point_node(found_node);
  printf("\n");

  /* test case 17 - what happens when you try to find an occurrence
     of a given data point in a list of lots of nodes (one matching)?
     NOTE: if your list contains multiple nodes with matching data,
     returning any of them is fine.
  */
  printf("TEST CASE 17\n");
  new_node = create_node(create_point(5,2));
  add_front(list, new_node);
  new_node = create_node(create_point(6,30));
  add_back(list, new_node);
  new_node = create_node(create_point(1,1));
  add_front(list, new_node);
  p->x = 3;
  p->y = 7;
  found_node = find_occurrence(list, p, compare_point_data);
  print_point_node(found_node);
  printf("\n");

  /* test case 18 - what happens when you try to find an occurrence
     of a given data point in a list of lots of nodes (none matching)? */
  printf("TEST CASE 18\n");
  p->x = 5;
  p->y = 14;
  found_node = find_occurrence(list, p, compare_point_data);
  print_point_node(found_node);
  printf("\n");

  /* test case 19 - what happens when you try to free a list
     of lots of nodes? YOU WILL HAVE TO RUN VALGRIND TO MAKE
     SURE YOU GET NO ERRORS HERE. Memory leaks are otherwise
     invisible!
  */
  printf("TEST CASE 19\n");
  free_list(list, free_point_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 20 - what happens when you try to free a list
     of one node? YOU WILL HAVE TO RUN VALGRIND TO MAKE
     SURE YOU GET NO ERRORS HERE. 
  */
  printf("TEST CASE 20\n");
  new_node = create_node(create_point(7,14));
  add_front(list, new_node);
  free_list(list, free_point_node);
  traverse(list, print_point_node);
  printf("\n");

  /* test case 21 - what happens when you try to free an empty list?
     YOU WILL HAVE TO RUN VALGRIND TO MAKE SURE YOU GET NO ERRORS HERE.
  */
  printf("TEST CASE 21\n");
  free_list(list, free_point_node);
  traverse(list, print_point_node);
  printf("\n");
  

	free(p);
  free(list);
  return 0;
}
コード例 #25
0
/**
 * Validate an action
 *
 * @param state a state to check
 * @param action a move to perform
 * @return 1 if action is valid, else return 0
 */
int validate_action( const struct State * state, const struct Move * action )
{
    struct List * moves = new_list();
    struct List * temp_moves;
    struct ListNode * current;
    int is_valid = 0;

    /* combine all actions */
    for( int i = 0; i < SIZE; i++ )
    {
        /* actions right */
        temp_moves = actions_right( state, i );

        current = temp_moves->head; 
        while( current != NULL )
        {
            add_front( &moves, current->data );
            current = current->next;
        }

        delete_list( &temp_moves );

        /* actions left */
        temp_moves = actions_left( state, i );

        current = temp_moves->head; 
        while( current != NULL )
        {
            add_front( &moves, current->data );
            current = current->next;
        }

        delete_list( &temp_moves );

        /* actions up */
        temp_moves = actions_up( state, i );

        current = temp_moves->head; 
        while( current != NULL )
        {
            add_front( &moves, current->data );
            current = current->next;
        }

        delete_list( &temp_moves );

        /* actions down */
        temp_moves = actions_down( state, i );

        current = temp_moves->head; 
        while( current != NULL )
        {
            add_front( &moves, current->data );
            current = current->next;
        }

        delete_list( &temp_moves );
    }

    /* check if moves is in possible actions */
    struct Move * move;
    current = moves->head;
    while( current != NULL )
    {
        move = current->data;
        if( compare_move( move, action ) == 1 )
        {
            is_valid = 1;
        }
        current = current->next;

        Free( move, sizeof( struct Move ) );
    }

    delete_list( &moves );

    return is_valid;
}
コード例 #26
0
/**
 * Find all possible actions/moves in a state
 *
 * @param state a state to check for moves
 * @return a list of actions/moves
 */
struct List * actions( const struct State * state )
{

    struct List * moves = new_list();
    struct List * temp_moves;
    struct ListNode * current;

    /* combine all actions */
    for( int i = 0; i < SIZE; i++ )
    {
        /* actions right */
        temp_moves = actions_right( state, i );

        current = temp_moves->head; 
        while( current != NULL )
        {
            add_front( &moves, current->data );
            current = current->next;
        }

        delete_list( &temp_moves );

        /* actions left */
        temp_moves = actions_left( state, i );

        current = temp_moves->head; 
        while( current != NULL )
        {
            add_front( &moves, current->data );
            current = current->next;
        }

        delete_list( &temp_moves );

        /* actions up */
        temp_moves = actions_up( state, i );

        current = temp_moves->head; 
        while( current != NULL )
        {
            add_front( &moves, current->data );
            current = current->next;
        }

        delete_list( &temp_moves );

        /* actions down */
        temp_moves = actions_down( state, i );

        current = temp_moves->head; 
        while( current != NULL )
        {
            add_front( &moves, current->data );
            current = current->next;
        }

        delete_list( &temp_moves );
    }

    return moves;
}
コード例 #27
0
/**
 * Add a child to a game node
 *
 * @param parent a parent node
 * @param child the child node to add to parent
 */
void add_child_game_node( struct GameNode * parent, struct GameNode * child )
{
    child->parent = parent;
    add_front( &parent->children, child );
}