|

楼主 |
发表于 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
可以帮我看一下没有
谢谢- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct node
- {
- char *student_name;
- char *gender;
- char *course;
- int student_number;
- struct node *next;
- struct node *prev;
- }*newnode, *firstPtr,*temp,student;
- void insert_new(){
- char *studentname;
- char *gender;
- char *course;
- int studentnumber;
- printf("Enter the name of the student\n");
- scanf("%s",&studentname);
- printf("Enter the gender of the student\n");
- scanf("%s",&gender);
- printf("Enter the course of the student\n");
- scanf("%s",&course);
- printf("Enter the number of the student\n");
- scanf("%d",&studentnumber);
- newnode = (struct node*)malloc(sizeof (struct node));
- newnode->student_name = studentname;
- newnode->gender = gender;
- newnode->course = course;
- newnode->student_number = studentnumber;
- newnode->next = NULL;
- if (firstPtr == NULL)
- {
- firstPtr = newnode;
- }
- else
- {
- temp = firstPtr;
- while (temp->next != NULL)
- {
- temp = temp->next;
- }
- temp->next = newnode;
- firstPtr = temp;
- }
- printf("Insert success\n");
- printf("Press any key to go back main menu\n");
- getch();
- }
- void insert_last(){
- }
- void print_begin(){
- if (firstPtr == NULL)
- printf("List is empty\n");
- else
- {
- struct node *tempdisplay;
- tempdisplay = firstPtr;
- while (tempdisplay != NULL)
- {
- printf("%s\n", tempdisplay->student_name);
- printf("%s\n", tempdisplay->gender);
- printf("%s\n", tempdisplay->course);
- printf("%d\n", tempdisplay->student_number);
- tempdisplay = tempdisplay->next;
- }
- }
- printf("Press any key to go back main menu\n");
- getch();
- }
- void print_last(){
- }
- void remove_student(){
- }
- main()
- {
- firstPtr=NULL;
- int main_choice;
- do{
- printf("\n");
- printf("\n");
- printf("\n");
- printf(" 1.Add a new student to the beginning\n");
- printf(" 2.Add a new student to the end\n");
- printf(" 3.Print out the entire list from the beginning\n");
- printf(" 4.Print out the entire list from the end\n");
- printf(" 5.Remove a student from the list\n");
- printf(" 6.Quit the program\n");
- printf("\n\nEnter the number of the options\n");
- scanf("%d", &main_choice);
- switch (main_choice){
- case 1: insert_new(); break;
- case 2: insert_last(); break;
- case 3: print_begin(); break;
- case 4: print_last(); break;
- case 5: remove_student(); break;
- case 6:{
- printf("\n\n\nThe program is closing...\n...\n");
- return 0;
- break;
- }
- }
- system("cls");
- } while (main_choice != 6);
- }
复制代码 |
|
|
|
|
|
|
|
发表于 9-4-2014 10:09 PM
|
显示全部楼层
- char *studentname;
- char *gender;
- char *course;
- int studentnumber;
- printf("Enter the name of the student\n");
- scanf("%s",&studentname);
- printf("Enter the gender of the student\n");
- scanf("%s",&gender);
- printf("Enter the course of the student\n");
- scanf("%s",&course);
- printf("Enter the number of the student\n");
- scanf("%d",&studentnumber);
复制代码 谁教你可以这样做的?.gif) |
|
|
|
|
|
|
|

楼主 |
发表于 9-4-2014 10:52 PM
|
显示全部楼层
geekman 发表于 9-4-2014 10:09 PM 
谁教你可以这样做的?
跟着我学校的exmple改的咯==为什么?哪里错 |
|
|
|
|
|
|
|
发表于 10-4-2014 08:46 AM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 11-4-2014 06:56 AM
|
显示全部楼层
geekman 发表于 10-4-2014 08:46 AM 
那些只是Pointer,并没有实际储存的空间。
我改了一点你说得东西。。。可是我的diplayfunction还是不可以T_T- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct node
- {
- char *student_name;
- char *gender;
- char *course;
- int *student_number;
- struct node *next;
- struct node *prev;
- }*newnode, *firstPtr,*temp,student;
- void insert_new(char *name, char *gender, char *course, int *number){
- newnode = (struct node*)malloc(sizeof (struct node));
- newnode->student_name = name;
- newnode->gender = gender;
- newnode->course = course;
- newnode->student_number = number;
- newnode->next = NULL;
- if (firstPtr == NULL)
- {
- firstPtr = newnode;
- }
- else
- {
- temp = firstPtr;
- while (temp->next != NULL)
- {
- temp = temp->next;
- }
- temp->next = newnode;
- firstPtr = temp;
- }
- printf("Insert success\n");
- printf("Press any key to go back main menu\n");
- getch();
- }
- void insert_last(){
- }
- void print_begin(){
- if (firstPtr == NULL)
- printf("List is empty\n");
- else
- {
- struct node *tempdisplay;
- tempdisplay = firstPtr;
- while (tempdisplay != NULL)
- {
- printf("%s\n", tempdisplay->student_name);
- printf("%s\n", tempdisplay->gender);
- printf("%s\n", tempdisplay->course);
- printf("%d\n", tempdisplay->student_number);
- tempdisplay = tempdisplay->next;
- }
- }
- printf("Press any key to go back main menu\n");
- getch();
- }
- void print_last(){
- }
- void remove_student(){
- }
- main()
- {
- firstPtr=NULL;
- int main_choice;
- do{
- printf("\n");
- printf("\n");
- printf("\n");
- printf(" 1.Add a new student to the beginning\n");
- printf(" 2.Add a new student to the end\n");
- printf(" 3.Print out the entire list from the beginning\n");
- printf(" 4.Print out the entire list from the end\n");
- printf(" 5.Remove a student from the list\n");
- printf(" 6.Quit the program\n");
- printf("\n\nEnter the number of the options\n");
- scanf("%d", &main_choice);
- switch (main_choice){
- case 1: {
- char name;
- char gender;
- char course;
- int number;
- printf("Enter the name of the student\n");
- scanf("%s", &name);
- printf("Enter the gender of the student\n");
- scanf("%s", &gender);
- printf("Enter the course of the student\n");
- scanf("%s", &course);
- printf("Enter the number of the student\n");
- scanf("%d", &number);
- insert_new(name,gender,course,number);
- }break;
- case 2: insert_last(); break;
- case 3: print_begin(); break;
- case 4: print_last(); break;
- case 5: remove_student(); break;
- case 6:{
- printf("\n\n\nThe program is closing...\n...\n");
- return 0;
- break;
- }
- }
- system("cls");
- } while (main_choice != 6);
- }
复制代码 |
|
|
|
|
|
|
|
发表于 13-4-2014 11:16 AM
|
显示全部楼层
Chaisoo333 发表于 11-4-2014 06:56 AM 
我改了一点你说得东西。。。可是我的diplayfunction还是不可以T_T
请描述症状。我没可能自行去猜测什么东西是你所说的 “不可以”。
就算是去看医生,医生第一句也是问你:“身体什么地方有病痛,怎样的不舒服?”,而不是你一进门就埋头地跟你抽血验尿,安排收费昂贵的X光,磁振扫描,HIV检疫等等折腾十几个钟头后才告诉你:“根据所有的检测结果,我初步怀疑,你可能,或许,70%的概率,并没有病,只是有点睡眠不足。”
|
|
|
|
|
|
|
|

楼主 |
发表于 13-4-2014 05:19 PM
|
显示全部楼层
geekman 发表于 13-4-2014 11:16 AM 
请描述症状。我没可能自行去猜测什么东西是你所说的 “不可以”。
就算是去看医生,医生第一句也是问你 ...
Ermmm 就是我的第三个function 要read data ,but 我的program read出来的是乱码
我觉得是我的这段有问题。。。是不是不可以这样assign value的?我应该要怎样assign才对?- void insert_new(char name[], char xgender[], char xcourse[], int number){
- newnode = NULL;
- newnode = (struct node*)malloc(sizeof (struct node));
- newnode->student_name[40] = name;
- newnode->gender[40] = xgender;
- newnode->course[40] = xcourse;
- newnode->student_number = number;
- newnode->next = NULL;
复制代码 |
|
|
|
|
|
|
|

楼主 |
发表于 13-4-2014 05:45 PM
|
显示全部楼层
Ok....那段我用strcpyfunction 解决了
只是我也得我的用pointer的方法怪怪的
你觉得哪里可以改进吗
把我的直接把- printf("Enter the name of the student\n");
- scanf("%s", name);
- printf("Enter the gender of the student\n");
- scanf("%s", gender);
- printf("Enter the course of the student\n");
- scanf("%s", course);
- printf("Enter the number of the student\n");
- 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" ;这边- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct node
- {
- char student_name[40];
- char gender[40];
- char course[40];
- int student_number;
- struct node *next;
- struct node *prev;
- }*newnode, *firstPtr,*temp,student;
- void insert_last(){
- }
- void print_begin(){
- struct node *tempdisplay;
- tempdisplay = firstPtr;
- if (firstPtr == NULL)
- printf("List is empty\n");
- else
- {
- while (tempdisplay!= NULL)
- {
- printf("%s", tempdisplay->student_name);
- printf("%s", tempdisplay->gender);
- printf("%s", tempdisplay->course);
- printf("%d\n", tempdisplay->student_number);
- tempdisplay = tempdisplay->next;
- }
- }
- printf("\n\nPress any key to go back main menu\n");
- getch();
- }
- void print_last(){
- }
- void remove_student(){
- }
- void insert_new(char name[], char xgender[], char xcourse[], int number){
- newnode = NULL;
- newnode = (struct node*)malloc(sizeof (struct node));
- strcpy(newnode->student_name,name);
- strcpy(newnode->gender, xgender);
- strcpy(newnode->course, xcourse);
- newnode->student_number = number;
- newnode->next = NULL;
- if (firstPtr == NULL)
- {
- firstPtr = newnode;
- }
- else
- {
- while (firstPtr->next != NULL)
- {
- firstPtr = firstPtr->next;
- }
- firstPtr->next = newnode;
- }
- printf("Insert success\n");
- printf("Press any key to go back main menu\n");
- getch();
- }
- main()
- {
- firstPtr=NULL;
- char name[40];
- char gender[40];
- char course[40];
- int number;
- int main_choice;
- do{
- printf("\n");
- printf("\n");
- printf("\n");
- printf(" 1.Add a new student to the beginning\n");
- printf(" 2.Add a new student to the end\n");
- printf(" 3.Print out the entire list from the beginning\n");
- printf(" 4.Print out the entire list from the end\n");
- printf(" 5.Remove a student from the list\n");
- printf(" 6.Quit the program\n");
- printf("\n\nEnter the number of the options\n");
- scanf("%d", &main_choice);
- switch (main_choice){
- case 1: {
- printf("Enter the name of the student\n");
- fgets(name,100,stdin);
- printf("Enter the gender of the student\n");
- fgets(gender,100,stdin);
- printf("Enter the course of the student\n");
- fgets(course,100,stdin);
- printf("Enter the number of the student\n");
- scanf("%d", &number);
- insert_new(name,gender,course,number);
- }break;
- case 2: insert_last(); break;
- case 3: print_begin(); break;
- case 4: print_last(); break;
- case 5: remove_student(); break;
- case 6:{
- printf("\n\n\nThe program is closing...\n...\n");
- return 0;
- break;
- }
- }
- system("cls");
- } while (main_choice != 6);
- }
复制代码 本帖最后由 Chaisoo333 于 13-4-2014 06:20 PM 编辑
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|