我是一个遥远的学习学生,这次我会问社区“去哪里”。 该程序使用链表生成操作。 我理解有关节点的三个指针和两个要比较的候选者的想法。与此同时,我没有意识到节点中临时存储信息的想法。 我应该声明节点的结构大小还是使用单独声明的变量?
非常感谢提前!
/* Define libraries to be included */
#include <stdio.h>/*standard input & out library*/
#include <malloc.h>/*allocate memory block*/
#include <string.h>/*defines memory functions to manipulate strings
and arrays*/
#include <ctype.h>/*classifies and transform characters*/
#include <iomanip>
using namespace std;
/*----------------------------------------------------------------------*/
/* define structure
there is no difference btw struct & class*/
typedef struct client {
int number; /*unique account number*/
int balance;/*balance*/
char lastName[20]; /*contains family name*/
char firstName [20];/*contains name*/
char phone[20]; /*contains phone number 10 digits*/
char email[20];
struct client *prev;/* pointer is used to navigate through list
of structures*/
struct client *next;
/*next is used to navigate through structures.*/
} Client;
Client *firstc,*currentc,*newc; /*pointers*/
/* 'firstc' is used to point to first record in list
'currentc' points to current record in list
'newc' contains address of new structure/node
*/
/*-------------------------------------------*/
voidSort()
{
/*temporary pointer to head of list*/
struct client *temphead;
/*variables */
/* stumbling point: I suppose it's not correct*/
int tempnumber; /*unique account number*/
int tempbalance;/*balance*/
char templastName[20]; /*contains family name*/
char tempfirstName [20];/*contains name*/
char tempphone[20]; /*contains phone number 10 digits*/
char tempemail[20];
/*variable to count iterations */
int counter;
if (firstc!==NULL)
/*switch to head of list*/
struct client *temphead = firstc;
while(temphead)
{
/* */
temphead = temphead->next;
counter++;
}
temphead = firstc;
for (int j = 0; j<coiunter; j++)
while(temphead->next)
{
if(temphead->balance > temphead->next->balance)
{
tempbalance = temphead->balance; //
temphead->balance = temphead->next->balance; //
temphead->next->balance = tempbalance;//
tempnumber = temphead->number;
temphead->number = temphead->next->number;
temphead->next->number = tempnumber;
templastName = temphead->lastName
//.......................
}
else
temphead = temphead->next;
}
temphead = firstc;
}
答案 0 :(得分:0)
您应该使用自定义函数来交换列表中的两个节点,而不是复制节点的每个结构成员。
void swap(struct client *a, struct client *b)
{
a->prev->next = b;
b->prev = a->prev;
b->next->prev = a;
a->next = b->next;
a->prev = b;
b->next = a;
}