2014-02-13 43 views
1

我有作家寫先到先服務和循環賽模擬並比較它們。我開始創建一個鏈接列表來製作一個事件列表。我做了插入和print_list函數,但無法使其工作。我的朋友告訴我使用雙鏈表,所以我重新啓動鏈表,並試圖使打印功能正常工作。我有一個關於插入函數的問題,因爲我在while循環中的語句順序有誤。修復了很多錯誤後,打印功能多次打印相同的節點,而不是整個列表。我不能確定程序的哪一部分是錯誤的。我認爲我在插入函數和I點指針都有錯誤,所以我得到事件指向自己或者我在打印函數中有錯誤。打印雙向鏈接列表

當我刪除這行代碼的打印功能只打印當前節點,沒有別的。

newpointer-> next = * eventlist;

我認爲這意味着我在這部分有問題,但我不明白爲什麼。

if(prevptr == NULL)     //if putting node at the beginning of the list 
{ 
    printf("DONEW\n"); 
    newpointer->next = *eventlist;      
    *eventlist = newpointer; 
} 

能否請你告訴我,什麼是錯的代碼?

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

struct event { 
     struct event *next; 
     int processid; 
     int arrivaltime; 
     int cputime; 
}; 
typedef struct event Event; 
typedef Event *eventPtr; 

Event create_node(int processid, int arrivaltime, int cputime); 
void add_event(Event *newpointer, eventPtr *eventlist); 
void print_node(Event node); 
void print_eventlist(Event *eventlist); 
Event get_nextevent(Event *eventlist); 

int main(int argc, char* argv[]) 
{ 
    int sourcenum,a,b,c,i; 
    Event tempevent; 
    eventPtr eventlist = NULL; 
    print_eventlist(eventlist); 
    char* sources,timeSlice; 
    if(argc != 3) 
    printf("Proper usage is: main.c sources time-slice\n"); 
    else 
    { 
     sourcenum = atoi(argv[1]); 
     timeSlice = atoi(argv[2]); 
     for(i = 0; i < sourcenum ; i++) 
     { 
       //print_eventlist(&(*eventlist)); 
       printf("please enter the process-id, arrival-time and cpu-time.\n"); 
       scanf("%d %d %d",&a, &b, &c); 
       printf("something\n"); 
       tempevent = create_node(a,b,c); 
       printf("something\n"); 
       add_event(&tempevent, &eventlist); 
       print_eventlist(eventlist); 
     } 
     print_eventlist(eventlist);  
    } 
    return 0; 
} 


void add_event(Event *newpointer, eventPtr *eventlist)  //insert 
{ 
    printf("DONE1\n"); 
    eventPtr currentptr, prevptr; 
    printf("DONE2\n"); 
    prevptr = NULL; 
    printf("DONE3\n"); 
    currentptr = *eventlist; 
    printf("DONE4\n"); 
    while((currentptr != NULL && currentptr->arrivaltime < newpointer->arrivaltime))     /*on every loop previous and current pointer gets upgraded*/ 
    { 
      printf("DONEW\n"); 
      prevptr = currentptr; 
      currentptr = currentptr->next; 
    } 
    printf("DONEW\n"); 
    if(prevptr == NULL)     //if putting node at the beginning of the list 
    { 
     printf("DONEW\n"); 
     newpointer->next = *eventlist;      
     *eventlist = newpointer; 
    } 
    else //putting list between current and previous 
    { 
     printf("DONEW\n"); 
     prevptr->next = newpointer; 
     newpointer->next = currentptr; 
    } 
    printf("DONE\n"); 
} 

Event create_node(int processid, int arrivaltime, int cputime) 
{ 
     Event *ptr; 
     ptr = malloc(sizeof(Event)); 
     if(ptr != NULL) 
     { 
     ptr->processid = processid;  
     ptr->arrivaltime = arrivaltime;  
     ptr->cputime = cputime; 
     ptr->next = NULL; 
     } 
     return *ptr; 
} 

void print_node(Event node) 
{ 
    printf("process-id = %d\n",node.processid); 
    printf("Arrival Time = %d\n",node.arrivaltime); 
    printf("CPU time = %d\n",node.cputime); 
} 

