C 用链表实现堆栈

C 用链表实现堆栈,c,stack,linked-list,C,Stack,Linked List,下面是我用链表实现的堆栈。程序运行正常。您对功能/性能/内存使用有何评论 #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 = curr

下面是我用链表实现的堆栈。程序运行正常。您对功能/性能/内存使用有何评论

#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);
    }

}
#包括
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
};
整数长度(结构节点*当前)
{
int len=0;
while(当前)
{
len++;
当前=当前->下一步;
}
回程透镜;
}
结构节点*推送(结构节点*堆栈,int数据)
{
结构节点*当前=堆栈;
结构节点*newNode=(节点*)(malloc(sizeof(节点*));
新建节点->数据=数据;
newNode->next=NULL;
//长度(电流);
//单元素盒
if(stack==NULL)
{           
stack=newNode;
}
else//多元素情况
{
while(当前!=NULL)
{
如果(当前->下一步==NULL){
当前->下一步=新节点;
打破
}
其他的
{
当前=当前->下一步;
}
}
}
返回栈;
}
布尔isemp(结构节点*堆栈)
{
if(stack==NULL)
{
printf(“堆栈为空”);
返回true;
}
否则{
返回false;
}
}
结构节点*弹出(结构节点*堆栈)
{
结构节点*当前=堆栈;
结构节点*previous=NULL;
bool isempty=false;
while(!isemp(堆栈)和当前)
{
如果(当前->下一步==NULL)
{
//删除以前的;
如果(先前)
{
上一个->下一个=空;
printf(“弹出的元素是%d”,当前->数据);
当前=当前->下一步;
}
else if(长度(堆栈)==1)
{
printf(“弹出最后一个元素%d”,堆栈->数据);
stack=NULL;
电流=零;
}
}
其他的
{
先前=当前;
当前=当前->下一步;
//堆栈=当前;
}
}
返回栈;
}
void main()
{
结构节点*stack=NULL;
int数据=1;
int指数=5;
while(索引)
{       
堆栈=推送(堆栈、数据);
数据++;
索引--;
}
while(堆栈!=NULL)
{
stack=pop(stack);
}
}

您的代码中存在大量问题。在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
        }
    }`

实际上,您正在将一个新创建的节点插入到链表的末尾。因此,在某种程度上,通过链表实现了一个队列

在您的代码中存在大量问题。。在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
        }
    }`

实际上,您正在将一个新创建的节点插入到链表的末尾。因此,在某种程度上,通过链表实现的队列是在链接中实现的

在列表的起始端添加一个新项目,并从同一端弹出。虽然边界条件没有得到正确处理。但是时间复杂度会更好,因为您不必在列表中遍历到最后。

在链接中完成了相同的实现方法


将新项添加到列表的起始端,并从同一端弹出。但边界条件处理不正确。但时间复杂度会更好,因为您不必在列表中遍历到最后。

不可读。。。修复你的代码格式。这个问题不是更适合你吗?目前还没有“属于codereview”…@andersoj你有足够的代表来帮助新来者进行代码格式设置;)@rlb.usa:有代表就有辨别力无法阅读。。。修复你的代码格式。这个问题不是更适合你吗?目前还没有“属于codereview”…@andersoj你有足够的代表来帮助新来者进行代码格式设置;)@rlb.usa:有代表就有辨别力相对长度单位。。在推送中,我在尾部添加新节点,因此当我弹出时,我也从尾部移除节点。我的想法是,堆栈操作的主要内容是在同一端添加和删除节点。你会如何编写堆栈实现的代码,你会喜欢从头部插入和删除节点吗?@user457660是的,我肯定会在开始时插入节点,因为这只是计算上的O(1)。此外,pop()也会是O(1)。如果有帮助,请接受这个答案。em。。在推送中,我在尾部添加新节点,因此当我弹出时,我也从尾部移除节点。我的想法是,堆栈操作的主要内容是在同一端添加和删除节点。你会如何编写堆栈实现的代码,你会喜欢从头部插入和删除节点吗?@user457660是的,我肯定会在开始时插入节点,因为这只是计算上的O(1)。pop()也会是O(1)。如果有帮助,请接受这个答案。