English 中文(简体)
Bubble Sorting a associated list, by name
原标题:Bubble Sorting a linked list, by names
  • 时间:2012-05-20 19:47:53
  •  标签:
  • c

I have a linked list, and I want to sort them by names (for example the names "Bx", "Tx", "Ax" would become : "Ax", "Bx", "Tx")... I need to switch the names if the one in the node s right has a "smaller name"..

这是我写的:

typedef struct data
{
char *name;
}data;

typedef struct Node 
{
data NodeData;
struct Node *next;
struct Node *prev;
}Node;

void Sorting(Node *head)
{
 Node *temp = head;
 Node *temp2 = (Node*)malloc(sizeof(Node));
 while (temp != NULL)
 {
       if (1 == (strcmp(temp -> NodeData.name, temp -> next -> NodeData.name))) 
       {
              strcpy (temp2 -> NodeData.name, temp -> NodeData.name);
              strcpy (temp -> NodeData.name, temp -> next -> NodeData.name);
              strcpy (temp -> next -> NodeData.name, temp2 -> NodeData.name);
       } 

       temp = temp -> next;

 }

}

I m getting an runtime - error on the part where I need to swarp betwen the node s name(the strcpy lines): An access violation (segmentation fault)...

最佳回答
if (1 == (strcmp(temp -> NodeData.name, temp -> next -> NodeData.name))) {...}

When this line is executed, there is no guarantee that temp->next is not NULL. If it is NULL, temp->next->NodeData.Name would be rather painfull.

而且,正如已经说过的那样,测试波环(Sstrcmp)结果与1相比是不正确的。 扼杀可转折任何价值等于零、零或零。

而且,正如已经说过的那样,扼杀是不正确的;扼杀可能具有不同的分配规模,或者可以生活在不可磨灭的记忆中(固定不变)。 浏览点就足够了。

最新资料:

void Sorting(Node *head)
{
 Node *temp ;

                /* since the compare dereferences temp->next you ll have to verify that it is not NULL */
 for (temp = head; temp && temp->next; temp = temp->next)  
 {
       if (strcmp(temp->NodeData.name, temp->next->NodeData.name) > 0 )  
       {
                /* no need for a whole node, since you only copy a pointer */
              char *cp;
              cp = temp->NodeData.name;
              temp->NodeData.name = temp->next->NodeData.name;
              temp->next->NodeData.name = cp;
       } 

 }

}

BTW:划出一个有链接的清单确实令人怀疑。 相联的清单比较容易,而且更加精.地被合并。

问题回答

我不认为你应该通过交换数据价值来对清单进行分类,相反,你应该把标语自己.掉。 请注意,这要求你把点子归还名单上新的第一点。

EDIT:如果你重新成为双向点,通过名单负责人的点子也是行之有效的。 它可以使法典更加简单。

char* 此处

char *temp2;

Node 此处

strcpy (temp2 -> NodeData.name, temp -> NodeData.name);

因此,根据这一条,你的行为是:<条码>temp2<>> 代码”是一种结构或结合,但不是。





相关问题
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 ...

热门标签