在用户输入时,我很难打印第二个列表。仅打印第一个列表。另外,我不确定如何交换列表值然后打印它们。我的老师希望我们弄清楚如何在不编写函数的情况下做到这一点,但我不明白如何做到这一点。到目前为止,这是我的代码
#include<stdio.h>
int main()
{
int List1[5], List2[5];
int i, j;
printf("Please enter the values to List1 array\n ");
for (i = 0; i < 5; i++)
{
scanf_s("%d", &List1[i]);
}
printf("Please enter the values to List2 array\n");
for (j = 0; j < 5; j++);
{
scanf_s("%d", &List2[j]);
}
printf("List1 and List2 before swap\n");
printf("\nList1\t\t List 2\n");
for (int i = 0; i < 5; i++)
{
printf("%d\n", List1[i]);
}
for (j = 0; j < 5; j++);
{
printf("%d\n", List2[j]);
}
//??? put something here to swap the elements and
printf("\nList1 and List2 after swap");
return 0;
}
答案 0 :(得分:0)
这些不是链表,它们只是int向量,如果要交换两个链表,则代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Structure
{
int n;
};
struct StructureNode
{
struct Structure structure;
struct StructureNode * next;
};
typedef struct StructureNode * Node;
void NewNode(struct Structure s, Node * p )
{
Node temp;
temp = (Node)malloc(sizeof(struct StructureNode));
temp->structure = s;
temp->next = *p;
*p = temp;
}
void Read(struct Structure * p)
{
printf("Insert a number in the list:");
scanf("%d", &p->n);
}
void ViewLists(Node head1, Node head2)
{
printf("List1:\n");
while(head1 != NULL)
{
printf("%d\n", head1->structure.n);
head1 = head1->next;
}
printf("List2:\n");
while(head2 != NULL)
{
printf("%d\n", head2->structure.n);
head2 = head2->next;
}
}
void SwapLists(Node head1, Node head2)
{
struct Structure temp;
while(head1 != NULL && head2 != NULL)
{
temp = head1->structure;
head1->structure = head2->structure;
head2->structure = temp;
head1 = head1->next;
head2 = head2->next;
}
}
int Menu()
{
int c;
printf("\nHow to swap two linked list by St3veR0nix\n"
"\n1) Insert a value in List1\n"
"\n2) Insert a value in List2\n"
"\n3) Swap the lists\n"
"\n4) Print both lists\n"
"\n>>");
scanf("%d", &c);
return c;
}
int main()
{
Node head1 = NULL;
Node head2 = NULL;
struct Structure s;
int c;
do {
c = Menu();
switch(c)
{
case 1: Read(&s);
NewNode(s, &head1);
break;
case 2: Read(&s);
NewNode(s, &head2);
break;
case 3: SwapLists(head1, head2);
break;
case 4: ViewLists(head1, head2);
break;
default: c = 0;
}
} while(c != 0);
}
答案 1 :(得分:0)
您的代码@ RTR81中只有一个明显的错误。您用于for
数组的List2
循环,即j
循环在for
语句的末尾有一个分号。这导致循环甚至在移至第二个迭代之前就结束了。只需删除最后的分号(;),便可以读取整个输入数组。
for (j = 0; j < 5; j++); //this semicolon needs to be removed.
现在进入交换部分。这是一个非常简单的伙伴。声明另一个变量,例如temp
,首先将List1
元素的值放入其中,然后将List2
元素的值放入其中。然后将temp
的值放入List2
元素中。
我在下面发布了代码:
#include<stdio.h>
int main()
{
int List1[5], List2[5];
int i, j, temp;
printf("Please enter the values to List1 array\n ");
for (i = 0; i < 5; i++)
{
scanf("%d", &List1[i]);
}
printf("Please enter the values to List2 array\n");
for (j = 0; j < 5; j++)//removed the semicolon here
{
scanf("%d", &List2[j]);
}
printf("List1 and List2 before swap\n");
printf("\nList 1:");
for (i = 0; i < 5; i++)
{
printf("\t%d", List1[i]);
}
printf("\nList 2:");
for (j = 0; j < 5; j++)//removed the semicolon here
{
printf("\t%d", List2[j]);
}
for(i = 0; i < 5; i++)
{
temp = List1[i];
List1[i] = List2[i];
List2[i] = temp;
}
//??? put something here to swap the elements and
printf("\nList1 and List2 after swap");
printf("\nList 1:");
for (i = 0; i < 5; i++)
{
printf("\t%d", List1[i]);
}
printf("\nList 2:");
for (j = 0; j < 5; j++)//removed the semicolon here
{
printf("\t%d", List2[j]);
}
return 0;
}
这将导致输出如下:
> Please enter the values to List1 array >1 2 3 4 5 >Please enter the values to List2 array >9 8 7 6 5 > >List1 and List2 before swap > > List 1: 1 2 3 4 5 >List 2: 9 8 7 6 5 >List1 and List2 after swap >List 1: 9 8 7 6 5 >List 2: 1 2 3 4 5
希望这对您有帮助!