我试图创建邻接列表来表示文件" input.txt"中边缘列表中的图形。我知道指针如何工作的基础知识,但我有动态结构数组的问题,它由单链表组成。
#include <stdio.h>
#include <stdlib.h>
struct list {
int vertex;
struct list *next;
};
void create_list(int v, struct list* **array);
int main()
{
int i, v = 5;
struct list *ptr, **array = (struct list **)malloc(sizeof(struct list *) * v);
for (i = 0; i < v; i++)
array[i] = NULL;
array[i] = NULL;
create_list(v, &array);
for(i = 0; i < v; i++) {
ptr = array[i];
printf("%d: ", i);
while(ptr != NULL) {
printf(" ->%d", ptr->vertex);
ptr = ptr->next;
}
printf("\n");
}
return 0;
}
void create_list(int v, struct list* **array)
{
int m, n;
struct list *ptr, *tmp;
FILE *luki;
luki = fopen("input.txt", "r");
while(fscanf(luki, "%d %d\n", &m, &n) == 2) {
tmp = (struct lista*)malloc(sizeof(struct list));
tmp->vertex = n;
tmp->next = NULL;
if (*array[m] == NULL) //Here my program crashes when m changes from 0 to 1
*array[m] = tmp;
else {
ptr = array[m];
while(ptr->next != NULL)
ptr = ptr->next;
ptr->next = tmp;
}
}
fclose(luki);
}
你能帮我弄清楚它应该是什么样的吗?
此外,首先我在不使用指向数组的指针的情况下创建了函数:
void create_list(int v, struct list **array)
create_list(v, array);
在调试和(我使用CodeBlocks)时它的效果非常好:
0: 4 ->3 ->1
1: 2
2: 3
3:
4:
但是在正常运行程序时我得到了这个:
0:
1:
2:
3:
4:
为什么调试时的输出是正确的,如果将数组传递给函数create_list是错误的?
答案 0 :(得分:0)
(1)#menuBackground {
background:#5EA5B9;
width:100%;
height:50px;
text-align: center;
}
#menuContainer {
text-align: center;
}
/*Strip the ul of padding and list styling*/
ul {
list-style-type:none;
margin:0;
padding:0;
}
/*Create a horizontal list with spacing*/
li {
display:inline-block;
vertical-align: top;
margin-right:1px;
}
/*Style for menu links*/
li a {
display:block;
min-width:140px;
height:50px;
text-align:center;
line-height:50px;
font-family:Georgia;
color:#fff;
background:#5EA5B9;
text-decoration:none;
font-size: 1rem;
}
/*Hover state for top level links*/
li:hover a {
color: #036;
background:#fff
}
/*Prevent text wrapping*/
li ul li a {
width:auto;
min-width:100px;
padding:0 20px
}
/*Style 'show menu' label button and hide it by default*/
.show-menu {
font-family:Georgia;
text-decoration:none;
color:#fff;
background:#5EA5B9;
text-align:center;
padding:16px 0;
display:none;
width:100%!important
}
/*Hide checkbox*/
input[type=checkbox] {
display:none
}
/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ #menu {
display:block;
margin:0 auto
}
/*Responsive Styles*/
@media screen and (max-width : 760px) {
/*Make dropdown links appear inline*/
ul {
position:static;
display:none;
white-space: initial;
}
/*Create vertical spacing*/
li {
margin-bottom:1px
}
/*Make all menu links full width*/
ul li,li a {
width:100%
}
/*Display 'show menu' link*/
.show-menu {
display:block
}
}
<div id="menuBackground">
<div id="menuContainer">
<label for="show-menu" class="show-menu">Show Menu</label> <input type="checkbox" id="show-menu" role="button" />
<ul id="menu">
<li><a href="index.html">Home</a>
</li>
<li><a href="accommodations.html">Accommodations</a>
</li>
<li><a href="amenities.html">Amenities</a>
</li>
<li><a href="rates.html">Rates</a>
</li>
<li><a href="links.html">Links</a>
</li>
<li><a href="contact.html">Contact</a>
</li>
</ul>
</div>
</div>
(2)void create_list(int v, struct list **array); ... create_list(v, array);
#include <stdio.h>
#include <stdlib.h>
struct list {
int vertex;
struct list *next;
};
void create_list(int v, struct list **array);
int main(void) {
int i, v = 5;
struct list *ptr, **array = (struct list **)malloc(sizeof(struct list *) * v);
for (i = 0; i < v; i++)
array[i] = NULL;
create_list(v, array);
for(i = 0; i < v; i++) {
ptr = array[i];
printf("%d: ", i);
while(ptr != NULL) {
printf("%d", ptr->vertex);
ptr = ptr->next;
if(ptr)
printf(" -> ");
}
printf("\n");
}
return 0;
}
void create_list(int v, struct list **array){
int m, n;
struct list *ptr, *tmp;
FILE *luki;
luki = fopen("input.txt", "r");
while(fscanf(luki, "%d %d\n", &m, &n) == 2) {
tmp = (struct list*)malloc(sizeof(struct list));
tmp->vertex = n;
tmp->next = NULL;
if (array[m] == NULL)
array[m] = tmp;
else {
ptr = array[m];
while(ptr->next != NULL)
ptr = ptr->next;
ptr->next = tmp;
}
}
fclose(luki);
}