【发布时间】:2016-11-04 18:34:58
【问题描述】:
我正在尝试制作一个循环链表来模拟旋转轮盘赌。我已经搜索了网络并在我的代码中运行了我的逻辑,但我似乎无法发现我的逻辑中的错误。我真正需要帮助的是了解如何制作双向链表循环。提前感谢您的帮助。该程序的目标是生成一个随机数,然后使用该数字遍历循环双向链表。目前,当我运行程序时,迭代到最后一个数字,然后重复最后一个数字,剩余的迭代不在随机数中。
/*
Roulette program
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node{
int value;
struct node * next;
struct node * prev;
};
struct node * create_node( struct node *, int );
void print_forwards( struct node *, int );
void print_backwards( struct node * );
struct node * get_last_node( struct node * );
int main ( int argc, char * argv[] ) {
printf("Welcome to the game of Roulette!\n");
int rand_num;
int i;
srand( time(NULL) );
rand_num = rand() % 400 + 1;
printf("\nFowards Random #: %d\n", rand_num );
// Numbers in order on a European Roulette wheel.
//0-32-15-19-4-21-2-25-17-34-6-27-13-36-11-30-8-23-10-5-24-16-33-1-20-
//14-31-9-22-18-29-7-28-12-35-3-26
struct node * the_list = NULL;
the_list = create_node( the_list, 0 );
the_list = create_node( the_list, 32 );
the_list = create_node( the_list, 15 );
the_list = create_node( the_list, 19 );
the_list = create_node( the_list, 4 );
the_list = create_node( the_list, 21 );
the_list = create_node( the_list, 2 );
the_list = create_node( the_list, 25 );
the_list = create_node( the_list, 17 );
the_list = create_node( the_list, 34 );
the_list = create_node( the_list, 6 );
the_list = create_node( the_list, 27 );
the_list = create_node( the_list, 13 );
the_list = create_node( the_list, 36 );
the_list = create_node( the_list, 11 );
the_list = create_node( the_list, 30 );
the_list = create_node( the_list, 8 );
the_list = create_node( the_list, 23 );
the_list = create_node( the_list, 10 );
the_list = create_node( the_list, 5 );
the_list = create_node( the_list, 24 );
the_list = create_node( the_list, 16 );
the_list = create_node( the_list, 33 );
the_list = create_node( the_list, 1 );
the_list = create_node( the_list, 20 );
the_list = create_node( the_list, 14 );
the_list = create_node( the_list, 31 );
the_list = create_node( the_list, 9 );
the_list = create_node( the_list, 22 );
the_list = create_node( the_list, 18 );
the_list = create_node( the_list, 29 );
the_list = create_node( the_list, 7 );
the_list = create_node( the_list, 28 );
the_list = create_node( the_list, 12 );
the_list = create_node( the_list, 35 );
the_list = create_node( the_list, 3 );
the_list = create_node( the_list, 26 );
print_forwards( the_list, rand_num );
// print_backwards( the_list );
getchar();
return 0;
}
struct node * create_node( struct node * the_list, int new_value ){
// Create the new node and set it value.
// Also set the next and prev. pointers to NULL.
struct node * head = (struct node *)malloc( sizeof( struct node ) );
head->value = new_value;
head->next = head;
head->prev = head;
struct node * temp;
temp->prev = (struct node *)malloc( sizeof( struct node ) );
temp = temp->prev;
temp->prev = head;
head->next = temp;
// create the new node at the front of the list.
if( the_list ){
// Set the existing list's prev pointer
// to point to our new node.
the_list->prev = head;
}
// In any case, set our new node to the list.
// This will work fine if the list is empty
// because the "the_list" pointer is NULL.
head->next = the_list;
return head;
}
void print_backwards( struct node * the_list ){
printf( "\n-- Printing Backwards --\n" );
while( the_list ){
printf( "%d\n",
the_list->value
);
the_list = the_list->next;
}
}
void print_forwards( struct node * the_list, int rand_num ){
printf( "\n-- Printing Forwards --\n" );
int i;
struct node * end = get_last_node( the_list );
for( i = 0; i < rand_num; i++ ){
printf( "%d ",
end->value
);
end = end->prev;
}
}
struct node * get_last_node( struct node * the_list ){
while( the_list ){
if( the_list->next == NULL ){
break;
}else{
the_list = the_list->next;
}
}
return the_list;
}
【问题讨论】:
-
你想要代码还是只是解释?
-
可能有更简单的方法来实现这个循环链表。
-
转换为圆形很简单。首先,一些问题:您希望
create_node插入列表的前面/头部还是后面/尾部?您定义了一个带有双向链接指针的节点,但您的某些代码似乎只使用了单链接列表? -
不确定你的任务是什么,但我认为
malloc根本不合适。轮盘赌是提前知道的,在运行时不需要创建任何东西。我只是静态地声明它,随机数 mod 空格数,这是你的旋转。也就是说,在我看来,你在create_node函数中两次mallocing 看起来很可疑。如果该功能的目的是在轮子上创建一个空间(我认为是),那么您应该mallocing 一个空间。 -
提示:在循环列表中,您永远不应该到达空指针。
标签: c logic circular-list