打印双链表

时间:2014-02-13 07:47:14

标签: c doubly-linked-list

我有一个写作先到先服务和循环罗宾模拟的作业并比较它们。我开始创建一个用于制作事件列表的链表。我已经制作了insert和print_list函数,但是无法使它工作。我的朋友告诉我使用Doubly Linked List,所以我重新处理链表,并尝试使打印功能正常工作。我有一个关于我的插入函数的问题,因为我在while循环中的语句顺序错误。修复了许多错误后,print函数多次打印同一节点,而不是整个列表。我不能在程序的哪个部分出错。我认为我在插入函数和指针错误中都有错误,所以我得到事件指向自己或者我在打印函数中有错误。

当我删除这行代码时,print函数只打印当前节点而没有其他内容。

  

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;  
}*/

2 个答案:

答案 0 :(得分: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函数并传递其地址。它将正确插入节点。

最终产品看起来像这样(带有少量mod):

#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
==========================================

无论如何,希望它能给你一些好主意。

答案 1 :(得分:1)

更改类型:

 Event tempevent;

 eventPtr tempevent;

你遇到的问题是你传递&amp; tempvent并且在事件发生时,这个地址总是一样的。更改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;
}