void print_eventlist(eventPtr currentPtr)           /*function receives an eventlist and prints all of the events*/ 
{ 
    if(currentPtr == NULL) 
    { 
     puts("List is empty\n"); 
    } 
    else 
    { 
     puts("The List is:"); 
     while(currentPtr != NULL) 
     { 
      printf("==========================================\n");//list #%d\n",i); 
      print_node(*currentPtr); 
      printf("\n==========================================\n"); 
      currentPtr = currentPtr->next; 
     } 
     puts("NULL\n"); 
    } 
} 
    /*int i=0;          //This is what I had before 
    eventPtr currentptr; 
    *currentptr = *eventlist; 
    printf("something!!!"); 
    while(currentptr != NULL) 
    { 
     i++; 
     printf("==========================================\nlist #%d\n",i); 
     print_node(*currentptr); 
     printf("\n==========================================\n"); 
     currentptr = currentptr->next;    
    } 
}*/ 

/*Event get_nextevent(Event *eventlist) 
{  
    Event newstruct = create_node(eventlist->processid, eventlist->arrivaltime, eventlist->cputime); 
    eventlist = eventlist->next; 
    return newstruct; 
}*/ 
+1

而不是在'create_node'中返回一個本地堆棧對象,應該返回一個動態的(堆分配的)內存,可以從程序的其餘部分預測地操作它。即返回'PTR'不''PTR' .. – amdixon

+0

我改變了它,它的工作,但我不明白爲什麼?你可以請給我一本書或一頁閱讀內存分配。我看到我沒有完全理解它。 – Vato

+2

Side Bar:當我說'eventPtr'給這段代碼添加* nothing *時,你只需要相信我。你應該使用'Event *'。這將使得任何精通C的人都更容易閱讀。 – WhozCraig

回答

1

變化的類型:

Event tempevent; 

eventPtr tempevent; 

你的問題是,你通過& tempevent與事件tempevent,該地址總是一樣的。更改create_node以返回一個eventPtr。

eventPtr create_node(int processid, int arrivaltime, int cputime) 
{ 
     Event *ptr; 
     ptr = malloc(sizeof(Event)); 
     if(ptr != NULL) 
     { 
     ptr->processid = processid;  
     ptr->arrivaltime = arrivaltime;  
     ptr->cputime = cputime; 
     ptr->next = NULL; 
     } 
     return ptr; 
} 
2

在這段代碼中有很多東西需要解決。

首先,您在create_node中泄漏內存。這個功能應該專爲動態分配的情況下,與傳遞的參數做準備,並返回給調用者的地址被分配給一個指針在呼叫側:

Event* create_node(int processid, int arrivaltime, int cputime) 
{ 
    Event *ptr = malloc(sizeof(*ptr)); 
    if (ptr == NULL) 
    { 
     perror("Failed to allocate Event."); 
     exit(EXIT_FAILURE); 
    } 

    ptr->processid = processid; 
    ptr->arrivaltime = arrivaltime; 
    ptr->cputime = cputime; 
    ptr->next = NULL; 
    return ptr; 
} 

接下來,您add_event函數應該將一個事件指針作爲列表頭,理想情況下是所述指針的地址,以便在初始插入時對其進行修改。插入函數有兩種思想流派,(1)總是返回列表頭,(2)使用列表頭參數的指針指針,並根據需要通過解引用對其進行修改。我更喜歡後者,因爲它使得代碼很簡單,就是。在你的情況下更是如此,因爲在其他地方完成新的事件創建。

// link an event on to the end of our linked list. 
void add_event(Event** pp, Event* event) 
{ 
    while (*pp) 
     pp = &(*pp)->next; 
    *pp = event; 
} 

一旦這些都被認真考慮,在main()源也變得顯著簡單,考慮到我還固定不當timeSlice變量,這是char,應該已經int。更重要的是,你所擁有的邏輯將會有一個空白的列表頭節點,它不會爲你帶來任何收益。您可以使用指針,然後調用我們的add_event函數並傳遞其地址。它會正確插入節點。

最終產物看起來像這樣(輕微MODS):

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

typedef struct event 
{ 
    struct event *next; 
    int processid; 
    int arrivaltime; 
    int cputime; 
} Event; 

