| |

VerySource

 Forgot password?
 Register
Search
View: 590|Reply: 1

Questions about linked lists, ask for help ~~ urgent ~~

[Copy link]

7

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-1-3 18:40:01
| Show all posts |Read mode
Create two linked lists (A, B), then join them and sort them from low to high
Debugging under VC + 6.0, the LINK function jumps out when running. F11 observes that the HEAD returned by the function twice points incorrectly, but I don't know what is wrong ~
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define NAMELEN 10
#define LEN sizeof (struct data)
typedef
struct data
{
long num;
char name [NAMELEN];
struct data * next;
};
struct data * Crt (void)
{
int count = 1;
struct data * head, * fwd;
head = fwd = (struct data *) malloc (LEN);
head-> num = -1;
do
{
fwd = head-> next = (struct data *) malloc (LEN);
printf ("Stu% -4d\n", count ++);
printf ("Num =>");
scanf ("% ld",&fwd-> num);
printf ("Name =>");
scanf ("% s",&fwd-> name);
}
while (fwd-> num);
return (head);
}
void link (struct data * A_head, struct data * B_head)
{
struct data * fwd = A_head;
fwd = fwd-> next;
while (fwd-> next)
fwd = fwd-> next;
fwd-> next = B_head;
}
void ord (struct data * head)
{
struct data * fwd, * flw, * ord;
ord = fwd = flw = head;
fwd = fwd-> next;
while (ord-> next)
{
if (fwd-> num <ord-> num)
{
flw-> next = fwd-> next;
fwd-> next = ord-> next;
ord = ord-> next = fwd;
}
flw = fwd;
fwd = fwd-> next;
}
}
void print (struct data * fwd)
{
int count = 1;
fwd = fwd-> next;
while (fwd)
{
printf ("Stu% -4d\n", count ++);
printf ("Num% -4d\n", fwd-> num);
printf ("NAME% s\n", fwd-> name);
fwd = fwd-> next;
}
}
void main ()
{
void link (struct data * A_head, struct data * B_head);
    void ord (struct data * head);
    void print (struct data * fwd);
struct data * A_head, * B_head;
printf ("Input infomations of Group A students\n");
A_head = Crt ();
printf ("Input infomations of Group B students\n");
B_head = Crt ();
printf ("Now link Group A and Group B\n");
link (A_head, B_head);
printf ("Finally, order the new Group\n");
ord (A_head);
printf ("Now check the result\n");
print (A_head);
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-10 20:00:01
| Show all posts
head = fwd = (struct data *) malloc (LEN);
head-> num = -1;
"head-> num = -1" is problematic, head-> num is a random value.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list