开发者

Trying to make a single-linked list in C++ that stores values in chronological order instead of storing everything backwards

I've been working for hours trying to get this program to insert values into new nodes or "Player" structures for this program.

The thing is, this takes place inside function main(). My teacher requires me to insert as little code as possible since essentially "everything" is there which allows me to change it.

Here's the original code that stores each new value in the head while putting the older value inside "addNew":

Player * head = NULL;

for(int i=0; i<100; i++)
{
    Player * addNew = (Player *)malloc(sizeof(Player));
    if(head == NULL)
    {
        head->len = i;

        Player * addNew = (Player *)malloc(sizeof(Player));

        head->next = NULL;
    }

    addNew->next = addNew;
    addNew->len = i;
}

Player * p = head;
//do this until 'p' has no address.
for(int i=0; p!=0; i++)
{
    printf("%s ", p->str);
    p = p->next;
}

Does anyone have any ideas on how to solve this? IMPORTANT: My teacher would like me to not add any new variables or a tail. Please don't ask.

Update: Here's some older code:

//The head is the last one to hold a value. Therefore it gets pushed to the right.
    Player * head = NULL;

    Original Algorithm
    for(int i=0; i<5; i++)
    {
        Player * addNew = (Player *)malloc(sizeof(Player));
        printf("Insert a string: ");
        scanf("%s", addNew->str);

        addNew->next = head; //assign head's current address to addNew->next
        head = addNew; //assign all of addNew to head       
    }

Update: Here's a new implementation which doesn't work. I can't really find out where exactly that other for loop should go.

addNew->ID = 1;
        addNew->ID += i;
        if(head == NULL)
        { 
            head = addNew;
            addNew->n开发者_如何学Pythonext = head; //assign head's current address to addNew->next
            //head->next = addNew;
        }

        //head->next = addNew;  
        addNew->next = head;
        //head = addNew; //assign all of addNew to head     
        printf("%d\n", addNew->ID);


Simple trick: have the list* always point to the last element in the list. And set that last element's next pointer to the start of the list. Now you can always easily find both the start and the end of the list with just one pointer. The start is at list->next. Don't call it tail, just "list".


Right now, you're adding everything to the head of the list. However, you want to add to the tail instead.

You already have a pointer to the head. In addition to that though, you'll also need a pointer to the tail. Can you think of a way to get (and update) it?


You could save a pointer to the tail of the list and instead of saving the next item on each node, save the previous one.


This might not be an option but I thought I'd suggest it anyway. Instead of storing things in a different order, you could leave things stored as they are, but simply change the way you output the values. This would not only be very simple to implement, it would also be a faster during list inserts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