/*
// 链表
#include "stdio.h"
#include "stdlib.h"
typedef struct node{
int value;
struct node *next;
}pnode;
pnode* create()
{
pnode *head,*last,*p;
int num;
head=last=NULL;
printf("input data:\n");
while((scanf("%d",&num))==1)
{
p=(pnode *)malloc(sizeof(pnode));
p->value=num;
p->next=NULL;
if(head==NULL)
{
head=last=p;
}else{
last->next=p;
last=last->next;
}
}
return head;
}
void search(pnode *head)
{
pnode *p;
p=head;
while (p) {
printf("%d",p->value);
p=p->next;
}
}
pnode * insert(pnode *head)
{
pnode *p,*q = NULL;
p=head;
int data;
int a=2;
getchar();
printf("input insert data:");
scanf(" %d",&data);
pnode *new=(pnode *)malloc(sizeof(pnode));
new->value=data;
if(head==NULL)
{
head=new;
new->next=NULL;
}
if(head->value==a)
{
new->next=head;
head=new;
}else{
while (p->value!=a&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->value==a)
{
new->next=p;
q->next=new;
}else{
q->next=new;
new->next=NULL;
}
}
return head;
}
pnode *delete(pnode *head)
{
pnode *p,*q;
p=head;
q=NULL;
int data;
printf("input delete data:");
scanf(" %d",&data);
if(head!=NULL)
{
if(head->value==data)
{
free(head);
head=NULL;
}else{
while (p->value!=data&&p->next!=NULL) {
q=p;
p=p->next;
}
if(p->value==data)
{
q->next=p->next;
free(p);
}else{
printf("the data is not exit in the node\n");
}
}
}else{
printf("the node is null!");
}
return head;
}
int main()
{
pnode *head;
head=create();
search(head);
printf("\n");
insert(head);
search(head);
printf("\n");
delete(head);
search(head);
return 0;
}
*/
/*
//link队列
#include "stdio.h"
#include "stdlib.h"
typedef struct Queue{
int data;
struct Queue *next;
}Queue;
typedef struct pointer{
Queue *front;
Queue *rear;
}Qpointer;
void creatQueue(Qpointer *qp)
{
Queue *que=(Queue *)malloc(sizeof(Queue));
que->next=NULL;
qp->rear=qp->front=que;
printf("createQueue success!\n");
}
int isEmpty(Qpointer *qp)
{
if(qp->front==qp->rear)
{
return 1;
}
return 0;
}
void QueuePush(Qpointer *qp)
{
int data;
printf("input push data:\n");
while ((scanf("%d",&data))==1) {
Queue *q=NULL;
q=(Queue *)malloc(sizeof(Queue));
if(q==NULL)
{
printf("malloc failed!\n");
exit(-1);
}
q->data=data;
q->next=NULL;
qp->rear->next=q;
qp->rear=q;
}
}
void QueuePop(Qpointer *qp)
{
Queue *q;
int data;
if(isEmpty(qp))
{
printf("Queue is null!\n");
exit(-1);
}
q=qp->front->next;
data=q->data;
qp->front->next=q->next;
if(qp->rear==q)
{
qp->rear=qp->front;
}
printf("the pop data is %d\n",q->data);
free(q);
q=NULL;
}
void freeQueue(Qpointer *qp)
{
Queue *q=qp->front->next;
while (q!=NULL) {
q=qp->front->next;
QueuePop(qp);
}
qp=NULL;
}
void selectQueue(Qpointer *qp)
{
Queue *q;
if(isEmpty(qp))
{
printf("queue is null!\n");
}else{
q=qp->front->next;
printf("the data of queue:\n");
while (q) {
printf("%d\t",q->data);
q=q->next;
}
}
printf("\n");
}
int main()
{
Qpointer *qp;
qp=(Qpointer *)malloc(sizeof(Qpointer));
creatQueue(qp);
selectQueue(qp);
QueuePush(qp);
selectQueue(qp);
QueuePop(qp);
selectQueue(qp);
freeQueue(qp);
selectQueue(qp);
return 0;
}
*/
/*
//循环队列
#include "stdio.h"
#include "stdlib.h"
#define QueueNum 10
typedef int QueElemType;
typedef struct Queue{
QueElemType *base;
int front;
int rear;
}SqQueue;
void initQueue(SqQueue *que)
{
que->base=(QueElemType *)malloc(sizeof(QueElemType)*QueueNum);
if(!que->base)
{
printf("init Queue failed!\n");
exit(-1);
}else{
que->front=que->rear=0;
printf("init Queue success\n");
}
}
int isEmpty(SqQueue *que)
{
if(que->front==que->rear)
{
return 1;
}
return 0;
}
int isFull(SqQueue *que)
{
if((que->rear+1)%QueueNum==que->front)
{
return 1;
}
return 0;
}
void pushQueue(SqQueue *que)
{
QueElemType element;
printf("input push element:\n");
while ((scanf("%d",&element))==1) {
if (isFull(que)) {
printf("the queue is full!\npush failed!\n");
break;
}else{
*(que->base+que->rear)=element;
que->rear=(que->rear+1)%QueueNum;
}
}
}
void visitQueue(SqQueue *que)
{
if(isEmpty(que))
{
printf("the Queue is null!\n");
}else{
printf("the element of Queue is:\n");
int n=que->front;
while (n!=que->rear) {
printf("%d\t",*(que->base+n));
n=(n+1)%QueueNum;
}
}
printf("\n");
}
void popQueue(SqQueue *que)
{
QueElemType element;
if(isEmpty(que))
{
printf("the Queue is null!\n");
}else{
element=que->base[que->front];
que->front=(que->front+1)%QueueNum;
}
printf("the pop element is %d.\n",element);
}
void freeQueue(SqQueue *que)
{
while (que->front!=que->rear) {
popQueue(que);
}
}
int main()
{
SqQueue *p;
initQueue(p);
pushQueue(p);
visitQueue(p);
popQueue(p);
visitQueue(p);
freeQueue(p);
visitQueue(p);
return 0;
}
*/
#include "stdio.h"
#include "stdlib.h"
#define StackNum 10
typedefint ElementType;
typedef struct{
ElementType *base;
ElementType *top;
int size;
}Stack;
void initStack(Stack *st)
{
st->base=(ElementType *)malloc(sizeof(ElementType)*StackNum);
if(!st->base)
{
printf("init Stack failed!\n");
exit(-1);
}
st->top=st->base;
st->size=StackNum;
printf("init Stack success!\n");
}
int isEmpty(Stack *st)
{
if(st->top==st->base)
{
return 1;
}
return 0;
}
void pushStack(Stack *st)
{
ElementType element;
if((st->top-st->base)>=(st->size))
{
st->base=(ElementType *)realloc(st->base, (st->size+StackNum)*sizeof(ElementType));
if(!st->base)
{
printf("push stack failed!\nrealloc errror!\n");
exit(-1);
}
st->top=st->base+st->size;
st->size+=StackNum;
}
printf("input stack element:\n");
while ((scanf("%d",&element))==1) {
*(st->top)=element;
st->top++;
}
}
void popStack(Stack *st)
{
ElementType element;
if(isEmpty(st))
{
printf("the stack is null!\n");
}else{
element=*--st->top;
st->size--;
}
}
void visitStack(Stack *st)
{
int i=0;
if(!isEmpty(st))
{
for(i=0;i<st->size;i++)
{
printf("%d\t",*(st->base+i));
}
}
printf("\n");
}
int main()
{
Stack *st;
initStack(st);
pushStack(st);
visitStack(st);
popStack(st);
visitStack(st);
return 0;
}