新人提问,我是初学者,请求帮助

cookieshi 2007-06-11 08:49:01

设有堆栈类,是链式存储的堆栈。另有一个链表类,其节点和堆栈的节点一样,完成两个类的下列方法.

堆栈类:
入栈:给定一个新值,将该新值形成一个节点入栈
出栈:如果堆栈为空,则返回NULL,否则返回一个指向堆栈栈顶节点的指针。
判断堆栈是否为空:如果栈为空,返回true, 否则返回false


链表类:
添加:给定一个数值,可以形成一个节点,增加到链表的尾部。
颠倒:把链表的尾部元素转换为表头,倒数第二变成第二…,返回值为一个链表。(使用堆栈类的方法加以实现)
打印:把链表的所有元素,从表头到尾,打印出来。

编写主程序:
1、调用链表的add方法,把1,2 ,3,4,5,6 ,7, 8, 9, 10追加到链表中。然后打印链表。
2、调用链表的“颠倒”方法,形成一个新的链表,然后再把得到的链表打印出来。

说明:
这是一道题
...全文
234 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yixiao386 2007-06-11
  • 打赏
  • 举报
回复
UP之
todototry 2007-06-11
  • 打赏
  • 举报
回复
mark之
huashizhixin 2007-06-11
  • 打赏
  • 举报
回复
#include <stdio.h>
#include"stdlib.h"

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

typedef struct Node Node ;
//void initial(Node *head);
Node *creat(int n);

Node *converse(Node *head); //逆序链表函数,参数为原来的头指针,返回值为逆序后的头指针

int main(void)
{

Node *head,*current;
int n;
printf("输入你要创建的接点数:\n");
scanf("%d",&n);
head=creat(n);
printf("the orignal order is :\n" );
current=head->next;
while (current!=NULL)
{
printf("the value is %d\n",current->data);
current=current->next;
}
head=converse(head);
printf("the converse order is :\n");
current=head;
while (current->next!=NULL)
{
printf("the value is : %d\n ",current->data);
current=current->next;
}
return 0;
}
Node * creat(int n) /*建立新的链表的函数*/
{
Node *p,*h,*s;
int i;
if((h=(Node *)malloc(sizeof(Node)))==NULL)
{
printf("不能分配内存空间!");
exit(0);
}
h->data=0;
h->next=NULL;
p=h;
for(i=0;i<n;i++)
{
if((s= (Node *) malloc(sizeof(Node)))==NULL)
{
printf("不能分配内存空间!");
exit(0);
}
p->next=s;
printf("请输入第%d个数",i+1);
scanf("%d",&(s->data));
s->next=NULL;
p=s;
}
return(h);
}

Node *converse(Node *_head)
{
Node *current,*current_conv,*current_conv_next,*head;

head=_head;
if(head==NULL) //如果为空链表,则返回NULL
return NULL;
else if (head->next==NULL) //如果链表只有一个节点,则返回节点本身
return head;
current=head->next; //通过头找到一个节点
current_conv=head->next; //把当前的current_conv移到链表的第二个节点
current_conv_next=head; //把current_conv_next指为原来的头
current_conv_next->next=NULL; //删除原来的头,并作为current_conv链表的尾
while(current->next!=NULL)
{
//此时,current和current_conv都在实际的同一个节点位置上
current=current->next; //找到原链表下一个节点,并置为当前节点
current_conv->next=current_conv_next; //将current_conv的下一个节点指为原表的前一个节点位置
current_conv_next=current_conv; //按原链表顺序移动链表
current_conv=current;
}
current_conv->next=current_conv_next;
head=current_conv;
return head;
}
星羽 2007-06-11
  • 打赏
  • 举报
回复
给你写了一个 栈的

#include "stdio.h"

template<class T>
class Stack
{
public :
Stack() {
size = 32;
data = new T[32];
cur = -1;
}

~Stack(){
if (data)
delete[] data;
}

void push(T& t) {

if (cur >= size - 1)
{
T* p = new T[size + 32];
memcpy(p, data, sizeof(T) * size);
size += 32;

if (data)
delete[] data;
data = p;
}

data[++cur] = t;
}

T* pop() {

if (cur == -1)
return NULL;

return &data[cur--];
}

private :
T* data;
int cur;
int size;

};

int main()
{
Stack<int> test;

for (int i= 0; i < 100; i++)
test.push(i);

int* p = test.pop();

while (p)
{
printf("%d\n", *p);

p = test.pop();
}

return 0;
}
huashizhixin 2007-06-11
  • 打赏
  • 举报
回复
#include"stdio.h"
#define MAXNUM 888
/* 定义栈结构 -顺序栈 */
typedef struct
{
int stack[MAXNUM]; /* 循序栈 */
int top; /* 栈指针 */
}STACK, * PSTACK;

/* 栈的初始化 */
int init_stack(PSTACK head)
{
head->top = -1;
return 0;
}

/* 入栈 */
int push_stack(PSTACK head, int x)
{
if(head->top >= MAXNUM-1)/* 栈满, 无法入 */
return 0;
head->stack[++head->top] = x;
return 1;
}

/* 出栈 */
int pop_stack(PSTACK head)
{
if(head->top < 0)/* 空栈 */
return 0;
return head->stack[head->top--];
}
void main()
{
int a;
STACK mystack;
//mystack=(STACK *)malloc(sizeof(STACK));
init_stack(&mystack);
printf("please input the number :");
scanf("%d",&a);
push_stack(&mystack,a);
int t=pop_stack(&mystack);
printf("%d",t);


}
  • 打赏
  • 举报
回复
std::stack
std::list

15,447

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