English 中文(简体)
C 与结构有密切联系的清单
原标题:C Doubly linked list with structure

我做一个双重链接的清单。 我知道,它正在发挥作用,但来到这里是为了确保和看到我是否正确行事。

在另一方面,当我提出这个问题时,我还谈到与双重链接清单无关的其他问题,而是与C文档之间的结构和“可见性”。 如果你理解,我应向这另外两个疑问提出其他问题,请说明。 否则,我会感到自由。

我的档案1.c 我有:

http://www.ohchr.org。

#include <stdio.h>
#include <stdlib.h>

typedef struct team{
    char *name;
    char *teamPlace;
}Team;

typedef struct nodeTeam{
    int numberOfTeams;
    Team team;
    struct nodeTeam *next;
    struct nodeTeam *prev;
}NodeTeam;

int createsListOfTeams(NodeTeam **head, NodeTeam **tail);
void printListOfTeams(NodeTeam *listofTeams);
int addNodeTeamsSorted(NodeTeam *head, NodeTeam **tail, Team team);

int main()
{
    NodeTeam *headEquipas,*tailEquipas;
    Team eq;
    /*Creates the doubly linked list*/
    if(createsListOfTeams(&headEquipas,&tailEquipas)){
        printf("
Error
");
        return 0;
    }
    /*Add the teams to the doubly linked list. At the end, all teams will be sorted by name*/
    eq.name = "D team";
    eq.teamPlace = "D team place";
    if (addNodeTeamsSorted(headEquipas,&tailEquipas,eq)){
        printf("
Error
");
        return 0;
    }

    eq.name = "A team";
    eq.teamPlace = "A team place";
    if (addNodeTeamsSorted(headEquipas,&tailEquipas,eq)){
        printf("
Error
");
        return 0;
    }

    eq.name = "C team";
    eq.teamPlace = "C team place";
    if (addNodeTeamsSorted(headEquipas,&tailEquipas,eq)){
        printf("
Error
");
        return 0;
    }

    eq.name = "B team";
    eq.teamPlace = "B team place";
    if (addNodeTeamsSorted(headEquipas,&tailEquipas,eq)){
        printf("
Error
");
        return 0;
    }

    /*Will print all the teams*/
    printListOfTeams(headEquipas);

    return 0;
}

另见我的档案2.c。 我

http://www.ohchr.org。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    typedef struct team{
        char *name;
        char *teamPlace;
    }Team;

    typedef struct nodeTeam{
        int numberOfTeams;
        Team team;
        struct nodeTeam *next;
        struct nodeTeam *prev;
    }NodeTeam;

    /*Add the teams to the doubly linked list. At the end, all teams will be sorted by name*/
    int createsListOfTeams(NodeTeam **head, NodeTeam **tail){
        (*head) = (NodeTeam *)malloc(sizeof(NodeTeam));

        if ((*head) == NULL){
            return -1;
        }
        (*head)->numberOfTeams = 0;
        (*head)->team.teamPlace = "";
        (*head)->team.name = "";
        (*head)->next = NULL;
        (*head)->prev = NULL;

        *tail = *head;
        return 0;
    }

    /*Creates the doubly linked list*/
    int addNodeTeamsSorted(NodeTeam *head, NodeTeam **tail, Team team){
        NodeTeam *no,*listIni;


        no = (NodeTeam*) malloc(sizeof(NodeTeam));
        if (no == NULL){
            return -1;
        }

        /*copy of my list*/
        listIni = head;

        no->team = team;
        /*to see is it s the first element of my list*/
        if(head->numberOfTeams == 0)
        {
            no->next = head->next;
            no->prev = head;
            head->next = no;
            *tail = no;

        }
        else{ /*If not the first element*/
            head = head->next;
            while(head->prev != *tail && strcmp(head->team.name,no->team.name) < 0 && strcmp((*tail)->team.name,no->team.name)>0){
                head = head->next;
                (*tail) = (*tail)->prev;
            }
            if(strcmp(head->team.name,no->team.name) >= 0 || head->prev == *tail){
                no->next = head;
                no->prev = head->prev;
                (head->prev)->next = no;
                head->prev = no;

            }
            else if(strcmp((*tail)->team.name,no->team.name) <= 0){
                no->next = (*tail)->next;
                no->prev = (*tail);
                (*tail)->next = no;
                *tail = no;

            }
        }

        /*Updates the number of element of the list*/
        head = listIni;
        head->numberOfTeams++;

        return 0;
    }
    /*Prints my lists*/
    void printListOfTeams(NodeTeam *listofTeams){
        printf("|   number of teams %22d |
",listofTeams->numberOfTeams);
        printf("|      team name      |        team place      |
");
        printf("--------------------------------------------------
");
        listofTeams = listofTeams->next;
        while (listofTeams != NULL){
            printf("| %-21s | %-22s |
",listofTeams->team.name,listofTeams->team.teamPlace);
            listofTeams = listofTeams->next;
        }
        printf("--------------------------------------------------

");
    }

因此,我的问题是:

<>Q1——这是否是执行一个双倍链接名单的正确方式,该名单的头和尾号分别标明名单的开始和结束?

<>Q2——为什么在我的两个档案中都宣布:<条码>、条码/条码>和<条码> 小组。 既然所有这些都属于同一项目,那么就应该把声明“看上去”给我项目的所有档案?

<<>Q3-struct Teamwhy do I have contributed toclarchar *name undchar name [31]?

最佳回答

I made some edits after your previous comments and after analyzed your code more carefully. I wrongly interpreted one remark about the head and tail items and though you were designing a circular list

  1. 我需要时间复制/编辑你的法典。 虽然这几乎是行之有效的,但我必须说,我本来会以另一种方式设计。

    • moving the prev/ next pointers into the struct team
    • and replacing the team member of nodeTeam by a head pointer to the first team.

    这将产生以下效益:

    • prevent a useless waste of space for the numberOfTeams that is duplicated for each nodeTeams but only meaningful to the first
    • avoid a confusion between the conceptual head and the actual first team

    在小组名单上增加点人的价值

    printf("| %-21s | %-22s | %p - p=%p n=%p ",listofTeams->team.name, listofTeams->team.teamPlace, listofTeams, listofTeams->prev, listofTeams->next);

    我注意到你在联系方面可能存在的难题:

    PO: 1 D-1, 1 D-1, 1 D-1, 1 P-4, 1 P-3, 1 P-2, 1 GS, 1 NS

    | B team | B team place | 0x101d009e0 - p=0x101d00980 n=0x101d009b0

    CANC小组 驻地0x 101d009b0 - p=0x101d00980 n=0x101d00950

    | D team | D team place | 0x101d00950 - p=0x101d009b0 n=0x0

    你们可以看到,下点人是ok,但前线人显示 可疑重复s(0x101d00920 是头)。

    If you trace the execution of your code and check what it done in addNodeTeamsSorted() You could notice that everything is ok until step 3 (addition of team C, after existing A & D) :

    • due to the weird double modification of both head and tail to find the place to insert the new item, head an tail are crossing: tail actually points to A and head to D (dont forget that while head is Nodeteam * and its modification won t be propagated outside of the function, tail is a Nodeteam **, thus when it is changed into the caller and for the next call it will be wrong
    • in the step 4 (addition of B ) in the else if part of the test, you change properly the prev/next of no, the next of (*tail) but not prev of no->next so you have
      • B -> next = C : OK
      • B -> prev = A : OK
      • *tail (= A ) -> next = B : OK
      • C -> prev still = A : WRONG
  2. 这并不像编辑所想的那样。 他只处理汇编单位,一次。 .c中未宣布什么,包括.h在内的不同内容将不为人所知。 如果你想要在两个单元之间分享指示声明,并预防编码维护方面的错误,减少类型,将其列入共同标题档案(例如equipa.h)。 c

  3. You have to use char* instead of char[] because in your main() in file1.c you are making direct assignments from literal strings and the compiler won t let you assign a literal string to a char array. If you wan to use char[] instead change your

    eq.nome = "D team";

    by string copying like

    strcpy(eq.nome, "D team");

    of course, I am only dealing with the concept, actually you should take care that the string that will be copied are not longer than the buffer by using strncpy() and sizeof(eq.nome)

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签