2008秋季-计算机软件基础-0917课堂用例(1)
链栈:
参见:http://www.cnblogs.com/emanlee/archive/2007/09/12/890647.html
#include<stdio.h>
#include<stdlib.h>
struct stacknode
{
int data;
struct stacknode *next;
};
//
struct stacknode * InitialLinkList ()
{
struct stacknode * head;
head=(struct stacknode *)
malloc(sizeof(struct stacknode ));//
head->next=NULL;
return head;
}
//入栈
void PushIntoStack(struct stacknode * head, int value)
{
struct stacknode * p;
p=(struct stacknode *)malloc(sizeof(struct stacknode ));
p->data=value;
p->next=head->next;
head->next=p;
}
void PopFromStack(struct stacknode * head)
{
struct stacknode * p;
if(head->next==NULL)
printf("Pop Failed \n");
else
{
p=head->next;
head->next=p->next;
free(p);
}
}
void ShowStackElement(struct stacknode *head)
{
struct stacknode * p;
p=head->next;
printf("\n显示栈中元素:\n");
while(p!=NULL)
{
printf(" %d ",p->data);
p=p->next;
}
}
void main()
{
struct stacknode *head;
head=InitialLinkList();
PushIntoStack(head,1);
ShowStackElement(head);
PushIntoStack(head,2);
ShowStackElement(head);
PushIntoStack(head,3);
ShowStackElement(head);
PopFromStack(head);
ShowStackElement(head);
PopFromStack(head);
ShowStackElement(head);
PopFromStack(head);
ShowStackElement(head);
PopFromStack(head);
ShowStackElement(head);
PopFromStack(head);
ShowStackElement(head);
}
#include<stdlib.h>
struct stacknode
{
int data;
struct stacknode *next;
};
//
struct stacknode * InitialLinkList ()
{
struct stacknode * head;
head=(struct stacknode *)
malloc(sizeof(struct stacknode ));//
head->next=NULL;
return head;
}
//入栈
void PushIntoStack(struct stacknode * head, int value)
{
struct stacknode * p;
p=(struct stacknode *)malloc(sizeof(struct stacknode ));
p->data=value;
p->next=head->next;
head->next=p;
}
void PopFromStack(struct stacknode * head)
{
struct stacknode * p;
if(head->next==NULL)
printf("Pop Failed \n");
else
{
p=head->next;
head->next=p->next;
free(p);
}
}
void ShowStackElement(struct stacknode *head)
{
struct stacknode * p;
p=head->next;
printf("\n显示栈中元素:\n");
while(p!=NULL)
{
printf(" %d ",p->data);
p=p->next;
}
}
void main()
{
struct stacknode *head;
head=InitialLinkList();
PushIntoStack(head,1);
ShowStackElement(head);
PushIntoStack(head,2);
ShowStackElement(head);
PushIntoStack(head,3);
ShowStackElement(head);
PopFromStack(head);
ShowStackElement(head);
PopFromStack(head);
ShowStackElement(head);
PopFromStack(head);
ShowStackElement(head);
PopFromStack(head);
ShowStackElement(head);
PopFromStack(head);
ShowStackElement(head);
}