佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

12
返回列表 发新帖
楼主: Chaisoo333

C语言求救

[复制链接]
 楼主| 发表于 9-4-2014 10:02 PM | 显示全部楼层
geekman 发表于 17-3-2014 07:53 AM
因为这个:那段代码的含义就是:walk through the list till it hit the end of the list. 当来到最后一个 ...

老大 我又有问题了
你看一下 这个是我要做的linked list
我只写好1 insert 和 3 display的罢了
可是我不知道为什么display后就error
可以帮我看一下没有
谢谢
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. struct node
  5. {
  6.         char *student_name;
  7.         char *gender;
  8.         char *course;
  9.         int student_number;
  10.         struct node *next;
  11.         struct node *prev;
  12. }*newnode, *firstPtr,*temp,student;


  13. void insert_new(){       
  14.         char *studentname;
  15.         char *gender;
  16.         char *course;
  17.         int studentnumber;
  18.         printf("Enter the name of the student\n");
  19.         scanf("%s",&studentname);
  20.         printf("Enter the gender of the student\n");
  21.         scanf("%s",&gender);
  22.         printf("Enter the course of the student\n");
  23.         scanf("%s",&course);
  24.         printf("Enter the number of the student\n");
  25.         scanf("%d",&studentnumber);

  26.         newnode = (struct node*)malloc(sizeof (struct node));
  27.         newnode->student_name = studentname;
  28.         newnode->gender = gender;
  29.         newnode->course = course;
  30.         newnode->student_number = studentnumber;
  31.         newnode->next = NULL;

  32.         if (firstPtr == NULL)
  33.         {
  34.                 firstPtr = newnode;
  35.         }
  36.         else
  37.         {

  38.                 temp = firstPtr;
  39.                 while (temp->next != NULL)
  40.                 {
  41.                         temp = temp->next;
  42.                 }
  43.                 temp->next = newnode;
  44.                 firstPtr = temp;
  45.         }
  46.         printf("Insert success\n");
  47.         printf("Press any key to go back main menu\n");
  48.         getch();
  49. }

  50. void insert_last(){

  51. }

  52. void print_begin(){
  53.         if (firstPtr == NULL)
  54.                 printf("List is empty\n");
  55.         else
  56.         {
  57.                 struct node *tempdisplay;
  58.                 tempdisplay = firstPtr;

  59.                 while (tempdisplay != NULL)
  60.                 {
  61.                         printf("%s\n", tempdisplay->student_name);
  62.                         printf("%s\n", tempdisplay->gender);
  63.                         printf("%s\n", tempdisplay->course);
  64.                         printf("%d\n", tempdisplay->student_number);
  65.                         tempdisplay = tempdisplay->next;
  66.                 }
  67.         }
  68.         printf("Press any key to go back main menu\n");
  69.         getch();
  70. }

  71. void print_last(){

  72. }

  73. void remove_student(){

  74. }

  75. main()
  76. {
  77.         firstPtr=NULL;
  78.         int main_choice;
  79.         do{
  80.                 printf("\n");
  81.                 printf("\n");
  82.                 printf("\n");
  83.                 printf("               1.Add a new student to the beginning\n");
  84.                 printf("               2.Add a new student to the end\n");
  85.                 printf("               3.Print out the entire list from the beginning\n");
  86.                 printf("               4.Print out the entire list from the end\n");
  87.                 printf("               5.Remove a student from the list\n");
  88.                 printf("               6.Quit the program\n");
  89.                 printf("\n\nEnter the number of the options\n");
  90.                 scanf("%d", &main_choice);

  91.                 switch (main_choice){

  92.                 case 1: insert_new(); break;
  93.                 case 2: insert_last(); break;
  94.                 case 3: print_begin(); break;
  95.                 case 4: print_last(); break;
  96.                 case 5: remove_student(); break;
  97.                 case 6:{
  98.                                    printf("\n\n\nThe program is closing...\n...\n");
  99.                                    return 0;
  100.                                    break;
  101.                 }
  102.                 }
  103.                 system("cls");
  104.         } while (main_choice != 6);
  105. }
