2

所以这是我的链表程序代码。我知道这不是很好,但它确实有效。我想更改 ins() 函数,以便按大小将新元素插入到列表中。即,列表中的最后一个节点将包含最大的整数,而最小的第一个。整数是从文本文件中读入的,正如您在 main() 中所见,文本文件中的 INSERT 和 REMOVE 命令被解释为插入(下一行的以下整数)到列表中的命令,或者从列表中删除一个元素。如何使用现有代码执行此操作?我尝试修改我的 ins() 函数,但无法正确排序我的列表。

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

struct node {
    int number;
    struct node *next;
};

/* prototypes */

void ins(struct node *llist, int number);
void rem(struct node *llist);
void sho(struct node *llist);

int main(void)
{
    int number;
    char command[6];
    struct node *llist;
    struct node *root;

    llist = (struct node *)malloc(sizeof(struct node));
    llist->number = 0;
    llist->next = NULL;

    root = llist;

    printf("addr: \n\n%p,%p\n\n", &llist, &root);

    FILE *file;
    file = fopen("a3data.txt", "r");

    if (file == NULL)
    {
        printf("\n----------------------------------------\n");
        printf("| Error.  Did not read file.  Exiting. |\n");
        printf("----------------------------------------\n\n");
        exit(1);
    }
    else
    {
        while ((fscanf(file, "%s", command)) != EOF)
        {
            if((strcmp(command, "INSERT"))==0)
                {
                    fscanf(file, "%d", &number);
                    printf("\nINSERT            ", number);
                    ins(llist, number);
                    sho(llist);
                }
            else if((strcmp(command, "REMOVE"))==0)
                {
                    printf("\n         REMOVE   ");                
                    rem(llist);
                    sho(llist);
                }
        }
    }

    printf("\n");
    free(llist);
    return(0);
}

void ins(struct node *llist, int number) 
{
    while(llist->next != NULL)
    {
        llist = llist->next;
    }

    llist->next = (struct node *)malloc(sizeof(struct node));
    llist->next->number = number;
    llist->next->next = NULL;
}

void rem(struct node *llist)
{
    while(llist->next->next != NULL)
    {
        llist = llist->next;
    }

    llist->next = NULL;
}

void sho(struct node *llist)
{
    while(llist->next != NULL)
    {
        printf("%d ", llist->number);
        llist = llist->next;
    }

    printf("%d", llist->number);
}
4

3 回答 3

0

Modified function in your case

void ins(struct node *llist, int number) 
{
  struct node *llist1;
  while(llist->next != NULL)
  {
    if (llist->next->number > number)
       break;
    llist = llist->next;
  }

  llist1 = (struct node *)malloc(sizeof(struct node));
  llist1->number = number;
  llist1->next = llist->next;
  llist->next = llist1;
}
于 2013-07-19T07:12:03.820 回答
0

in ins() function, instead of

while(llist->next != NULL)

use

while((llist->next != NULL) && (llist->number < number))

This will help you to sort while inserting itself.

Also in rem() function, you have to free the memory that you dynamically allocate.

于 2013-07-19T06:53:53.673 回答
0

Your insert is simply adding the element in the tail. You have to write code to compare the present value of node(list->number) against the number to be inserted. If the current value is greater than the number to be inserted, then the number has to be inserted before the current node. For this, you will have to keep previous pointer also. The code, I have written, is just to understand the logic, Didn't check for correctness.

if(llist->number > innumber) {
    node *newNode = (struct node *)malloc(sizeof(struct node);
    newNode->number = innumber; 
    prevPointer->next = newNode;
    newNode->next = llist;
}
于 2013-07-19T06:54:00.623 回答