2015-04-29 28 views
0

我真的不知道如何讓雙向鏈表工作。我以前使用平行數組來排序用戶名和年齡與一個bubblesort。 目標是從用戶讀取名稱和年齡並將值存儲到節點中。打印向前和向後排列的值,我認爲使用strcmp對於這個程序會更容易,因爲對我來說bubblesort非常困難。
我需要基於目前爲止的代碼提供建議。什麼是最好的方式來使這個程序的工作。如何使用雙向鏈表向前,向後排序並輸出到文件

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

//create structure of person that can hold a name and an age that can be used in a doubly linked list 

int main(int argc, char *argv[]) 
{ 
    struct person //structure of person 
    { 
     char name [41]; 
     int age; 
     struct person *next;//doubly linked list 
     struct person *prev;  
    }; 

    typedef struct person people;//using a new type of our structure 

    people first;//declade first of type people 
    first.age = 12;//puts 12 in the age portion of the variable first of the structure person which can hold someones age 

    strcpy (first.name, "sue"); 
    people *p; //creates a pointer to a storage location 
    p = &first; //that storage location now has the contents of the storage location of first. 
    (*p).age = 13; //this is the same as p->age=13; or arrow. This dereferences the pointer. arrow for pointer, (*x) 
    //dereferencing through structure 
    //make two pointers to manage a doubly linked list 
    //make two pointers of type people named head and tail 
    people *head,*tail; 
    *head = NULL; 
    *tail = NULL; 
    //start with printing rout9ine 
    printit(head);//shows nothing 
    //linked list 
    void printit(people *h); 
    { 
      people *t; 
      t=h; 
      while (t != NULL) 
      { 
        //print the information here, he didnt give me code so i need to remember this on my own 
        t = t->next;//continues the list  goes through pointers to the first node then destroyes t by making it 
        //the next. You can also use the h. 
      } 
    } 

    //sending address, pass by reference 

    buildit(&head,&tail); 
    //head is a pointer of type people, want the pointer not the value 
    //so you need a pointer of a pointer, it acccepts the address of head and tail which 
    //is the address of the addres of the start and end of the linked list 
    void buildit (people **h, people **t) 
    { 
      people *current; 
      current = malloc (sizeof(people)); //creates space for a people structure, current points to it. 
      //assume doubly linked list 
      //t refers to the address of tail, if you want the contents of the address of tail you need *t. 
      (*t) -> next = current; 
      current -> prev = *t; 
      *t = current; 
    } 

    //structure in iostream, called FILE 
    FILE *out; 
    out = fopen(/*name of the file*/ stringname, access); 
    //fopen opens a file, you can open stdin, com 1, com 2, opens keyboard or ports or files. 
    //access has two values, r and w. r = read w = write. input reads from output writes to. 
    out = fopen("output.txt","w"); //opens output.txt 

    char filename [81]; 
    gets (filename); //allows a user to input the file they want to open. array of chars, reads entire string spaces and all 
    out = fopen(filename,"w"); 

    if (out != NULL) //checks if file is open or note, if null no file opened up, it checks 
    { 
    fprintf (out, "formatstring"; parameterlist); //file print f   
    fscanf //adds file handle to parameters, thats it. 
    fgets //same, file gets 
    } 
    //changing a file needs pass by reference, not value. 
    ptintit(person*, FILE**); 
    void printit(person*h, FILE**o) 
    { 
     fprintf (*o,",,,,,",...); 
    } 


    system("PAUSE");  
    return 0; 
} 
+0

您是否需要使用雙向鏈表?如果不是這樣,使用數組將是更好的選擇(如peterh建議的),因爲元素將被放置在連續的存儲器位置,並且每個元素訪問將更有效率,因爲可以避免大量不必要的指針解引用。 – jithinpt

+0

另外,您能否更清楚地陳述您面臨的問題?你得到一個不正確的輸出?或者你想要更好的表現? – jithinpt

+0

不,它必須是一個雙向鏈表。我試圖用指針將我的頭圍繞在地址的地址上。 – IdislikeCalot

回答

0

如果你想要一個簡單的解決方案,bubble sorting會做你想要什麼。

在速度很重要的環境中,該列表將被轉換爲更適合任務的數據結構(從數組到數組),並且將使用更快的算法進行排序。

0

列表排序操作只需要一個鏈接列表,因此您可以使用下一個指針實現排序,然後在排序之後,通過列表設置前面的指針。向後和向前只需要改變比較的意義,除非你真的想要使用先前的指針進行向後排序,然後通過列表來設置下一個指針。

冒泡排序可以工作,但是從第二個空列表指針開始(比如node * sorted = NULL;),然後從原始列表中移除一個節點並將它們插入到第二個列表在適當的位置。對此的另一種變化是掃描具有最大值的節點的原始列表,然後從原始列表中刪除該節點並將其預先加入到排序列表中。

自底向上合併排序會快得多,但在這種情況下可能不需要。