为什么我的代码只能打印一个节点值,为什么不能打印其他节点值?
Why my code is able to print only one node value why not other node value?
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *root_1 = NULL;
struct node *root_2 = NULL;
struct node *current;
void create(int option){
struct node *temp,*current;
int data;
printf("Enter data value \n");
scanf("%d",&data);
if(option == 1){
if(root_1 == NULL){
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
// temp->next = Null;
current = temp;
root_1 = temp;
}else{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
current->next = temp;
current = temp;
}
}else{
if(root_2 == NULL){
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
// temp->next = Null;
current = temp;
root_2 = temp;
}else{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
current->next = temp;
current = temp;
}
}
}
void dispaly(int option){
printf("List \n");
struct node *temp;
if(option == 1){
temp = root_1;
while(temp!=NULL){
printf("%d \n",temp->data);
temp = temp->next;
}
}else{
temp = root_2;
while(temp!=NULL){
printf("%d \n",temp->data);
temp = temp->next;
}
}
}
void function(int option){
int ch;
while(1){
printf("1.create,2.display,3.exit \n");
printf("Ur choice \n");
scanf("%d",&ch);
switch(ch){
case 1:
create(option);
break;
case 2:
dispaly(option);
break;
case 3:
exit(0);
default:
printf("Wrong option \n");
}
}
}
void main(){
int option;
while(1){
printf("1.List-1 \t 2.List-2 \n");
printf("enter ur option \n");
scanf("%d",&option);
switch(option){
case 1:
function(option);
break;
case 2:
function(option);
break;
}
}
}
在我的代码中,我试图创建两个链接 lists.in 这个程序我可以创建两个列表,但我无法显示,请任何人帮助 me.two 区分两个列表我正在创建两个根值,它们是 root_1 和 root_2。
// still to do:
// apply the same fix as suggested for the option == 1 code,
// to the option == 2 code
// Note: in comparison statements, like: 'if(option == 1)',
// the literal should be on the left to enable the compiler to catch
// the following error:
// 'if( option = 1)' <-- single '=' rather than '=='
// I.E. use: 'if( 1 == option )'
// IMO: 'georgian' code formatting
// removes the vertical white space
// that is used to greatly enhance the readability of the code
// suggest always putting '{' and '}' on their own line
struct node *root_1 = NULL;
struct node *root_2 = NULL;
struct node *current; // <-- remove this line (or rename the variable)
// as 'current' is one of the auto variables
// in the create() function
void create(int option){
struct node *temp,*current;
int data;
printf("Enter data value \n");
scanf("%d",&data); // <-- change to:
// if( 1 != scanf( " %d", &data ) )
// { // then scanf failed
// perror( "scanf for data failed" );
// // free all malloc'd areas
// // for both linked lists here
// exit( EXIT_FAILURE );
// }
//
// // implied else, scanf successful
//
if(option == 1){
if(root_1 == NULL){
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
// temp->next = Null; // <-- this line should be: 'temp->next = NULL;'
current = temp; // <-- remove this line, it does nothing useful
// attach first node to linked list
root_1 = temp;
}else{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
// <-- insert following line to initialize link field
temp->next = NULL;
current = root1;
// <-- insert following lines
// step through linked list until end
while( NULL != current->next )
{
current = current->next;
} // end while
// attach new node to end of linked list
current->next = temp;
current = temp; // <-- remove this line,
// <-- it will corrupt the linked list
}
}else{
if(root_2 == NULL){
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
// temp->next = Null;
current = temp;
root_2 = temp;
}else{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
current->next = temp;
current = temp;
}
}
}
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *root_1 = NULL;
struct node *root_2 = NULL;
struct node *current;
void create(int option){
struct node *temp,*current;
int data;
printf("Enter data value \n");
scanf("%d",&data);
if(option == 1){
if(root_1 == NULL){
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
// temp->next = Null;
current = temp;
root_1 = temp;
}else{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
current->next = temp;
current = temp;
}
}else{
if(root_2 == NULL){
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
// temp->next = Null;
current = temp;
root_2 = temp;
}else{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
current->next = temp;
current = temp;
}
}
}
void dispaly(int option){
printf("List \n");
struct node *temp;
if(option == 1){
temp = root_1;
while(temp!=NULL){
printf("%d \n",temp->data);
temp = temp->next;
}
}else{
temp = root_2;
while(temp!=NULL){
printf("%d \n",temp->data);
temp = temp->next;
}
}
}
void function(int option){
int ch;
while(1){
printf("1.create,2.display,3.exit \n");
printf("Ur choice \n");
scanf("%d",&ch);
switch(ch){
case 1:
create(option);
break;
case 2:
dispaly(option);
break;
case 3:
exit(0);
default:
printf("Wrong option \n");
}
}
}
void main(){
int option;
while(1){
printf("1.List-1 \t 2.List-2 \n");
printf("enter ur option \n");
scanf("%d",&option);
switch(option){
case 1:
function(option);
break;
case 2:
function(option);
break;
}
}
}
在我的代码中,我试图创建两个链接 lists.in 这个程序我可以创建两个列表,但我无法显示,请任何人帮助 me.two 区分两个列表我正在创建两个根值,它们是 root_1 和 root_2。
// still to do:
// apply the same fix as suggested for the option == 1 code,
// to the option == 2 code
// Note: in comparison statements, like: 'if(option == 1)',
// the literal should be on the left to enable the compiler to catch
// the following error:
// 'if( option = 1)' <-- single '=' rather than '=='
// I.E. use: 'if( 1 == option )'
// IMO: 'georgian' code formatting
// removes the vertical white space
// that is used to greatly enhance the readability of the code
// suggest always putting '{' and '}' on their own line
struct node *root_1 = NULL;
struct node *root_2 = NULL;
struct node *current; // <-- remove this line (or rename the variable)
// as 'current' is one of the auto variables
// in the create() function
void create(int option){
struct node *temp,*current;
int data;
printf("Enter data value \n");
scanf("%d",&data); // <-- change to:
// if( 1 != scanf( " %d", &data ) )
// { // then scanf failed
// perror( "scanf for data failed" );
// // free all malloc'd areas
// // for both linked lists here
// exit( EXIT_FAILURE );
// }
//
// // implied else, scanf successful
//
if(option == 1){
if(root_1 == NULL){
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
// temp->next = Null; // <-- this line should be: 'temp->next = NULL;'
current = temp; // <-- remove this line, it does nothing useful
// attach first node to linked list
root_1 = temp;
}else{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
// <-- insert following line to initialize link field
temp->next = NULL;
current = root1;
// <-- insert following lines
// step through linked list until end
while( NULL != current->next )
{
current = current->next;
} // end while
// attach new node to end of linked list
current->next = temp;
current = temp; // <-- remove this line,
// <-- it will corrupt the linked list
}
}else{
if(root_2 == NULL){
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
// temp->next = Null;
current = temp;
root_2 = temp;
}else{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
current->next = temp;
current = temp;
}
}
}