堆栈与链表的实现
这是我的带有链表的堆栈实现。该程序运行正常。您对功能/性能/内存使用有什么意见吗?
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的代码中有无数的问题。在push()方法中,你正在这样做:
你实际上正在做的是将一个新创建的节点插入到链表的末尾。因此有点<通过链表实现队列。
There are myriad number of problems in your code .In push() method you are doing this:
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.
实现相同功能的一种方法是在链接中完成
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.