指向链表的指针随机变化

时间:2018-12-28 20:33:37

标签: c pointers linked-list stack void-pointers

对于我的一项学校作业,我必须制作自己的堆栈库和POSTFIX计算器。 计算器必须利用堆栈库并进行一些计算。

我将两个不同的数字压入堆栈。数字6和3。标头应指向最近添加的节点(LIFO)。因此,当添加6时:

  

HEADER-> 6-> NULL

添加3时:

  

HEADER-> 3-> 6-> NULL

当我在添加'6'之后打印标题的值时,这很好。正在打印6。 但是,当我在添加“ 3”之前打印标题的值时,它正在打印“ 3”。何时仍应打印6。

所以我的问题总结如下: 将另一个节点添加到我的链表中时,标头突然指向最新节点,甚至没有更改它。

通过一些代码和调试结果,您可能会更好地理解我。 顺便说一句:不要介意typedef,我不喜欢它们。我的老师希望我们使用它。

typedef struct stackObject* pStackObject_t;
typedef struct stackObject
{
    void* obj;
    pStackObject_t next;
} StackObject_t;

typedef struct stackMEta* pStackMeta_t;
typedef struct stackMEta
{
    pStackObject_t stack;
    size_t objsize;
    int numelem; //number of elements
    int handle; //welke stack
    pStackMeta_t next;
} StackMeta_t;

int mystack_push(int handle, void* obj)
{
**DELETED NON RELATED CODE BASED ON FEEDBACK**
    if (currentMeta->handle == handle)
    {
        pStackObject_t newObject = malloc(sizeof(StackObject_t));
        newObject->obj = obj;
        printf("%s%d\n", "Wanting to push int to stack:  ", *(int*)obj);

        //First node
        if (currentMeta->stack == NULL)
        {
            currentMeta->stack = newObject;
            currentMeta->stack->next = NULL;
            printf("%s%d\n", " FIRST Curentmeta->stack pointing to  ", *(int*)currentMeta->stack->obj);
            return 0;
        }
        else
        {
            printf("%s%d\n", "NOT FIRST Currentmeta->stack pointing to ", *(int*)currentMeta->stack->obj);
            newObject->next = currentMeta->stack;
            currentMeta->stack = newObject;
            printf("%s%d\n", "Currentmeta->stack ", *(int*)currentMeta->stack->obj);
            printf("%s%d\n", "Currentmeta->stack->next ", *(int*)currentMeta->stack->next->obj);
            printf("%s%d\n", "Succesful pushed int to stack:  ", *(int*)currentMeta->stack->obj);
            return 0;
        }
    }
return -1;
}

终端:

Created stack with handle: 1 and objsize 4 bytes
Wanting to push int to stack:  6
FIRST Curentmeta->stack pointing to  6
Wanting to push int to stack:  3
NOT FIRST Currentmeta->stack pointing to 3
Currentmeta->stack 3
Currentmeta->stack->next 3
Succesful pushed int to stack:  3

我的单元测试在此代码下运行良好。我的计算器没有,虽然是相同的函数调用。

1 个答案:

答案 0 :(得分:0)

我发现它的工作速率是50/50。在另一个程序中使用相同的输入值会带来很多好处。

我将代码更改为此:

  DateRange dateRange = new DateRange() { StartDate = "2015-06-15", EndDate = "2015-06-30" };

    // Create the Metrics object.
    Metric sessions = new Metric { Expression = "ga:sessions", Alias = "Sessions" };

    //Create the Dimensions object.
    Dimension browser = new Dimension { Name = "ga:browser" };

    // Create the ReportRequest object.
    // Create the ReportRequest object.
    ReportRequest reportRequest = new ReportRequest
    {
        ViewId = "XXXX",
        DateRanges = new List<DateRange>() { dateRange },
        Dimensions = new List<Dimension>() { browser },
        Metrics = new List<Metric>() { sessions }
    };

    List<ReportRequest> requests = new List<ReportRequest>();
    requests.Add(reportRequest);

    // Create the GetReportsRequest object.
    GetReportsRequest getReport = new GetReportsRequest() { ReportRequests = requests };

    // Call the batchGet method.
    GetReportsResponse response = analyticsreporting.Reports.BatchGet(getReport).Execute();

现在一切正常。先前的代码已经超出了范围,但以某种方式使用了旧值。谢谢大家的帮助。