有关链表和堆栈如何工作的上一个问题

时间:2019-07-06 08:56:06

标签: c data-structures linked-list stack

昨天,我从一位新撰稿人那里看到了this question,内容涉及链表如何在C中工作。我无法使代码正常运行,因此我以不同的方式实现了该表。我花了一些时间。在完成并发布我的答案之前,该问题被搁置为离题,因为它没有显示期望的和实际的输出,因此我无法发布我的答案。由于我的回答可能会对某人有所帮助,因此我决定重新发布问题并提供我的代码。

首先,我按照Antti Haapala和Weather Vane的建议修改了OP代码。以下是我的版本:

#include <stdio.h>
#include <stdlib.h>
typedef struct suppliers {
    int idSUP;
    int coonection;
    int bill;
    suppliers* next;
}suppliers;
void printlist(suppliers* h);
void insert(suppliers* head, int idSUP, int coonection, int bill);
void main() {
    int idSUP;
    int coonection;
    int bill;
    suppliers* head = NULL;
    printf("please enter supplier data\n");
    while (scanf("%d,%d,%d", &idSUP, &coonection, &bill) != EOF)
        insert(head, idSUP, coonection, bill);
    printlist(head);
}

void insert(suppliers* head, int idSUP, int coonection, int bill) {
    suppliers* t, temp;
    t = (struct suppliers*)malloc(sizeof(struct suppliers));
    if (t == NULL) {
        printf("ERROR");
    }
    t->idSUP = idSUP;
    t->bill = bill;
    t->coonection = coonection;

    t->next = head;
    head = t;
}

void printlist(suppliers* h) { while (h != NULL) { printf("%d", h->bill); h = h->next; } }

我输入了以下内容,但输入Ctrl + Z后程序挂在scanf上。

please enter supplier data
1,1,1
^Z

我很少使用scanf,所以我不知道问题出在哪里。也许这是OP遇到的相同问题。我不知道。有谁知道为什么我上面链接到的上一个问题的OP中的代码没有产生任何输出?

以下是我建议的修订版本。

1 个答案:

答案 0 :(得分:0)

对于输入的输入方式和head结构的实现方式,我采取了不同的方法。我没有使用指向supplies的指针,而是创建了一个q结构,该结构保留指向第一个节点和最后一个节点的指针。最后一个节点指针用于用刚用malloc创建的节点快速更新其下一个指针。

具有最后一个指针,实现堆栈并不难。 (好吧,很难,因为我并不那么快。;)

我也将字段重命名为更长的名称,

下面是我的修改代码:

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

typedef struct suppliers {
    int idSupplier;
    int connection;
    int bill;
    suppliers* nextPtr;
    suppliers* previousPtr;
}suppliers;

typedef struct {
    size_t qCount;
    suppliers* firstPtr;
    suppliers* lastPtr;
}q;

void insert(q* list, int idSupplier, int connection, int bill);
void printList(q list);
void printStack1(q stack);
void printStack2(q stack);


int main() {
    int idSupplier;
    int connection;
    int bill;
    int scanCount; 
    q list;
    q stack;

    list.firstPtr = NULL;
    list.lastPtr = NULL;
    list.qCount = 0;
    stack.firstPtr = NULL;
    stack.lastPtr = NULL;
    stack.qCount = 0;

    printf("Please enter supplier data.  Enter 'q' to quit.\n");
    scanCount = scanf("%d, %d, %d", &idSupplier, &connection, &bill);
    while (scanCount) {
        insert(&list,  idSupplier, connection, bill);
        insert(&stack, idSupplier, connection, bill);
        scanCount = scanf("%d, %d, %d", &idSupplier, &connection, &bill);
    }
    if (list.qCount > 0) {
        printList(list);
    }
    if (stack.qCount > 0) {
        printStack1(stack);
        printStack2(stack);
    }
}

void insert(q* queue, int idSupplier, int connection, int bill)
{
    suppliers* t;

    queue->qCount++;

    t = (suppliers*)malloc(sizeof(suppliers));
    if (t == NULL) {
        printf("ERROR");
    }

    if (queue->firstPtr == NULL) {
        queue->firstPtr = t;
    }
    else {
        queue->lastPtr->nextPtr = t;
    }
    t->previousPtr = queue->lastPtr;
    queue->lastPtr = t;

    t->idSupplier = idSupplier;
    t->bill = bill;
    t->connection = connection;
    t->nextPtr = NULL;
}

void printList(q list) 
{ 
    suppliers* currentPtr;

    printf("\n\n");
    currentPtr = list.firstPtr;
    while (currentPtr != NULL) {
        printf("sup=%-3d   conn=%-3d   bill=%-3d\n", currentPtr->idSupplier, 
            currentPtr->connection, currentPtr->bill);
        currentPtr = currentPtr->nextPtr;
    }
}

void printStack1(q stack)
{
    suppliers* currentPtr;

    if (stack.lastPtr != NULL) {
        printf("\n\n");

        currentPtr = stack.lastPtr;
        do {
            printf("sup=%-3d   conn=%-3d   bill=%-3d\n", currentPtr->idSupplier,
                currentPtr->connection, currentPtr->bill);
            currentPtr = currentPtr->previousPtr;
        }
        while (currentPtr != stack.firstPtr);
        printf("sup=%-3d   conn=%-3d   bill=%-3d\n", currentPtr->idSupplier,
            currentPtr->connection, currentPtr->bill);
    }
}

void printStack2(q stack)
{
    suppliers* currentPtr;

    printf("\n\n");

    currentPtr = stack.lastPtr;
    for (size_t i = stack.qCount; i > 0; i--) {
        printf("sup=%-3d   conn=%-3d   bill=%-3d\n", currentPtr->idSupplier,
            currentPtr->connection, currentPtr->bill);
        currentPtr = currentPtr->previousPtr;
    }
}

这是我的输出:

Please enter supplier data.  Enter 'q' to quit.
1,  2,  3
4,  5,  6
10, 11, 12
20, 21, 22
q


sup=1     conn=2     bill=3
sup=4     conn=5     bill=6
sup=10    conn=11    bill=12
sup=20    conn=21    bill=22


sup=20    conn=21    bill=22
sup=10    conn=11    bill=12
sup=4     conn=5     bill=6
sup=1     conn=2     bill=3


sup=20    conn=21    bill=22
sup=10    conn=11    bill=12
sup=4     conn=5     bill=6
sup=1     conn=2     bill=3

存储创建的节点数可以让我有两种不同的方式来打印堆栈。就我个人而言,我更喜欢printStack2,但这就是我。

希望这会有所帮助。

更新:我将其标记为正确答案,即使不是。。这是实现这些内容的一种方法,但不是最好的方法。

相关问题