예제 #1
0
파일: list.c 프로젝트: telescreen/misc
int main(int argc, char **argv)
{
	struct person *p;
	int tmp_age;
	char tmp_name[32];
	int i, n;

	list_t *l = list_init();
	node_t *tmp_node;

	printf("Number of people: ");
	scanf("%d", &n);

	for (i = 0; i < n; ++i) {
		p = (struct person*)malloc(sizeof(struct person));
		assert(p != NULL);
		printf("age: ");
		scanf("%d", &p->age);
		while(getchar()!='\n');
		printf("name: ");
		fgets(p->name, 32, stdin);
		p->name[strlen(p->name) - 1] = '\0';

		tmp_node = node_init(p);
		list_insert(l, tmp_node);
	}
	printf("List length: %d\n", l->length);

	printf("Traverse list forward\n");
	list_traverse(l, LIST_FORWARD, dump_data);
	printf("Traverse list backward\n");
	list_traverse(l, LIST_BACKWARD, dump_data);		

	tmp_node = list_remove_head(l);
	printf("Removed node: \n");
	dump_data(tmp_node);
	free(tmp_node);
	printf("New list. List length = %d\n", l->length);
	list_traverse(l, LIST_FORWARD, dump_data);

	tmp_node = list_remove_tail(l);
	printf("Removed node: \n");	
	dump_data(tmp_node);
	free(tmp_node);
	printf("New list. List length = %d\n", l->length);
	list_traverse(l, LIST_FORWARD, dump_data);

	list_free(l);
	
	return 0;
}
// 不带头节点的单向不循环链表
int main(void)
{
	person_t person;
	char buf[128];

	struct node_t *head = NULL;

	while (1)
	{
	retry:
		printf("pls input n: ");	
		my_scanf(goto retry, "%d", &person.id);

		if (-1 == person.id)
			break;			

		person.sex = "MF"[rand() % 2];
		printf("pls input name: ");
		my_fgets(buf, 128);

		person.name = malloc(strlen(buf) + 1);
		memcpy(person.name, buf, strlen(buf) + 1);
		
		add_node(&head, &person, sizeof(person_t));
	}

	list_traverse(head, print_person);
	list_free(head, name_free);

	return 0;
}
예제 #3
0
void *cache_check(cache_t cache, void *data, cache_match_func_t match)
{
   char *found;

   /* Check for an empty cache. */
   if (list_size(cache->list) == 0) {
      return(NULL);
   }

   /* Ok.  Search the list for the element, starting from the front
    * of our list.  If the traversal finds the element, then promote it to
    * the front if it's not already there, and return a pointer to the element.
    * Otherwise, return NULL.  In either case, make sure to reset the
    * current element pointer back to the front of the list.
    */
   if (list_traverse(cache->list, data, match,
		     (LIST_FRNT | LIST_FORW | LIST_ALTR)) == LIST_OK) {
      /* We found what we're looking for. */
      if (list_curr(cache->list) != list_front(cache->list)) {
	 fprintf(stderr, "cache_check: moving found to front.\n");
	 found = (char *) list_remove_curr(cache->list);
	 list_mvfront(cache->list);
	 list_insert_before(cache->list, found, 0);
	 return(found);
      }
      else {
	 return(list_front(cache->list));
      }
   }
   else {
      /* We did not find the element. */
      list_mvfront(cache->list);
      return(NULL);
   }
}
예제 #4
0
파일: rig_list_lock.c 프로젝트: llongi/rig
bool rig_list_remove(RIG_LIST l, void *item) {
	NULLCHECK_EXIT(l);
	NULLCHECK_EXIT(item);

	size_t key = rig_hash(item, l->typeinfo.size, RIG_HASH_MURMUR2);

	MX_LOCK(l);

	// Find first occourrence of item in list, or the right next key (if not present)
	Node prev, curr;
	list_traverse(l, l->head, key, item, &prev, &curr);

	if (curr != NULL && curr->key == key) {
		// Item present, remove it
		prev->next = curr->next;

		l->count--;

		MX_UNLOCK(l);

		// Free memory
		free(curr);

		return (true);
	}

	MX_UNLOCK(l);

	errno = ENOENT;
	return (false);
}
예제 #5
0
int check_list()
{
  LIST *list, *listreturn;

  char *dataptr;

  size_t counter;

  int travreturn;
  
  list = list_init();
  if(!list)
    return 1;

  /* test if list is functional without anything in it (expect NULL return) */
  listreturn = list_mvprev(list);
  if(listreturn)
    return 2;

  /* play with some data */
  for(counter = 0; datas[counter]; counter ++)
    {
      dataptr = list_insert_after(list, datas[counter], strlen(datas[counter]) + 1);
      if(strcmp(dataptr, datas[counter]))
	{
	  fprintf(stderr, "got %s, expecting %s\n", dataptr, datas[counter]);
	  return 3;
	}
      
    }

  for(counter = 0; datas[counter]; counter ++)
    {
      dataptr = list_insert_before(list, datas[counter], strlen(datas[counter]) + 1);
      if(strcmp(dataptr, datas[counter]))
	{
	  fprintf(stderr, "got %s, expecting %s\n", dataptr, datas[counter]);
	  return 4;
	}
    }

  /* test traversal */
  travreturn = list_traverse(list, (void *)NULL, &list_traverse_print, 0);
  if(travreturn != LIST_EXTENT)
    return 5;

  /* The defaults for list traversal are to not change the current
     element of the list. Thus, list_curr should return the last
     inserted piece of data which is at datas[counter - 1] */
  dataptr = (char *)list_curr(list);
  if(strcmp(dataptr, datas[counter - 1]))
    {
      fprintf(stderr, "get %s, expecting %s\n", dataptr, datas[counter - 1]);
      return 6;
    }

  list_free(list, LIST_DEALLOC);

  return 0;
}
예제 #6
0
파일: test.c 프로젝트: royye62/mydemo
int main(void)
{
	int i;
	struct node_t *head = NULL;

	for (i = 0; i < 10; i++)
		list_append(&head, &i, sizeof(int));
		
	list_traverse(head, print);

	head = list_reverse(head);

	list_traverse(head, print);

	return 0;
}
예제 #7
0
int main() {
  struct list *list;
  struct entry *entry;

  // NOTE: 30エントリ追加するためのスレッド2つ
  pthread_t put_thirty_entries1, put_thirty_entries2;
  // NOTE: 10エントリ取り出すためのスレッド6つ
  pthread_t get_ten_entries1, get_ten_entries2, get_ten_entries3,
            get_ten_entries4, get_ten_entries5, get_ten_entries6;

  list = list_init();

  pthread_create(&put_thirty_entries1, NULL, put_thirty_entries, (void *)(list));
  pthread_create(&get_ten_entries1, NULL, get_ten_entries, (void *)(list));
  pthread_create(&get_ten_entries2, NULL, get_ten_entries, (void *)(list));
  pthread_create(&get_ten_entries3, NULL, get_ten_entries, (void *)(list));
  pthread_create(&get_ten_entries4, NULL, get_ten_entries, (void *)(list));
  pthread_create(&get_ten_entries5, NULL, get_ten_entries, (void *)(list));
  pthread_create(&put_thirty_entries2, NULL, put_thirty_entries, (void *)(list));
  pthread_create(&get_ten_entries6, NULL, get_ten_entries, (void *)(list));

  pthread_join(put_thirty_entries1, NULL);
  pthread_join(get_ten_entries1, NULL);
  pthread_join(get_ten_entries2, NULL);
  pthread_join(get_ten_entries3, NULL);
  pthread_join(get_ten_entries4, NULL);
  pthread_join(get_ten_entries5, NULL);
  pthread_join(put_thirty_entries2, NULL);
  pthread_join(get_ten_entries6, NULL);

  /* entry list */
  list_traverse(list, print_entry, NULL);

  return (0);
}
예제 #8
0
파일: main.c 프로젝트: Zeno-/gears
int main(void)
{
	List *mylist = list_new();
	ListNode *cnode;
	int i;
	int r = 0;

	if (!mylist)
		return 1;

#if 1
	for (i = 0; i < 15; i++) {
		struct intnode *newnode = malloc(sizeof(*newnode));
		if (!newnode) {
			r = 1;
			goto mallocerror;
		}
		newnode->v = i;
		list_prepend(mylist, (ListNode *)newnode);
		assert(list_contains_node(mylist, (ListNode *)newnode));
	}


#else
	for (i = 0; i < 5; i++) {
		struct intnode *newnode = malloc(sizeof(*newnode));
		if (!newnode) {
			r = 1;
			goto mallocerror;
		}
		newnode->v = i;
		list_append(mylist, (ListNode *)newnode);
	}


#endif

	printf("List length: %lu\n", (unsigned long)mylist->len);

#if 0
	for (cnode = list_head(mylist); cnode != NULL; cnode = list_next(mylist, cnode)) {
		printf("%d\n", ((struct intnode *)cnode)->v);
	}
#else
	list_traverse(mylist, print_intnode);
#endif

mallocerror:

	list_dispose(mylist);

	return r;
}
예제 #9
0
파일: game.c 프로젝트: pyrated/jellypaddle
static void step(int value) {
  static double dt;
  int i;
  t1 = get_time();
  dt = t1 - t0;
  t0 = t1;

  reprocess_keys();
  list_traverse(bodies, do_step, &dt);
  list_traverse(bodies, do_verlet, &dt);

  for (i = 0; i < 5; i++) {
    list_traverse(bodies, do_edges, NULL);
    list_traverse(bodies, do_center, NULL);
    list_traverse(bodies, do_collisions, NULL);
  }

  glClear(GL_COLOR_BUFFER_BIT);

  glUseProgram(body_program->id);
  glEnableVertexAttribArray(body_program->attribute[0]);
  glEnableVertexAttribArray(body_program->attribute[1]);
  list_traverse(bodies, do_render, &dt);

  glutSwapBuffers();
  glutTimerFunc(16.6667, step, 0);
}
예제 #10
0
파일: rig_list_lock.c 프로젝트: llongi/rig
bool rig_list_insert(RIG_LIST l, void *item) {
	NULLCHECK_EXIT(l);
	NULLCHECK_EXIT(item);

	// Allocate memory for the new element
	Node node = malloc(sizeof(*node) + l->typeinfo.size);
	if (node == NULL) {
		errno = ENOMEM;
		return (false);
	}

	// Set the content of the new element
	size_t key = rig_hash(item, l->typeinfo.size, RIG_HASH_MURMUR2);
	node->key = key;
	str_ops_copy(node->data, item, l->typeinfo.size);

	MX_LOCK(l);

	if (l->count == l->capacity)  {
		// List full
		MX_UNLOCK(l);
		free(node);

		errno = EXFULL;
		return (false);
	}

	// Find first occourrence of item in list, or the right next key (if not present)
	Node prev, curr;
	list_traverse(l, l->head, key, item, &prev, &curr);

	if (!TEST_BITFIELD(l->flags, RIG_LIST_ALLOWDUPS) && curr != NULL && curr->key == key) {
		// No duplicates allowed and item already present, return
		MX_UNLOCK(l);
		free(node);

		errno = EEXIST;
		return (false);
	}

	// Link the new element in
	node->next = curr;
	prev->next = node;

	l->count++;

	MX_UNLOCK(l);

	return (true);
}
예제 #11
0
int
main()
{
	list *l = list_create(NULL);

	printf("0 list_length %d\n", list_length(l));
	list_append_string(l, _STRDUP("niels"));
	printf("1 list_length %d\n", list_length(l));
	list_append_string(l, _STRDUP("nes"));
	printf("1 list_length %d\n", list_length(l));
	list_append_string(l, _STRDUP("lilian"));
	printf("1 list_length %d\n", list_length(l));
	list_append_string(l, _STRDUP("nes"));
	printf("1 list_length %d\n", list_length(l));
	list_append_string(l, _STRDUP("max"));
	printf("1 list_length %d\n", list_length(l));
	list_append_string(l, _STRDUP("nes"));
	printf("1 list_length %d\n", list_length(l));
	list_traverse(l, print_data, NULL);
	printf("\n");

	list_traverse(l, destroy_data, NULL);
	list_destroy(l);
}
예제 #12
0
int main(int argc, char *argv[])
{
	lnode_t *phead = NULL;

	char ch;

//	while((ch = getchar()) != '\n')
	int i;
	for(i = 0; i < 5; i++)
	{

		char *p = malloc(1000);

		gets(p);
		
		list_insert(&phead, &p, 4, cmp);
	}

	list_traverse(phead, print);
	
	putchar('\n');

	char *buf = malloc(1000);
	gets(buf);

	
	if(list_search(phead, &buf, cmp) != NULL)
			printf("%s is found.\n", buf);
	else
			puts("not found.");
	
	free(buf);


	list_destroy(phead, free_data);


	return 0;
}
예제 #13
0
파일: test.c 프로젝트: royye62/mydemo
// 不带头节点的单向不循环链表
int main(void)
{
	int n;
	int indx;
	int key;
	struct node_t *head = NULL;

	while (1)
	{
	retry:
		printf("pls input n[-1 quit]: ");	
		my_scanf(goto retry, "%d", &n);

		if (-1 == n) 
			break;			

		list_append(&head, &n, sizeof(int));
	}

	list_traverse(head, print_int);
	putchar('\n');

	printf("test reverse list: \n");
	head = list_reverse(head);	
	list_traverse(head, print_int);
	putchar('\n');

	printf("test insert indx, pls input n: ");
	my_scanf( , "%d", &n);
	printf("pls input indx: ");
	my_scanf( , "%d", &indx);
	list_insert_indx(&head, &n, sizeof(int), indx);
	
	list_traverse(head, print_int);
	putchar('\n');

	printf("test restrieve, pls input indx: ");
	my_scanf(, "%d", &indx);
	list_retrieve(&head, &n, sizeof(int), indx);
	printf("the data you retrieve is %d\n", n);

	printf("test search, pls input key: ");
	my_scanf(, "%d", &key);
	indx = list_search(&head, &key, cmp_int);
	printf("the index you search is %d\n", indx);
	
	printf("test delby indx, pls input indx: ");
	my_scanf( , "%d", &indx);
	list_delby_indx(&head, indx, NULL);

	list_traverse(head, print_int);
	putchar('\n');

	printf("test delby key, pls input key: ");
	my_scanf( , "%d", &key);	
	list_delby_key(&head, &key, cmp_int, NULL);

	list_traverse(head, print_int);
	putchar('\n');

	list_free(head, NULL);

	return 0;
}