信管117116李可欣数据结构实验二

1、(1)顺序栈的入栈和出栈:

#include< iostream >

usingnamespace std;

#definestack_size 10

intstack[stack_size];

inttop = 0;

voidInit_Stack()  //初始化顺序栈

{

top= -1;

}

 

voidpush_stack(int x)

{

if(top == stack_size)

cout<< "栈满!" << endl;

else

{

top++;

stack[top]= x;

}

}

voidpop_stack()

{

if(top == -1)

cout<< "栈下溢!" << endl;

else

{

top--;

}

}

 

 

intmain()

{

Init_Stack();    //初始化顺序栈

cout<< "请输入你想入站的元素个数:";  //顺序栈的建立

intn;

cin>> n;

cout<< "入站的元素依次为:" << endl;

for(int i = 0; i < n; i++)

{

intx;

cin>> x;

push_stack(x);

}

 

cout<< "请输入你想出站的元素个数:";   //顺序栈的出栈操作

intn1;

cin>> n1;

cout<< "出站的元素依次为:" << endl;

for(int i = 0; i < n1; i++)

{

pop_stack();

cout<< stack[top + 1] << " ";

}

cout<< endl;

return0;

}

(2)链栈的入栈和出栈:

#include <stdio.h>

#include <malloc.h>

typedef struct STACK

{

  int data;

  struct STACK *next;

}Stack;

 

//初始化

void InitStack(Stack *&s)

{

  

  s=(Stack *)malloc(sizeof(Stack));

  s->next=NULL;

}

 

//释放栈

void ClearStack(Stack *s)

{

 Stack *p=s->next;

 while(p!=NULL)

 {

  free(s);

  s=p;

  p=p->next;

 }

}

 

//判断栈是否为空

int StackEmpty(Stack *s)

{

 return(s->next==NULL);

}

 

int length(Stack *s)

{

 int i=0;

 Stack *p;

 p=s->next;

 while(p!=NULL)

 {

  i++;

  p=p->next;

 }

 return i;

}

 

//得到栈顶元素

int GetTop(Stack *S,int &e)

{

  if(S->next==NULL)

      return 0;

  e=S->next->data;

  return 1;

}

  

//入栈

void Push(Stack *s,int e)

 Stack *p;

    p=(Stack *)malloc(sizeof(Stack));

 p->data=e;

 p->next=s->next;

 s->next=p;

  

}

 

//出栈

int Pop(Stack *s,int &e)

{

    Stack *p;

 if(s->next==NULL)

 {

  printf("空栈...\n");

  return 0;

 }

    p=s->next;

 e=p->data;

    s->next=p->next;

 free(p);

 return 1;

}

 

//输出栈的元素

void OutputStack(Stack *s)

{

 Stack *p;

 p=s->next;

 if(s->next==NULL) {

     printf("空栈...\n");

     return;

 }

   while(p!=NULL) {

 printf("%4d",p->data);

 p=p->next;

   }

   printf("\n");

}

 

int main()

{  

      int e;

      Stack *s;

   printf("初始化栈:\n");

   InitStack(s);

   printf("依次进栈元素1,2,3,4,5\n");

   Push(s,1);

   Push(s,2);

   Push(s,3);

   Push(s,4);

   Push(s,5);

   printf("栈为%s\n",(StackEmpty(s)?"空":"非空"));

   printf("栈的长度:%d\n",length(s));

   printf("从栈顶到栈底的元素:");

   OutputStack(s);

   printf("出栈序列:");

   while(!StackEmpty(s))

   {

    Pop(s,e);

    printf("%d",e);

   }

   ClearStack(s);

   system("pause");

}

2、(1)顺序队列的入队和出队:

#include<stdio.h> 

#include<stdlib.h> 

 

#defineQINITSIZE 100 

#defineQINCRECEMENT 10 

#defineSTATUS int  

#definenull 0 

#defineQElemType int 

#defineOK 1 

#defineERROR 0 

 

 

typedefstruct QueueType{ 

QElemType*front; 

QElemType*rear; 

intqsize; 

}queue; 

 

