【问题标题】:Variables to store node while sorting linked list排序链表时存储节点的变量
【发布时间】:2013-05-13 09:36:11
【问题描述】:

我是一名远程学习的学生,这次我会问社区“去哪里”。 该程序使用链表生成操作。 我理解关于三个指向节点的指针和两个要比较的候选者的想法。同时我没有想到在节点中临时存储信息。 我应该声明节点的结构大小还是使用单独声明的变量?

非常感谢!

/* 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;
}

【问题讨论】:

    标签: c++ list sorting linked-list


    【解决方案1】:

    您应该使用自定义函数来交换列表中的两个节点,而不是复制节点的每个结构成员。

    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;
    }
    

    【讨论】:

    • 太好了,科卡!我认为它会起作用。此外,从 VS 2008 'error C2106: '=' : left operand must be l-value' 复制每个成员上升错误标志。
    猜你喜欢
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多