- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个循环链表来模拟旋转轮盘赌轮。我已经搜索了网络并在代码中运行了我的逻辑,但我似乎无法发现我的逻辑中的错误。我真正需要帮助的是了解如何使双向链表循环。预先感谢您的任何帮助。该程序的目标是生成一个随机数,然后使用该数字迭代循环双向链表。目前,当我运行程序时,会迭代到最后一个数字,然后重复该最后一个数字,剩余的迭代将不包含在随机数中。
/*
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;
}
最佳答案
警告:正如其他人所提到的,使用固定长度数组而不是循环列表可能更容易,并将索引包装到以数组大小为模的数组中。
但是,话虽如此,回答你的问题......
为了说明如何将双向链表转换为循环链表,我提供了这两种类型的函数。您可以通过在命令行上指定-c
来选择循环。
我从你的代码开始,但我必须进行相当多的重组(例如一些错误修复和添加一些缺失的代码)才能使事情正常工作[请原谅无偿的风格清理]:
/*
Roulette program
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node {
int value;
struct node *next;
struct node *prev;
};
int circflg;
int opt_d;
struct node *create_node(struct node *, int);
void print_forwards(struct node *, int);
void print_backwards(struct node *);
// main -- main program
int
main(int argc,char **argv)
{
char *cp;
--argc;
++argv;
for (; argc > 0; --argc, ++argv) {
cp = *argv;
if (*cp != '-')
break;
switch (cp[1]) {
case 'c':
circflg = ! circflg;
break;
case 'd':
opt_d = ! opt_d;
break;
default:
break;
}
}
printf("Welcome to the game of Roulette!\n");
int rand_num;
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;
}
int
print_entry(int totlen,int val)
{
int curlen;
char buf[100];
curlen = sprintf(buf," %2d",val);
if ((totlen + curlen) > 78) {
printf("\n");
totlen = 0;
}
fputs(buf,stdout);
totlen += curlen;
return totlen;
}
struct node *
list_last_node(struct node *the_list)
{
struct node *cur;
struct node *end;
end = the_list;
for (cur = the_list; cur != NULL; cur = cur->next)
end = cur;
return end;
}
struct node *
list_create_node(struct node *the_list, int new_value)
{
struct node *prev;
// Create the new node and set it value.
struct node *temp = (struct node *) malloc(sizeof(struct node));
// set the new value
temp->value = new_value;
// create the new node at the front of the list.
if (the_list == NULL) {
the_list = temp;
temp->next = NULL;
temp->prev = NULL;
}
// append to end
else {
prev = list_last_node(the_list);
temp->next = NULL;
temp->prev = prev;
prev->next = temp;
}
return the_list;
}
void
list_print_backwards(struct node *the_list)
{
struct node *cur;
struct node *end;
struct node *prev;
int len;
printf("\n-- Printing Backwards --\n");
cur = list_last_node(the_list);
len = 0;
for (; cur != NULL; cur = prev) {
len = print_entry(len,cur->value);
prev = cur->prev;
}
printf("\n");
}
void
list_print_forwards(struct node *the_list, int rand_num)
{
int i;
struct node *cur;
struct node *next;
int len;
printf("\n-- Printing Forwards --\n");
len = 0;
i = 0;
for (cur = the_list; cur != NULL; cur = next, ++i) {
if (i >= rand_num)
break;
next = cur->next;
len = print_entry(len,cur->value);
}
printf("\n");
}
struct node *
circ_create_node(struct node *the_list, int new_value)
{
struct node *prev;
// Create the new node and set it value.
struct node *temp = (struct node *) malloc(sizeof(struct node));
// set the new value
temp->value = new_value;
do {
// create the new node at the front of the list.
if (the_list == NULL) {
the_list = temp;
temp->next = temp;
temp->prev = temp;
break;
}
// append to end
prev = the_list->prev;
temp->next = the_list;
temp->prev = prev;
prev->next = temp;
the_list->prev = temp;
} while (0);
return the_list;
}
void
circ_print_backwards(struct node *the_list)
{
struct node *cur;
struct node *prev;
int len;
printf("\n-- Printing Backwards --\n");
len = 0;
for (cur = the_list; cur != NULL; cur = prev) {
len = print_entry(len,cur->value);
prev = cur->prev;
if (prev == the_list)
break;
}
printf("\n");
}
void
circ_print_forwards(struct node *the_list, int rand_num)
{
int i;
struct node *cur;
struct node *next;
int len;
printf("\n-- Printing Forwards --\n");
i = 0;
len = 0;
for (cur = the_list; cur != NULL; cur = next, ++i) {
if (i >= rand_num)
break;
next = cur->next;
len = print_entry(len,cur->value);
if (next == the_list)
break;
}
printf("\n");
}
struct node *
create_node(struct node *the_list, int new_value)
{
if (circflg)
the_list = circ_create_node(the_list,new_value);
else
the_list = list_create_node(the_list,new_value);
if (opt_d) {
printf("create_node: added %d\n",new_value);
print_forwards(the_list,-1);
print_backwards(the_list);
}
return the_list;
}
void
print_backwards(struct node *the_list)
{
if (circflg)
circ_print_backwards(the_list);
else
list_print_backwards(the_list);
}
void
print_forwards(struct node *the_list,int rand_num)
{
if (circflg)
circ_print_forwards(the_list,rand_num);
else
list_print_forwards(the_list,rand_num);
}
关于c - 如何在c中创建一个双向链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40429669/
前言:笔记是参考B站up主尚硅谷,图片、代码都是哦。在blog写笔记~(图片、代码来源尚硅谷,侵权必删!) 尚硅谷数据结构学习路线B站网站:https://www.bilibili.com/video
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我想创建一个没有全局变量的单个链表。我用 NULL 初始化了第一个元素,然后想将第一个元素 node 复制到 list_。它被复制到函数中,但副作用不起作用。在我的主函数中,该值仍然是NULL。如果我
我正在尝试使链表与此处的链表相似: linked list in C 那就是在另一个结构中有“头”,我首先称它为“头”。但是我发现做那个改变。很难向 list_item 结构添加值。我已经尝试了一些东
我正在尝试理解链表的代码。我明白他们是如何工作的。我正在查看一些与动态内存和链表有关的代码,我在此处对其进行了简化: #include #include typedef struct nod
有人可以解释下面的代码吗?我是 C 的新手,正在努力弄清楚。为什么我们最后有 queueNodeT? typedef char queueElementT; typedef struct queueN
场景如下:- 我想反转单链表的方向,换句话说,反转后所有指针现在应该指向后.. 这个算法应该需要线性时间。 我想到的解决方案是使用另一个数据结构 A Stack.. 借助它可以轻松反转单向链表,所有指
在 python 中使用链表最简单的方法是什么?在 scheme 中,链表由 '(1 2 3 4 5) 定义。 Python 的列表 [1, 2, 3, 4, 5] 和元组 (1, 2, 3, 4,
本文首发公众号:小码A梦 一般数据主要存储的形式主要有两种,一种是数组,一种是链表。数组是用来存储固定大小的同类型元素,存储在内存中是 一片连续 的空间。而链表就不同于数组。链表
虽然之前有人问过关于链表与数组的问题,但答案大多归结为我们大多数人在某个时候可能已经学到的东西: 列表擅长插入和删除 数组擅长随机访问 现在像 Bjarne Stroustrup 这样受人尊敬的人有
位置 在堆中,碎片化(每个节点的 malloc) - 在几种不同的方式(缓慢分配,缓慢访问,内存碎片)方面效率低下 在堆中,在一个大块中 - 当需要重新分配 时,数据结构获得的所有灵活性都将丢失 在堆
我完成了泛型的学习,但并不容易。不过,我确实明白了。这是我的理解。我希望您纠正我的错误并回答几个问题:)。 public class LinkedList { //class definition }
我将如何创建一个链接列表来在 OCaml 中保存我的数据?我正在尝试制作一个单链表,但是我遇到了语法问题。我只想制作一个模块来简单地从链表中获取'a,插入'a或删除'a。 有人知道吗? 最佳答案 正如
我在使用这段代码时遇到了问题,我不确定我做错了什么 #include #include #include #include typedef struct flight_struct{
我正在创建一个函数来删除给定列表的最后一个节点(作为参数输入)。该函数本身非常简单,如下所示。 function popBack(list) { var current = list.head
我正在尝试开发一种方法,该方法将在链接列表中的当前节点之前插入传递给它的节点。它有3个条件。对于此实现,不能有任何头节点(仅对列表中第一个节点的引用),并且我无法添加更多变量。 如果列表为空,则将传递
使用 scala,我已将大约 100000 个节点添加到链表中。当我使用函数 length 时,例如 mylist.length。我收到“java.lang.StackOverflowError”错误
所以我正在学习处理链表。我将如何递归地添加节点内的项目。我可以通过执行 sum = h.item +h.next.item+h.next.next.item 添加它们,但这只有在我有小的链接列表时才有
所以我一直在努力理解链表的概念(一直在看一些示例代码,我在互联网上找到了这个。现在如果我能请别人确认我是否正确掌握了一些概念。我将绘制图表,说明我认为每个代码链接的作用。 #include #inc
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是一名优秀的程序员,十分优秀!