在链表上应用冒泡排序会在c中输出错误的输出

时间:2014-03-04 16:24:13

标签: c algorithm sorting data-structures linked-list

我正在尝试使用冒泡排序对链接列表进行排序。但是它会给出分段问题

我的逻辑如下:

注意:请注意我必须每个地方只使用引用传递(没有节点的返回和全局声明)

void bubble_sort(node * * List, int size) 
{
    int i, j, temp;
    node * first;
    node * second;
    node * final;
    final=*List

    for (i = 0; i < size - 1; i++)
    {
      first =final;
      second = first -> link;
        for (j = 0; j < size; j++) 
        {

            if (first -> data < second -> data)
            {
                temp = first -> data;
                first -> data = second -> data;
                second -> data = temp;
            }
            first = first -> link;
        }
    } * List = first;
}

它的函数调用是:

bubble_sort(&start,val);

任何人都可以帮我纠正逻辑吗?

编辑:我的完整代码:

  #include <stdio.h>
//#include <conio.h>
#include <malloc.h>

/* structure containing a data part and link part */
struct node 
{
    int data;
    struct node * link;
};
typedef struct node node;
node * start, * visit, * newnode;
//start=NULL
///////////////////////////////// Function declaration  ///////////////////
void append(node * * q, int num);
void displaylist(node * start);
void bubble_sort(node * * List, int val);
//////////////////////////////////////////////////////////////////////////////////////////
void append(node * * q, int num)
 {
    node * temp, * temp2;
    temp = * q;
    //temp= (node* )malloc(sizeof (node));
    if (temp == NULL) /* if the list is empty, create first node */
    {
        temp = (node * ) malloc(sizeof(struct node)); * q = temp;
    } else
    {
        temp2 = * q;
        while (temp2 -> link != NULL) 
        {
            temp2 = temp2 -> link;
        }
        temp = (node * ) malloc(sizeof(struct node));
        temp2 -> link = temp;
    }
    temp -> data = num;
    temp -> link = NULL;
}
///////////////////////////////////////////////////////////////////////
void displaylist(node * start) 
{
    visit = start;
    /* traverse the entire linked list */
    while (visit != NULL) 
    {
        printf("%d ", visit -> data);
        visit = visit -> link;
    }
}
/////////////////////////////////////////////////////////////////////////////////

/*
void bubble_sort(node **List,int size)
{
int i,j,temp;
node*first;
node*second;
node*final;
final=*List;
for(i=0;i<size-1;i++)
{
first=final;
second=first->link;
for(j=1;j<size;j++)
{

   if( first->data<second->data)
   {
        temp=first->data;
        first->data=second->data;
        second->data=temp;   
    }
   first=first->link;
   }
}
*List=first;
}   */
//////////////////////////////////////////////////////////////////////////////////////
void bubble_sort(node * * List, int size)
 {
    int i, j, temp;
    node * first;
    node * second;
    node * final;
    for (i = 0; i < size; i++) 
    {
        first = * List;
        for (j = 0; j < size - 1; j++) 
        {
            if (first -> data < first -> link -> data) 
            {
                temp = first -> data;
                first -> data = first -> link -> data;
                first -> link -> data = temp;
            }
            first = first -> link;
        }
    }

}

////////////////////////////////////////////////////////////////////////////////////////////
main() 
{
    start = NULL;
    int val, n, size;
    char ch;
    struct node * new;
    new = NULL;
    printf("\nEnter the size desired size of Linked list: \n");
    scanf("%d", & size);
    for (ch = 0; ch < size; ch++) 
    {
        printf("\nEnter a value: ");
        scanf("%d", & val);
        append( & new, val);
    }
    start = new;
    displaylist(start);
    printf("\nApplying Bubble sort on Linked list: \n");
    bubble_sort( & start, val);
    displaylist(start);
    printf("\n");
}

2 个答案:

答案 0 :(得分:1)

假设Val是列表中的节点数,您实际上没有对链接列表进行排序,而是将值从一个节点交换到另一个节点,以便对其进行排序。

bubble_sort( &start, size); change this as well should fix it(pass size not val)

试试这个。

void bubble_sort(node **List, int val)
{
int i,j,temp;
node *first;
node *second;
node *final;
for(i = 0; i < val; i++)
{
 first = *list
 for(j = 0; j < val-1; j++)
    {
        if(first->data < first->link->data)
        {
            temp = first->data;
            first->data = first->link->data;
            first->link->data = temp;    
        }
        first = first->link;
    }
}

}

答案 1 :(得分:1)

我相信你得到了一个分段错误,因为你试图在内部for循环中访问Null指针作为节点。

for (j = 0; j < val; j++)

这应该是:

for (j = 0; j < val - 1; j++)

因为在您的解决方案中j = val-1,它仍会尝试访问first->link->data,但first->linkNull