回 帖 发 新 帖 刷新版面

主题:[讨论]分享部分个人数据结构(c语言)的学习过程中源程序

都是一些把伪C改写成C的,
1.栈的操作(顺序表)内含一个课本上的简单应用(10进制->八进制)
#define STACK_INIT_SIZE 100
#define STACKINCREMENT  10
#include <stdlib.h>
typedef int status;
#include <stdio.h>
typedef struct sqstack
{
  int *base;
  int *top;
  int stacksize;

}sqstack;
status initstack(sqstack *s)
{
  s->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
  if(!s->base) return 0;
  s->top=s->base;
  s->stacksize=STACK_INIT_SIZE;
  return 1;

}

status push(sqstack *s,int e)
{
    if(s->top-s->base>=s->stacksize)
        {
        s->base=(int*)realloc(s->base, (s->stacksize+STACKINCREMENT)*sizeof(int));
            if(s->base==NULL) return 0;
        s->top=s->base+s->stacksize;
        s->stacksize+=STACKINCREMENT;
        }
    *(s->top)=e;
    s->top++;
    return 1;
}
status pop(sqstack *s,int e)
{

  if(s->top==s->base) return 0;
  e=*(s->top-1);
  s->top--;
  return e;
}
void main()
{  int a,e,N;
   sqstack *s;
   s=(sqstack *)malloc(sizeof(sqstack));

   a=initstack(s);
   scanf("%d",&N);
       while(N)
       {
         a=push(s,N%8);
         N=N/8;

       }
  while(s->top!=s->base)
  {
    e=pop(s,e);
    printf("%d",e);
  
  }
getch();
printf("\n");
}
2.栈的操作,验证括号是否匹配
#define STACK_INIT_SIZE 100
#define STACKINCREMENT  10
#include <stdlib.h>
typedef int status;
#include <stdio.h>
#include "string.h"
#define ERROR 1
typedef struct sqstack
{
  char *base;
  char *top;
  int stacksize;
}sqstack;
status initstack(sqstack *s)
{
  s->base=(char *)malloc(5*sizeof(char));
  if(!s->base) return 0;
  s->top=s->base;
  s->stacksize=5;
  return 1;

}

status push(sqstack *s,char *e)
{
    if(s->top-s->base>=s->stacksize)
        {
        s->base=(int*)realloc(s->base, (s->stacksize+STACKINCREMENT)*sizeof(int));
            if(s->base==NULL) return 0;
        s->top=s->base+s->stacksize;
        s->stacksize+=STACKINCREMENT;
        }
    *s->top=*e;
    s->top++;
    return 1;
}
int pop(sqstack *s,char *e)
{

  if(s->top==s->base) return 0;
  *e=*(s->top-1);
  s->top--;
  return 1;
}
status stackempty(sqstack *s)
{
  return (s->top==s->base);

}
int main()
{  int a,N;
   char c,str[]={"{()(())}"},*p=str;
   sqstack *s;
   system("cls");
   s=(sqstack *)malloc(sizeof(sqstack));
   initstack(s);
for(p=str;*p;p++)
  {
    if(*p=='('||*p=='['||*p=='{') push(s,p);
    else if(*p==')'||*p==']'||*p=='}')
    {
      if(stackempty(s)) return ERROR;
      pop(s,&c);
      if(*p==')'&&c!='(') return ERROR;
      if(*p==']'&&c!='[') return ERROR;
      if(*p=='}'&&c!='{') return ERROR; //必须与当前栈顶括号匹配
    }
  }
  if(!stackempty(s)) return ERROR;
  printf("correct");
  getch();
}
3。链表队列的操作
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
typedef int status;
typedef struct qnode
{
   int data;
   struct qnode *next;
}qnode,*queueptr;
typedef struct linkqueue
{
  queueptr front;
  queueptr rear;
}linkqueue;
status initqueue (linkqueue *Q)
{
  Q->front=Q->rear=(queueptr)malloc(sizeof(qnode));
  if(!Q->front) return 0;
  Q->front->next=NULL;
   return 1;
}
status destroyqueue(linkqueue *Q)
{
  while(Q->front)
  {
    Q->rear=Q->front->next;
    free(Q->front);
    Q->front=Q->rear;
    }
  return 1;   
}
status enqueue(linkqueue *Q,int *e)
{
  queueptr p;
  p=(queueptr)malloc(sizeof(qnode));
  if(!p) return 0;
  p->data=*e;
  p->next=NULL;
  Q->rear->next=p;
  Q->rear=p;
  return 1;
}
status dequeue(linkqueue *Q,int *e)
{
queueptr p;
if(Q->front==Q->rear) return 0;
p=Q->front->next;
Q->front->next=p->next;
*e=p->data;
if(Q->rear==p) Q->rear=Q->front;
free(p);
return 1;
}
status emptyqueue(linkqueue *Q)
{

return (Q->front==Q->rear);

}
void main()
{
  int a[10]={1,2,3,4,5,6,7,8,9,10},e,i;
  linkqueue *Q;
  queueptr p;
  Q=(linkqueue *)malloc(sizeof(linkqueue));
  if(!Q) return;
  initqueue (Q);
  for(i=0;i<10;i++)
     enqueue(Q,&a[i]);
  p=Q->front->next;
  while(p!=NULL)
  {
    printf("%d   ",p->data);
        p=p->next;
  
  }
  
  printf("\n");
  
  for(i=0;i<10;i++)
  {
      dequeue(Q,&e);
      printf("%d  ",e);
  }
   printf("\n%d",emptyqueue(Q));
  
  getch();
}
4.顺序表队列的操作(采用了标志位来判断是否队满)
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
typedef int status;
#define maxsize 6
typedef struct sqqueue
{
  int *base;
  int front;
  int rear;
  int size;
}sqqueue;
status initsqqueue(sqqueue *Q)
{
  Q->base=(int *)malloc(maxsize*sizeof(int));
  if(!Q->base) return 0;
  Q->front=Q->rear=Q->size=0;
  return 1;
}
int queuelength(sqqueue *Q)
{

  return (Q->rear-Q->front+maxsize)%maxsize;
}
status enqueue(sqqueue *Q,int *e)
{
  if(Q->size==maxsize) return 0;
  Q->base[Q->rear]=*e;
  Q->rear=(Q->rear+1)%maxsize;
  (Q->size)++;
  return 1;
}
status dequeue (sqqueue *Q,int *e)
{
if(!Q->size) return 0;
*e=Q->base[Q->front];
Q->front=(Q->front+1)%maxsize;
(Q->size--);
return 1;
}
status destroysqeue(sqqueue *Q)
{
  Q->front=Q->rear=0;
  return 1;
}
void main()
{ int a[maxsize]={1,2,3,4,5,6},i,e;
  sqqueue *Q;
  Q=(sqqueue*)malloc(sizeof(sqqueue));
  initsqqueue(Q);
  for(i=0;i<maxsize;i++)
    enqueue(Q,&a[i]);
  for(i=0;i<maxsize;i++)
     printf("%d ",Q->base[i]);
  printf("\n");
  
  for(i=0;i<maxsize;i++)
    {
      dequeue(Q,&e);
      printf("%d  ",e);  
    }
   getch();
}

回复列表 (共4个回复)

沙发


谢谢了

板凳


怎么没认顶啊
顶下~~ [em1]

3 楼

谢谢,我想学数据结构,但是教材上只给了算法,没有具体实现。

4 楼

呵呵,谢谢你让大家来一起学习哦!

我来回复

您尚未登录,请登录后再回复。点此登录或注册