堆栈与链表的实现

发布于 2024-10-20 22:03:27 字数 2147 浏览 2 评论 0原文

这是我的带有链表的堆栈实现。该程序运行正常。您对功能/性能/内存使用有什么意见吗?

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

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

int length(struct node * current)
{
int len = 0;
while(current)
{
len++;
current = current->next;
}
return len;
}

struct node* push(struct node* stack, int data)
{
    struct node * current = stack;
    struct node * newNode = (node*)(malloc(sizeof(node*)));
    newNode->data = data;
    newNode->next = NULL;
    //length(current);
    //single element case
    if(stack == NULL)
    {           
    stack = newNode;
    }
    else// multiple element case
    {
        while(current!=NULL)
        {
            if(current->next==NULL){
                current->next = newNode;
                break;
                }
            else
            {
            current = current->next;
            }
        }
    }

    return stack;
}

bool isemp(struct node * stack)
{
    if(stack == NULL)
    {
    printf("Stack is empty");
    return true;
    }
    else{
        return false;
    }
}

struct node * pop(struct node * stack)
{

struct node * current = stack;
struct node * previous = NULL;
bool isempty = false;
while(!isemp(stack)&& current)
    {
        if(current->next==NULL)
        {
        //delete previous;

            if(previous)
            {
            previous->next = NULL;
            printf("Popped element is %d ", current->data);
            current = current->next;
            }
            else if(length(stack)==1)
            {
            printf("Pop last element %d",stack->data);
            stack = NULL;
            current = NULL;
            }
        }

        else
        {
        previous = current;
        current = current->next;
        //stack = current;
        }
    }
    return stack;
}


void main()
{
    struct node * stack = NULL;
    int data =  1;
    int index = 5;
    while(index)
    {       
        stack = push(stack,data );
        data++;
        index--;
    }
    while(stack!=NULL)
    {
    stack = pop(stack);
    }

}

Here is my stack implementation with linked list. The program is working correctly. Would you have any comments in terms of functionality/ performance/ memory usage?

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

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

int length(struct node * current)
{
int len = 0;
while(current)
{
len++;
current = current->next;
}
return len;
}

struct node* push(struct node* stack, int data)
{
    struct node * current = stack;
    struct node * newNode = (node*)(malloc(sizeof(node*)));
    newNode->data = data;
    newNode->next = NULL;
    //length(current);
    //single element case
    if(stack == NULL)
    {           
    stack = newNode;
    }
    else// multiple element case
    {
        while(current!=NULL)
        {
            if(current->next==NULL){
                current->next = newNode;
                break;
                }
            else
            {
            current = current->next;
            }
        }
    }

    return stack;
}

bool isemp(struct node * stack)
{
    if(stack == NULL)
    {
    printf("Stack is empty");
    return true;
    }
    else{
        return false;
    }
}

struct node * pop(struct node * stack)
{

struct node * current = stack;
struct node * previous = NULL;
bool isempty = false;
while(!isemp(stack)&& current)
    {
        if(current->next==NULL)
        {
        //delete previous;

            if(previous)
            {
            previous->next = NULL;
            printf("Popped element is %d ", current->data);
            current = current->next;
            }
            else if(length(stack)==1)
            {
            printf("Pop last element %d",stack->data);
            stack = NULL;
            current = NULL;
            }
        }

        else
        {
        previous = current;
        current = current->next;
        //stack = current;
        }
    }
    return stack;
}


void main()
{
    struct node * stack = NULL;
    int data =  1;
    int index = 5;
    while(index)
    {       
        stack = push(stack,data );
        data++;
        index--;
    }
    while(stack!=NULL)
    {
    stack = pop(stack);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事和酒 2024-10-27 22:03:27

你的代码中有无数的问题。在push()方法中,你正在这样做:

      while(current!=NULL)

    {
        if(current->next==NULL){
            current->next = newNode; //This is also Wrong .You are not making Head pointing to newly created  node
            break;
            }
        else
        {
        current = current->next; //This is Wrong For Stack Implementation
        }
    }`

你实际上正在做的是将一个新创建的节点插入到链表的末尾。因此有点<通过链表实现队列。

There are myriad number of problems in your code .In push() method you are doing this:

      while(current!=NULL)

    {
        if(current->next==NULL){
            current->next = newNode; //This is also Wrong .You are not making Head pointing to newly created  node
            break;
            }
        else
        {
        current = current->next; //This is Wrong For Stack Implementation
        }
    }`

What you are actually doing is inserting a newly created node to the end of the linked list.Thus somewhat a queue implemented through linked list.

撕心裂肺的伤痛 2024-10-27 22:03:27

实现相同功能的一种方法是在链接中完成
http://codepad.org/dRMwSOuO
其中一个新项目被添加到列表的初始端并从同一端弹出。尽管边界条件没有正确处理。但时间复杂度会更好,因为您不必在列表中遍历直到结束。

One way of implementing the same is done in the link
http://codepad.org/dRMwSOuO
where a new item is added to the initial end of the list and poped from the same end .The boundary conditions are not handled properly though.Still the time complexity will be better as you dont have to traverse in the list till end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文