停留在C链表中

问题描述 投票:0回答:1

我的问题是我想用一个学生结构创建一个链表。

我尝试了几种方法来解决它,但所有尝试都失败了。

它在第14和16行出现错误,我在Google上搜索了它,发现了不能解决我问题的提示。

我是初学者,所以我没有很好的经验。

现在,我完全陷入困境...

编辑:

给出以下错误

12 1 [警告]扩展的初始化程序列表仅在-std = c ++ 11或-std = gnu ++ 11中可用

14 8 [错误]预期在'*'标记之前的主要表达式

14 23 [错误]预期在'int'之前的主表达式

14 37 [错误]预期在'*'标记之前的主表达式

14 42 [错误]在此范围内未声明'insert_bottom'

14 43 [错误]预期在';'之前的'}'令牌

16 18 [错误]在'{'令牌之前的预期不合格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;

}
c struct linked-list singly-linked-list
1个回答
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;

}
© www.soinside.com 2019 - 2024. All rights reserved.