Event* create_node(int processid, int arrivaltime, int cputime); 
void add_event(Event** head, Event *event); 
void print_node(const Event* node); 
void print_eventlist(const Event *eventlist); 

int main(int argc, char* argv[]) 
{ 
    int sourcenum,a,b,c,i,timeslice; 
    Event* events = NULL; 

    if(argc != 3) 
    { 
     printf("Proper usage is: main.c sources time-slice\n"); 
     exit(EXIT_FAILURE); 
    } 

    // TODO: these really should be using strtol() for better 
    // error detection. left as exercise for author. 
    sourcenum = atoi(argv[1]); 
    timeslice = atoi(argv[2]); 

    for(i = 0; i < sourcenum ; i++) 
    { 
     printf("please enter the process-id, arrival-time and cpu-time.\n"); 
     if (scanf("%d %d %d",&a, &b, &c) == 3) 
     { 
      add_event(&events, create_node(a,b,c)); 
      print_eventlist(events); 
     } 
    } 
    return 0; 
} 

// link an event on to the end of a linked list. 
void add_event(Event** pp, Event* event) 
{ 
    while (*pp) 
     pp = &(*pp)->next; 
    *pp = event; 
} 

Event* create_node(int processid, int arrivaltime, int cputime) 
{ 
    Event *ptr = malloc(sizeof(*ptr)); 
    if (ptr == NULL) 
    { 
     perror("Failed to allocate Event."); 
     exit(EXIT_FAILURE); 
    } 

    ptr->processid = processid; 
    ptr->arrivaltime = arrivaltime; 
    ptr->cputime = cputime; 
    ptr->next = NULL; 
    return ptr; 
} 

void print_node(const Event* node) 
{ 
    if (node != NULL) 
    { 
     printf("process-id = %d\n",node->processid); 
     printf("Arrival Time = %d\n",node->arrivaltime); 
     printf("CPU time = %d\n",node->cputime); 
    } 
    else 
    { 
     printf("NULL\n"); 
    } 
} 

void print_eventlist(const Event* currentPtr) 
{ 
    puts("=========================================="); 
    while(currentPtr != NULL) 
    { 
     print_node(currentPtr); 
     puts("=========================================="); 
     currentPtr = currentPtr->next; 
    } 
    puts("NULL"); 
    puts("==========================================");//list #%d\n",i); 
} 

樣本輸出

從命令行請求5項獲得。

please enter the process-id, arrival-time and cpu-time. 
1 1 1 
========================================== 
process-id = 1 
Arrival Time = 1 
CPU time = 1 
========================================== 
NULL 
========================================== 
please enter the process-id, arrival-time and cpu-time. 
2 2 2 
========================================== 
process-id = 1 
Arrival Time = 1 
CPU time = 1 
========================================== 
process-id = 2 
Arrival Time = 2 
CPU time = 2 
========================================== 
NULL 
========================================== 
please enter the process-id, arrival-time and cpu-time. 
3 3 3 
========================================== 
process-id = 1 
Arrival Time = 1 
CPU time = 1 
========================================== 
process-id = 2 
Arrival Time = 2 
CPU time = 2 
========================================== 
process-id = 3 
Arrival Time = 3 
CPU time = 3 
========================================== 
NULL 
========================================== 
please enter the process-id, arrival-time and cpu-time. 
4 4 4 
========================================== 
process-id = 1 
Arrival Time = 1 
CPU time = 1 
========================================== 
process-id = 2 
Arrival Time = 2 
CPU time = 2 
========================================== 
process-id = 3 
Arrival Time = 3 
CPU time = 3 
========================================== 
process-id = 4 
Arrival Time = 4 
CPU time = 4 
========================================== 
NULL 
========================================== 
please enter the process-id, arrival-time and cpu-time. 
5 5 5 
========================================== 
process-id = 1 
Arrival Time = 1 
CPU time = 1 
========================================== 
process-id = 2 
Arrival Time = 2 
CPU time = 2 
========================================== 
process-id = 3 
Arrival Time = 3 
CPU time = 3 
========================================== 
process-id = 4 
Arrival Time = 4 
CPU time = 4 
========================================== 
process-id = 5 
Arrival Time = 5 
CPU time = 5 
========================================== 
NULL 
========================================== 

無論如何,希望它給你一些好點子。