STATUSq_init(queue *q) 

    q->front = (QElemType*)malloc(QINITSIZE*sizeof(QElemType)); 

    if(q->front == null)return ERROR; 

    q->rear = q->front; 

    q->qsize = QINITSIZE; 

    return OK; 

 

STATUSEnqueue(queue *q,int e) 

    if(q->rear - q->front >=QINITSIZE) 

    { 

        q->front = (QElemType*)realloc(q,(q->qsize + QINCRECEMENT)*sizeof(QElemType)); 

        q->rear = q->front; 

        q->qsize += QINCRECEMENT;  

    } 

    *q->rear++ = e; 

    return OK; 

STATUSDequeue(queue *q,int *e) 

    if(q->rear == q->front)returnERROR; 

    *e = *q->front++; 

    q->qsize--; 

    return OK; 

 

intmain() 

    queue q; 

    QElemType e,*p; 

    if(q_init(&q))printf("queueinitial OK!\n"); 

    Enqueue(&q,1); 

    Enqueue(&q,2); 

    Enqueue(&q,3); 

    Enqueue(&q,4); 

    Enqueue(&q,5); 

    p = q.front; 

    printf("队列:"); 

    while(p < q.rear) 

    printf("%d ",*p++); 

    Dequeue(&q,&e); 

    printf("删除元素为:"); 

    printf("%d\n",e); 

    p=q.front; 

    printf("删除队头:"); 

    while(p<q.rear) 

    printf("%d ",*p++); 

    printf("\n"); 

    return 0; 

}


(2)栈队列的入队和出队:


#include<iostream> 

 

usingnamespace std; 

typedefchar type; 

 

//链队结点定义 

typedefstruct node 

  type data; 

  struct node *next; 

}node,*QueueNode; 

 

//链表结构体定义 

typedefstruct Queue 

   QueueNode front;//队首指针 

   QueueNode rear;//队尾指针 

   int size;//队列元素个数 

}Queue,*pQueue; 

 

//初始化队列 

voidInitQueue(pQueue q) 

   q->front=(QueueNode)malloc(sizeof(node)); 

    if(NULL==q->front) 

    { 

        exit(-1); 

    } 

    q->front->next=NULL; 

   q->rear=(QueueNode)malloc(sizeof(node)); 

    if(NULL==q->rear) 

    { 

        exit(-1); 

    } 

    q->rear->next=NULL; 

    q->size=0; 

 

//入队操作 

voidEnterQueue(pQueue q,type data) 

    QueueNodenewNode=(QueueNode)malloc(sizeof(node)); 

    if(NULL==newNode) 

    { 

     cout<<"申请内存失败"<<endl; 

     exit(-1); 

    } 

    newNode->data=data; 

 

    q->size++; 

    if(NULL==q->front->next) //队列为空 

    { 

     q->front->next=newNode; 

     q->rear->next=newNode; 

     newNode->next=NULL; 

    } 

    else 

    { 

       q->rear->next->next=newNode; 

        newNode->next=NULL; 

        q->rear->next=newNode; 

    } 

 

//出队  返回原队头结点 

QueueNodeOutQueue(pQueue q) 

    if(NULL==q->front->next) 

    { 

        return NULL; 

    } 

     

    QueueNode temp =q->front->next; 

    q->front->next=temp->next; 

    q->size--; 

 

    return temp; 

//打印队列 

voidPrintQueue(pQueue q) 

    if(0==q->size) 

        exit(-1); 

 

    QueueNode temp=q->front->next; 

    while(NULL!=temp) 

    { 

        cout<<temp->data<<""; 

        temp=temp->next; 

    } 

 

    cout<<endl; 

int_tmain(int argc, _TCHAR* argv[]) 

    typea[5]={'a','b','c','d','e'}; 

    Queue q; 

    InitQueue(&q); 

     

    for(int i=0;i<5;i++) //入队 

        EnterQueue(&q,a[i]); 

    PrintQueue(&q); 

    OutQueue(&q);  //出队 

    PrintQueue(&q); 

    cout<<"队尾元素"<<q.rear->next->data<<endl; 

    return 0; 

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值