我首先说我是C的新手,现在,我很难解决一些非直观的错误。我已经尝试了很长一段时间才达成某种解决方案,但我总是走向死胡同。
我正在尝试构建一些用于通过动态链接列表插入和显示图形的函数。在编译时一切正常,但元素似乎没有很好地显示。实际上,就像下图中一样,只显示节点的第一个元素。
所以问题是导致这些错误和警告的原因以及如何删除它们?
如果你看看下面的代码,你会看到它有一些警告(我不知道它们出现的原因 - 我在Ubuntu中使用GNU编译器中的代码块)以及显示的问题图的元素。问题很可能出在display_graph函数中,但我无法实现。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct AdjListNode {
int dest;
struct LIST_NODE *next;
} LIST_NODE;
typedef struct AdjList {
struct LIST_NODE *head;
} ADJACENCY_LIST;
LIST_NODE *create_node(int dest) {
LIST_NODE *nod;
if(dest<0) exit(0);
nod = (LIST_NODE*)malloc(sizeof(LIST_NODE));
if(nod==NULL) {
printf("Problems at memory allocation!");
exit(0);
}
nod->dest = dest;
nod->next = NULL;
return (LIST_NODE*)nod;
}
void display_graph(ADJACENCY_LIST *v) {
int s, i;
LIST_NODE *nod;
s = sizeof(v);
for(i=0;i<=s;i++) {
nod = v[i].head;
//citeste lista cu head in primul nod
while(nod!=NULL) {
printf("Data from node: %d \n", nod->dest);
nod = nod->next;
}
}
}
int main()
{
int n; //number of graph nodes
int i; //just a counter
int dest; dest = -1; //it's actually the "name" of the nodes. They must all be positive so I started negative
char c;
ADJACENCY_LIST *t;
printf("The number of nodes of the graph: ");
scanf("%d", &n);
t = (ADJACENCY_LIST*)malloc(n*sizeof(ADJACENCY_LIST));
/* We make a loop for the nodes and each node has a while thru which I make the links */
for(i=0;i<n;i++) {
c = 'D'; // Initializing
printf("Specify the links of the node %d with the others:\n", i);
int contor; contor = 0;
while(c=='D') {
LIST_NODE *nod;
printf("The link with node: ");
scanf("%d%*c", &dest);
if(dest>=0){
nod = create_node(dest);
if(contor==0) t[i].head = (LIST_NODE*)nod; // just make the first node a head node
} else nod = NULL;
//verificam daca vrem sa continuam
printf("Do you want to link any other node to %d?(D to add, anything else STOP\n)", i);
c = getchar();
contor++; //increment counter
}
// inchidem lista
}
display_graph(t);
return 0;
}
任何帮助将不胜感激!
修改 正如Christhofe(确认问题)和Abhishek Vasisht所指出的那样,向量v的大小实际上是指针的大小。
但是,仍有一些警告,我不知道为什么它们仍然出现......都是
|| === Build:Grafuri1中的Debug(编译器:GNU GCC编译器)=== | /home/marianpc/Anul_1/SDA/Grafuri1/main.c||在函数'display_graph'中: /home/marianpc/Anul_1/SDA/Grafuri1/main.c|33 |warning:从不兼容的指针类型分配 /home/marianpc/Anul_1/SDA/Grafuri1/main.c|38 |警告:从不兼容的指针类型分配 /home/marianpc/Anul_1/SDA/Grafuri1/main.c|28 |警告:未使用的变量's'[-Wunused-variable] | /home/marianpc/Anul_1/SDA/Grafuri1/main.c||函数'main':| /home/marianpc/Anul_1/SDA/Grafuri1/main.c|71|warning:从不兼容的指针类型分配 /home/marianpc/Anul_1/SDA/Grafuri1/main.c|76 |warning:从不兼容的指针类型中分配 || ===构建完成:0个错误,5个警告(0分钟,0秒(秒))=== |
主要的是该程序现在正在运行。非常感谢!真有帮助!!
答案 0 :(得分:2)
试试这个
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct AdjListNode {
int dest;
struct LIST_NODE *next;
} LIST_NODE;
typedef struct AdjList {
struct LIST_NODE *head;
} ADJACENCY_LIST;
LIST_NODE *create_node(int dest) {
LIST_NODE *nod;
if (dest < 0) exit(0);
nod = (LIST_NODE*)malloc(sizeof(LIST_NODE));
if (nod == NULL) {
printf("Problems at memory allocation!");
exit(0);
}
nod->dest = dest;
nod->next = NULL;
return (LIST_NODE*)nod;
}
void display_graph(ADJACENCY_LIST *v,int values) {
int s, i;
LIST_NODE *nod;
for (i = 0; i < values; ++i)
{
nod = v[i].head;
printf("Data from node: %d \n", i);
while (nod != NULL)
{
printf("Data : %d \n", nod->dest);
nod = nod->next;
}
}
}
int main()
{
int n; //number of graph nodes
int i; //just a counter
int dest; dest = -1; //it's actually the "name" of the nodes. They must all be positive so I started negative
char* c = (char*)(malloc(sizeof(char)));
ADJACENCY_LIST *t;
LIST_NODE *last_added;
printf("The number of nodes of the graph: ");
scanf("%d", &n);
t = (ADJACENCY_LIST*)calloc(n,sizeof(ADJACENCY_LIST));
/* We make a loop for the nodes and each node has a while thru which I make the links */
for (i = 0; i < n; i++) {
//c = 'D'; // Initializing
printf("Specify the links of the node %d with the others:\n", i);
int contor; contor = 0;
do {
LIST_NODE *nod;
printf("The link with node: ");
scanf("%d", &dest);
if (dest >= 0) {
nod = create_node(dest);
if (contor == 0)
{
t[i].head = (LIST_NODE*)nod; // just make the first node a head node
last_added = nod;
}
else
{
last_added->next = nod;
last_added = nod;
}
}
//verificam daca vrem sa continuam
printf("Do you want to link any other node to %d?(D to add, anything else STOP\n)", i);
fflush(stdin);
*c = getchar();
contor++; //increment counter
} while (*c == 'D');
}
display_graph(t,n);
return 0;
}
答案 1 :(得分:0)
以下代码编译干净,有效。
它无法为第一级指针列表中的每个条目提供节点列表。
您可以轻松添加该功能。
您还需要添加将每个malloc节点传递给free()的功能 特别是发生错误时
private void Form1_Load(object sender, EventArgs e)
{
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Click += new System.EventHandler(this.button1_Click);
this.button3.Click += new System.EventHandler(this.button1_Click);
// OR
this.button1.Click += (s, a) => ShowMessageBox("Test1");
this.button2.Click += (s, a) => ShowMessageBox("Test2");
this.button3.Click += (s, a) => ShowMessageBox("Test3");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello Event World");
}
private void ShowMessageBox(string message)
{
MessageBox.Show(message);
}