簡體   English   中英

C插入排序-實現

[英]C Insertion Sort - Implementation

我剛剛開始使用C進行編程,在實現插入排序時需要一些幫助。

我正在執行C列表插入排序。

這是它的偽代碼,我想將其轉換為C

除此以外
使用循環在列表中找到應該在新人員之前的最后一項,並將新人員的“下一個”鏈接設置為指向此列表項之后的所有內容

設置此項的“下一個”鏈接以指向新人員

返回(開始)列表

這是我的偽代碼的部分實現

else    
      {
         for (int i =0; i < HOW_MANY; i++) 
         {

        people = people -> next;
        if (people -> next == NULL) return people;
     } //for
      }//else

       return pointer;    
}

這是我的完整方法:

struct person *insert_sorted (struct person *people, char *name, int age) {
//create a new space for the new person
  struct person *pointer = malloc(sizeof(struct person));
   // check it succeeded
    if(pointer == NULL)
    { 
     printf("The program could not allocate memory ");
      exit(-1);
    }
     // set the data for the new person
      strcpy(pointer -> name, name);
      pointer -> age = age;
      pointer -> next = people;
     // if the current list is empty
      if (people == NULL)
      {
        // set the new person's "next" link to point to the current list"
    pointer -> next = people;
    // return a pointer to the new person
    return pointer;
      }
else    
      {
         for (int i =0; i < HOW_MANY; i++) 
         {

        people = people -> next;
        if (people -> next == NULL) return people;
     } //for
      }//else

       return pointer;    
}

如果您需要完整的程序說明,​​請告訴我。

謝謝!

莎拉 :)

在將元素插入列表之前*人,您應該檢查正確的位置。 嘗試這個:

struct person *insert_sorted (struct person *people, char *name, int age) {

//create a new space for the new person
struct person *pointer = malloc(sizeof(struct person));
// check it succeeded
if(pointer == NULL)
{ 
 printf("The program could not allocate memory ");
  exit(-1);
}
 // set the data for the new person
  strcpy(pointer -> name, name);
  pointer -> age = age;
struct person *cursor = people;
struct person *previous = people;
if(people == NULL){
    pointer->next = NULL;
    return pointer;
}
while(cursor!=NULL && strcmp(pointer->name,cursor->name)<0){
    previous = cursor;
    cursor = cursor->next;
}
if(previous!=NULL)
    previous->next = pointer;
pointer->next = cursor;
return people;
}

通過這種方式,您可以在第一個元素之后插入新元素,並將其名稱按字母順序小寫,然后將其與下一個元素鏈接。

除了正確地命名事物,消除多余的注釋並通過indent -linux運行它之外,我對這段代碼沒有做任何事情。

struct person *insert_sorted(struct person *people, char *name, int age)
{
        struct person *newperson = malloc(sizeof *newperson);
        if (newperson == NULL) {
                printf("The program could not allocate memory ");
                exit(-1);
        }

        strcpy(newperson->name, name);
        newperson->age = age;
        newperson->next = people;

        if (people == NULL) {
                newperson->next = people;
                return newperson;
        }

        for (int i = 0; i < HOW_MANY; i++) {
                people = people->next;
                if (people->next == NULL)
                        return people;
        }

        return newperson;
}

有幾件事會立即彈出:

  • 尚不清楚您是否要初始化*newperson 最安全的是newperson = calloc(1, sizeof *newperson) ,因此讀者甚至不必考慮它,總是好結果。

  • 您沒有顯示結構定義,但是您沒有檢查入站名稱是否適合newperson-> name的存儲空間,並且如果newperson-> name是指針,則根本沒有分配任何指針。

  • 對newperson-> next進行了多余的分配,提出了相反的問題,即是否有不明顯的東西或可能缺少的東西使之必要。

  • 我看不到您在哪里比較關鍵值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM