[英]Stuck at C Linked List
我的问题是我想用一个学生结构创建一个链表。
我尝试了几件事来修复它,但所有尝试都失败了。
它在第 14 行和第 16 行给出了错误,我在 Google 上搜索它并找到了不能解决我的问题的提示。
我是初学者,所以我没有这么好的经验。
现在,我完全被它困住了......
编辑:
给出以下错误
12 1 [警告] 扩展初始值设定项列表仅适用于 -std=c++11 或 -std=gnu++11
14 8 [错误] '*' 标记之前的预期主表达式
14 23 [错误] 'int' 之前的预期主表达式
14 37 [错误] '*' 标记之前的预期主表达式
14 42 [错误] 'insert_bottom' 未在此 scope 中声明
14 43 [错误] 在 ';' 之前需要 '}' 令牌
16 18 [错误] '{' 标记之前的预期 unqualified-id
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct student{
char lastname[30];
char firstname[30];
int id_enrollment;
} Node;
struct node *head
{
node *insert_bottom(int num, node *head);
node *current_node = head;
node *new_node;{
while ( current_node != NULL && current_node->next != NULL);
current_node = current_node->next;
{
new_node = (Node *) malloc(sizeof(Node));
new_node->id_enrollment = num;
new_node->firstname = char;
new_node->lastname = char;
new_node->next= NULL;
if (current_node != NULL)
current_node->next = new_node;
else
head = new_node;
}
return head;
};
print(Node *head) {
Node *current_node = head;
while ( current_node != NULL) {
printf("%d ", current_node->data);
current_node = current_node->next;
}
}
int main()
{
while(1) {
printf("\n ***********************************");
printf("\n * Linked list operations: *");
printf("\n * 1. Show all *");
printf("\n * 2. Add element *");
printf("\n * 3. Quit *");
printf("\n ***********************************\n");
printf("\n Choose an option [1-3] : ");
if (scanf("%d", &option) != 1) {
printf(" *Error: Invalid input. Try again.\n");
scanf("%s", &temp);
continue;
}
switch (option) {
case 1:
struct student p1 = {"David","Brown",1};
struct student p2, p3;
p2.id_enrollment = 2;
strcpy(p2.firstname,"Sam");
strcpy(p2.lastname,"Sam");
p3.id_enrollment = 3;
strcpy(p3.firstname,"Addy");
strcpy(p3.lastname,"Sam");
printf("First Student\n");
printf("id_enrollment : %d\n", p1.id_enrollment);
printf("firstname : %s\n", p1.firstname);
printf("lastname : %s\n", p1.lastname);
printf("Second Student\n");
printf("id_enrollment : %d\n", p2.id_enrollment);
printf("firstname : %s\n", p2.firstname);
printf("lastname : %s\n", p2.lastname);
printf("Third Student\n");
printf("id_enrollment : %d\n", p3.id_enrollment);
printf("firstname : %s\n", p3.firstname);
printf("lastname : %s\n", p3.lastname);
case 2: /* Show all elements */
printf("\nElements in the list: \n [ ");
print(head);
printf("]\n\nPress any key to continue...");
getch();
break;
case 3: /* Exit */
return(0);
break;
return 0;
}
我希望这个解决方案对你来说足够清楚!
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct student
{
char lastname[30];
char firstname[30];
int id_enrollment;
struct student *next;///this pointer is the link between the nodes of the list
} node;
///you have already defined the type 'node' you don't have to write 'struct node *head' anymore
node *insert(char *firstname,char *lastname,int id_enrollment, node *head)
{
node *p=(node*)malloc(sizeof(node));
strcpy(p->firstname,firstname);
strcpy(p->lastname,lastname);
p->id_enrollment=id_enrollment;
p->next=head; ///in this case the elements are going to be shown in a backward order
head=p;
return head;
}
void print(node *head)
{
int i=1;
while ( head != NULL)
{
printf("the first name of the %d student : ",i);
printf("%s\n", head->firstname);
printf("the last name of the %d student : ",i);
printf("%s\n", head->lastname);
printf("the id enrollment of the %d student : ",i);
printf("%d\n", head->id_enrollment);
printf("\n");
head = head->next; i++;
}
}
node * input(node *head)
{
char first[20],last[20];
int id;
printf("insert the first name :");
scanf("%s",first);
printf("insert the last name :");
scanf("%s",last);
printf("insert the id enrellement :");
scanf("%d",&id);
head=insert(first,last,id,head);
return head;
}
int main()
{
node *head=NULL;
int option;
printf("\n ***********************************");
printf("\n * Linked list operations: *");
printf("\n * 1. Show all *");
printf("\n * 2. Add element *");
printf("\n * 3. Quit *");
printf("\n ***********************************\n");
printf("\n Choose an option [1-3] : ");
scanf("%d",&option);
while(option<1 || option>3)
{
printf("Invalid input. Try again : \n");
scanf("%d",&option);
}
head=insert("David","Brown",1,head);
head=insert("Sam","Sam",2,head);
head=insert("Addy","Sam",3,head);
switch (option)
{
case 1: /* Show all elements */
printf("\nElements of the list: \n");
print(head);
break;
case 2: /*add an element*/
/// it's better if you used a function instead of declaring a the pointer each time
///also a label can only be part of a statement and a declaration is not a statement
///so you have just to call the function input() in this case
head=input(head);
case 3: /* Exit */
break;
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.