在c中字母后插入数字或字母后插入数字

  • 本文关键字:插入 数字 c codeblocks
  • 更新时间 :
  • 英文 :


你好,我有一个任务要做老师指定我们必须用代理列表来做我们的程序所以程序的第一部分必须是表示一个图形我做到了第二部分是用户在数字后输入一个字母或者在字母后输入一个数字使用这个图形表示例如,如果你输入&;4gk2&;输出应该是"错误"。如果你输入"4f5b"输出应该是"正确的"。谁能帮我一下这个程序?我被卡住了。

这是我用C语言表示的图表,谁能完成它?

#include <stdio.h>
#include <stdlib.h>
int no_nodes;
struct node {
int data;
int number;
char lettre;
struct node *next;
};
void readAutomat(struct node *tab[]) {
struct node *newnode;
int k, data;
for (int i = 0; i < no_nodes; i++) {
struct node *last;
printf("How many nodes are adjacent to %d", i);
scanf("%d", &k);
for (int j = 1; j <= k; j++) {
newnode = (struct node *)malloc(sizeof(struct node *));
printf("Enter the %d neighbour of %d :", j, i);
scanf("%d", &data);
newnode->data = data;
newnode->next = NULL;
if (tab[i] == NULL) {
tab[i] = newnode;
} else {
last->next = newnode;
}
last = newnode;
}
}
}
void printAutomat(struct node *tab[]) {
struct node *temp;
for (int i = 0; i < no_nodes; i++) {
temp = tab[i];
printf("Vertices Adjacent to %d are :", i);
while (temp != NULL) {
printf("%dt", temp->data);
temp = temp->next;
}
}
}
int main() {
printf("Enter the numbers of nodes");
scanf("%d", &no_nodes);
struct node *tab[no_nodes];
for (int i = 0; i < no_nodes; i++) {
tab[i] = NULL;
}
readAutomat(tab);
printAutomat(tab);
return 0;
}

这是错误的

newnode=(struct node *)malloc(sizeof(struct node *));

它为指针创建空间而不是为节点结构,你需要

newnode=(struct node *)malloc(sizeof(struct node));

或更习惯地

newnode=(struct node *)malloc(sizeof(*newnode));

甚至更好-根据https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc#:~:text=In%20C%2C%20you%20don't,compiler%2C%20a%20cast%20is%20needed.

newnode=malloc(sizeof(*newnode));

最新更新