复制代码
回复

使用道具 举报


ADVERTISEMENT

发表于 9-4-2014 10:09 PM | 显示全部楼层
  1. char *studentname;
  2.         char *gender;
  3.         char *course;
  4.         int studentnumber;
  5.         printf("Enter the name of the student\n");
  6.         scanf("%s",&studentname);
  7.         printf("Enter the gender of the student\n");
  8.         scanf("%s",&gender);
  9.         printf("Enter the course of the student\n");
  10.         scanf("%s",&course);
  11.         printf("Enter the number of the student\n");
  12.         scanf("%d",&studentnumber);
复制代码
谁教你可以这样做的?
回复

使用道具 举报

 楼主| 发表于 9-4-2014 10:52 PM | 显示全部楼层
geekman 发表于 9-4-2014 10:09 PM
谁教你可以这样做的?

跟着我学校的exmple改的咯==为什么?哪里错
回复

使用道具 举报

发表于 10-4-2014 08:46 AM | 显示全部楼层
那些只是Pointer,并没有实际储存的空间。
回复

使用道具 举报

 楼主| 发表于 11-4-2014 06:56 AM | 显示全部楼层
geekman 发表于 10-4-2014 08:46 AM
那些只是Pointer,并没有实际储存的空间。

我改了一点你说得东西。。。可是我的diplayfunction还是不可以T_T
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. struct node
  5. {
  6.         char *student_name;
  7.         char *gender;
  8.         char *course;
  9.         int *student_number;
  10.         struct node *next;
  11.         struct node *prev;
  12. }*newnode, *firstPtr,*temp,student;


  13. void insert_new(char *name, char *gender, char *course, int *number){


  14.         newnode = (struct node*)malloc(sizeof (struct node));
  15.         newnode->student_name = name;
  16.         newnode->gender = gender;
  17.         newnode->course = course;
  18.         newnode->student_number = number;
  19.         newnode->next = NULL;

  20.         if (firstPtr == NULL)
  21.         {
  22.                 firstPtr = newnode;
  23.         }
  24.         else
  25.         {

  26.                 temp = firstPtr;
  27.                 while (temp->next != NULL)
  28.                 {
  29.                         temp = temp->next;
  30.                 }
  31.                 temp->next = newnode;
  32.                 firstPtr = temp;
  33.         }
  34.         printf("Insert success\n");
  35.         printf("Press any key to go back main menu\n");
  36.         getch();
  37. }

  38. void insert_last(){

  39. }

  40. void print_begin(){
  41.         if (firstPtr == NULL)
  42.                 printf("List is empty\n");
  43.         else
  44.         {
  45.                 struct node *tempdisplay;
  46.                 tempdisplay = firstPtr;

  47.                 while (tempdisplay != NULL)
  48.                 {
  49.                         printf("%s\n", tempdisplay->student_name);
  50.                         printf("%s\n", tempdisplay->gender);
  51.                         printf("%s\n", tempdisplay->course);
  52.                         printf("%d\n", tempdisplay->student_number);
  53.                         tempdisplay = tempdisplay->next;
  54.                 }
  55.         }
  56.         printf("Press any key to go back main menu\n");
  57.         getch();
  58. }

  59. void print_last(){

  60. }

  61. void remove_student(){

  62. }

  63. main()
  64. {
  65.         firstPtr=NULL;
  66.         int main_choice;
  67.         do{
  68.                 printf("\n");
  69.                 printf("\n");
  70.                 printf("\n");
  71.                 printf("               1.Add a new student to the beginning\n");
  72.                 printf("               2.Add a new student to the end\n");
  73.                 printf("               3.Print out the entire list from the beginning\n");
  74.                 printf("               4.Print out the entire list from the end\n");
  75.                 printf("               5.Remove a student from the list\n");
  76.                 printf("               6.Quit the program\n");
  77.                 printf("\n\nEnter the number of the options\n");
  78.                 scanf("%d", &main_choice);

  79.                 switch (main_choice){

  80.                 case 1: {
  81.                                         char name;
  82.                                         char gender;
  83.                                         char course;
  84.                                         int number;
  85.                                         printf("Enter the name of the student\n");
  86.                                         scanf("%s", &name);
  87.                                         printf("Enter the gender of the student\n");
  88.                                         scanf("%s", &gender);
  89.                                         printf("Enter the course of the student\n");
  90.                                         scanf("%s", &course);
  91.                                         printf("Enter the number of the student\n");
  92.                                         scanf("%d", &number);
  93.                                         insert_new(name,gender,course,number);
  94.                 }break;
  95.                 case 2: insert_last(); break;
  96.                 case 3: print_begin(); break;
  97.                 case 4: print_last(); break;
  98.                 case 5: remove_student(); break;
  99.                 case 6:{
  100.                                    printf("\n\n\nThe program is closing...\n...\n");
  101.                                    return 0;
  102.                                    break;
  103.                 }
  104.                 }
  105.                 system("cls");
  106.         } while (main_choice != 6);
  107. }
