我必须生成反向链表,这些是我的先决条件
定义结构:
struct node
{
int data;
struct node * link;
}
包含功能
追加---在链表的末尾添加数据。
反向---反转链表。
显示---显示链表中的所有数据。
void append ( struct node **, int ) ;
void display ( struct node * ) ;
void reverse (struct node **);
我收到错误:
1.‘::main’ must return ‘int’
void main()
2.‘strcmp’ was not declared in this scope
z = strcmp(ch,ch1);
我的代码:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
void append(struct node **head);
void reverse(struct node **head);
void display(struct node *p);
void main()
{
struct node *p = NULL;
append(&p);
printf("The elements in the linked list are: ");
display(p);
printf("The elements in the reversed linked list are: ");
reverse(&p);
display(p);
}
void reverse(struct node **head)
{
struct node *p,*q,*r;
p=q=r=*head;
p = p->link->link;
q = q->link;
r->link = NULL;
q->link = r;
while(p != NULL)
{
r = q;
q = p;
p = p->link;
q->link = r;
}
*head = q;
}
void append(struct node **head)
{
int c,z;
char ch[10] = "Yes";
char ch1[10];
struct node *temp,*rear;
do
{
printf("Enter the value:\n");
scanf("%d",&c);
temp = (struct node*)malloc(sizeof(struct node));
temp->data=c;
temp->link=NULL;
if(*head == NULL)
{
*head = temp;
}
else
{
rear->link = temp;
}
rear = temp;
printf("\nDo you want to add another node? Type Yes/No\n");
scanf("%s",ch1);
z = strcmp(ch,ch1);
}
while(z == 0);
printf("\n");
}
void display(struct node *p)
{
while(p != NULL)
{
printf("%d ",p->data);
p=p->link;
}
printf("\n");
}
答案 0 :(得分:1)
将void main()
更改为int main(void)
并添加#include <string.h>
main()
的有效签名是
int main(int argc, char **argv);
int main(void);
和strcmp()
(以及所有str*
函数)在 string.h 中声明。