复制代码
回复

使用道具 举报

发表于 13-4-2014 11:16 AM | 显示全部楼层
Chaisoo333 发表于 11-4-2014 06:56 AM
我改了一点你说得东西。。。可是我的diplayfunction还是不可以T_T

请描述症状。我没可能自行去猜测什么东西是你所说的 “不可以”。

就算是去看医生,医生第一句也是问你:“身体什么地方有病痛,怎样的不舒服?”,而不是你一进门就埋头地跟你抽血验尿,安排收费昂贵的X光,磁振扫描,HIV检疫等等折腾十几个钟头后才告诉你:“根据所有的检测结果,我初步怀疑,你可能,或许,70%的概率,并没有病,只是有点睡眠不足。”

回复

使用道具 举报

Follow Us
 楼主| 发表于 13-4-2014 05:19 PM | 显示全部楼层
geekman 发表于 13-4-2014 11:16 AM
请描述症状。我没可能自行去猜测什么东西是你所说的 “不可以”。

就算是去看医生,医生第一句也是问你 ...

Ermmm 就是我的第三个function 要read data ,but 我的program read出来的是乱码
我觉得是我的这段有问题。。。是不是不可以这样assign value的?我应该要怎样assign才对?
  1. void insert_new(char name[], char xgender[], char xcourse[], int number){
  2.         newnode = NULL;
  3.         newnode = (struct node*)malloc(sizeof (struct node));
  4.         newnode->student_name[40] = name;
  5.         newnode->gender[40] = xgender;
  6.         newnode->course[40] = xcourse;
  7.         newnode->student_number = number;
  8.         newnode->next = NULL;
复制代码
回复

使用道具 举报

 楼主| 发表于 13-4-2014 05:45 PM | 显示全部楼层
Ok....那段我用strcpyfunction 解决了
只是我也得我的用pointer的方法怪怪的
你觉得哪里可以改进吗
把我的直接把
  1. printf("Enter the name of the student\n");
  2.                                         scanf("%s", name);
  3.                                         printf("Enter the gender of the student\n");
  4.                                         scanf("%s", gender);
  5.                                         printf("Enter the course of the student\n");
  6.                                         scanf("%s", course);
  7.                                         printf("Enter the number of the student\n");
  8.                                         scanf("%d", &number);
复制代码
这段放进insert_new()吗?
那我就不用放insert_new(char,char这些了)
有差吗 ???

还有为什么我fgets(name,100,stdin); 这个不会走的?
我expect output会printf("Enter the name of the student\n";
然后fgets(name,100,stdin); 给user输入
可是他直接跳到printf("Enter the gender of the student\n";这边
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. struct node
  5. {
  6.         char student_name[40];
  7.         char gender[40];
  8.         char course[40];
  9.         int student_number;
  10.         struct node *next;
  11.         struct node *prev;
  12. }*newnode, *firstPtr,*temp,student;

  13. void insert_last(){

  14. }

  15. void print_begin(){

  16.         struct node *tempdisplay;
  17.         tempdisplay = firstPtr;

  18.         if (firstPtr == NULL)
  19.                 printf("List is empty\n");
  20.         else
  21.         {
  22.                 while (tempdisplay!= NULL)
  23.                 {
  24.                         printf("%s", tempdisplay->student_name);
  25.                         printf("%s", tempdisplay->gender);
  26.                         printf("%s", tempdisplay->course);
  27.                         printf("%d\n", tempdisplay->student_number);
  28.                         tempdisplay = tempdisplay->next;
  29.                 }
  30.         }
  31.         printf("\n\nPress any key to go back main menu\n");
  32.         getch();
  33. }

  34. void print_last(){

  35. }

  36. void remove_student(){

  37. }

  38. void insert_new(char name[], char xgender[], char xcourse[], int number){
  39.         newnode = NULL;
  40.         newnode = (struct node*)malloc(sizeof (struct node));
  41.         strcpy(newnode->student_name,name);
  42.         strcpy(newnode->gender, xgender);
  43.         strcpy(newnode->course, xcourse);
  44.         newnode->student_number = number;
  45.         newnode->next = NULL;

  46.         if (firstPtr == NULL)
  47.         {
  48.                 firstPtr = newnode;
  49.         }
  50.         else
  51.         {
  52.                 while (firstPtr->next != NULL)
  53.                 {
  54.                         firstPtr = firstPtr->next;
  55.                 }
  56.                 firstPtr->next = newnode;
  57.         }

  58.         printf("Insert success\n");
  59.         printf("Press any key to go back main menu\n");
  60.         getch();
  61. }

  62. main()
  63. {
  64.         firstPtr=NULL;
  65.         char name[40];
  66.         char gender[40];
  67.         char course[40];
  68.         int number;
  69.         int main_choice;

  70.         do{
  71.                 printf("\n");
  72.                 printf("\n");
  73.                 printf("\n");
  74.                 printf("               1.Add a new student to the beginning\n");
  75.                 printf("               2.Add a new student to the end\n");
  76.                 printf("               3.Print out the entire list from the beginning\n");
  77.                 printf("               4.Print out the entire list from the end\n");
  78.                 printf("               5.Remove a student from the list\n");
  79.                 printf("               6.Quit the program\n");
  80.                 printf("\n\nEnter the number of the options\n");
  81.                 scanf("%d", &main_choice);

  82.                 switch (main_choice){

  83.                 case 1: {
  84.                                         printf("Enter the name of the student\n");
  85.                                         fgets(name,100,stdin);
  86.                                         printf("Enter the gender of the student\n");
  87.                                         fgets(gender,100,stdin);
  88.                                         printf("Enter the course of the student\n");
  89.                                         fgets(course,100,stdin);
  90.                                         printf("Enter the number of the student\n");
  91.                                         scanf("%d", &number);

  92.                                         insert_new(name,gender,course,number);
  93.                 }break;
  94.                 case 2: insert_last(); break;
  95.                 case 3: print_begin(); break;
  96.                 case 4: print_last(); break;
  97.                 case 5: remove_student(); break;
  98.                 case 6:{
  99.                                    printf("\n\n\nThe program is closing...\n...\n");
  100.                                    return 0;
  101.                                    break;
  102.                 }
  103.                 }
  104.                 system("cls");
  105.         } while (main_choice != 6);
  106. }
复制代码
本帖最后由 Chaisoo333 于 13-4-2014 06:20 PM 编辑

回复

使用道具 举报


ADVERTISEMENT

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 15-8-2025 03:14 AM , Processed in 0.101750 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表