主题:实现顺序表,链表;栈和队列
顺序表:
#include<stdio.h>
#include<malloc.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
#define OK 1
#define EXIT_FAILURE 0
typedef int elemtype;
typedef struct
{
elemtype *elem;
int length;
int listsize;
}LNODE;
typedef LNODE *PTR;
void INIT_LIST(PTR l)
{
l->elem=(elemtype*)malloc(LIST_INIT_SIZE*sizeof(elemtype));
if(!l->elem)exit(EXIT_FAILURE);
l->length=0;
l->listsize=LIST_INIT_SIZE;
}
int INSERT_LIST(PTR l,int i,elemtype value)
{
elemtype *newbase;
elemtype *p;
if((i>(l->length)+1)||(i<1))exit(EXIT_FAILURE);
if((l->length)>=(l->listsize))
{
newbase=(elemtype*)realloc(l->elem,(LISTINCREMENT+LIST_INIT_SIZE)*sizeof(elemtype));
if(!newbase)exit(EXIT_FAILURE);
l->elem=newbase;
l->listsize=LIST_INIT_SIZE+LISTINCREMENT;
}
for(p=l->length+l->elem-1;p<=(l->elem)+i-1;p--)
{
*(p+1)=*p;
}
*(p-1)=value;
l->length++;
return OK;
}
elemtype DELETE_LIST(PTR l,int i)
{
elemtype *p;
elemtype *q;
elemtype value;
if((i<=0)||(i>l->length))exit(EXIT_FAILURE);
p=l->elem;
value=(*(p+i-1));
for(q=p+i-1;q<(p+l->length-1);q++)
{
*(q)=*(q+1);
}
l->length--;
return(value);
}
int LOCATE_LIST(PTR l,elemtype value)
{
elemtype *p;
elemtype *q;
int found=0;
p=l->elem;
for(q=p;q<=(p+l->length-1);q++)
{
if(*q==value)
{
found=q-p+1;
break;
}
}
return(found);
}
void main()
{
int i=1,num;
PTR L;
elemtype e,f;
INIT_LIST(L);
for(i=1;i<=10;i++)
{
printf("intput the data %d you what to insert,and the program will insert it to the list!!",i);
scanf("&e");
INSERT_LIST(L,i,e);
}
printf("input the data you wonder!!");
scanf("&f");
num=LOCATE_LIST(L,f);
if(!num) printf("not found(^_^!)");
else printf("the order of the data you wander is %d\n",num);
}
链表#include<stdio.h>
#include<malloc.h>
#define EXIT_FAILURE 0
#define OK 1
#define null 0
typedef int elemtype;
typedef struct LNODE
{
elemtype data;
struct LNODE *next;
}LNODE,*PTR;
void INIT_LIST(PTR l,int n)
{
LNODE *p;
int i;
elemtype elem;
l=(LNODE*)malloc(sizeof(LNODE));
l->next=null;
for(i=1;i<=n;i++)
{
printf("input what you want to insert for the number %d data",i);
scanf("&elem");
p=(LNODE*)malloc(sizeof(LNODE));
p->next=l->next;
p->data=elem;
l->next=p;
}
}
int INSERT_LIST(PTR l,int i,elemtype elem)
{
int j=1;
LNODE* p;
LNODE* q;
q=l;
while(q&&j<i)
{
q=q->next;
++j;
}
if(!q||j>i)exit(EXIT_FAILURE);
p=(LNODE*)malloc(sizeof(LNODE));
p->data=elem;
p->next=q->next;
q->next=p;
return OK;
}
elemtype DELETE_LIST(PTR l,int i)
{
elemtype value;
int j=1;
LNODE *p;
LNODE *q;
p=l;
while(p->next&&j<i)
{
p=p->next;
j++;
}
if(!(p->next)||(j>i))exit(EXIT_FAILURE);
q=p->next;
p->next=q->next;
value=q->data;
free(q);
return(value);
}
elemtype LOCATE_LIST (PTR l,elemtype value)
{
int j=1;
LNODE* p;
int found=0;
p=l->next;
while((p->data!=value)&&p->next)
{
p=p->next;
++j;
}
if(p->data==value)found=j;
return(found);
}
void main()
{
int e=10;
LNODE *t;
INIT_LIST(t,e);
}
栈
#include<stdio.h>
#include<malloc.h>
#define INIT_STACK_SIZE 10
#define STACKINCREMENT 10
#define EXIT_FAILURE 0
#define OK 1
typedef char elemtype;
typedef struct
{
elemtype* base;
elemtype* top;
int stacksize;
}STACK,*PTR;
int INIT_STACK(PTR l)
{
l->base=(elemtype *)malloc(INIT_STACK_SIZE * sizeof(elemtype));
if(!l->base)exit(EXIT_FAILURE);
l->stacksize=INIT_STACK_SIZE;
l->top=l->base;
return OK;
}
int PUSH(PTR l,elemtype value)
{
if((l->top-l->base)>=l->stacksize)
{
l->base=(elemtype*)relloc(l->base,(INIT_STACK_SIZE+STACKINCREMENT)*sizeof(elemtype));
if(!l->base)exit(EXIT_FAILURE);
l->top=l->base+l->stacksize;
l->stacksize+=STACKINCREMENT;
}
*l->top++=value;
return OK;
}
elemtype POP(PTR l)
{
elemtype e;
if(l->base==l->top)exit(EXIT_FAILURE);
e=*--l->top;
return(e);
}
int GETTOP(PTR l)
{
elemtype e;
if(l->base==l->top) return NULL;
e=*(l->top-1);
return e;
}
void main()
{
elemtype a,b,c;
PTR optr,opnd;
INIT_STACK(optr);
PUSH(optr,'#');
INITSTACK(opnd);
c=getchar();
while (c!='#'||GETTOP(optr)!='#')
{
if (!IN(c,OP))
{
PUSH(opnd,c);
c=getchar();
}
else
switch (PRECEDE(GETTOP(optr),c))
{
case '<':
PUSH(optr,c);
c=getchar();
break;
case '=':
POP(optr);
c=getchar();
break;
case '>':
theta=POP(optr);
a=POP(opnd);
b=POP(opnd);
PUSH(opnd,OPERATE(a,theta,b));
break;
}
}
}/*theta,OPERATE,IN*/代码中用了一个把表达式存储的例子,其中OPERATE,IN没有编写。
队列
#include<stdio.h>
#include<malloc.h>
#define EXIT_FAILURE 0
#define OK 1
typedef char elemtype;
typedef struct QNODE
{
elemtype data;
struct QNODE* next;
}QNODE,*QUEUEPTR;
typedef struct
{
QUEUEPTR front;
QUEUEPTR rear;
}LINKQUEUE,*LINKPTR;
int INIT_QUEUE (LINKPTR l)
{
l->front=l->rear=(QNODE*)malloc(sizeof(QNODE));
if (!l->front) exit(EXIT_FAILURE);
l->front->next=NULL;
l->front->data=0;
return OK;
}
int INSERT_QUEUE (LINKPTR l,elemtype value)
{
QUEUEPTR p;
p=(QNODE*) malloc (sizeof(QNODE));
if(!p) exit (EXIT_FAILURE);
l->rear->next=p;
l->rear=p;
p->data=value;
p->next=NULL;
(l->front->data)++;
return OK;
}
elemtype GETHEAD(LINKPTR l)
{
if(!(l->front==l->rear)) exit (EXIT_FAILURE);
return (l->front->next->data);
}
elemtype DELETE_QUEUE(LINKPTR l)
{
elemtype value;
QNODE *p;
if(!(l->front==l->rear)) exit(EXIT_FAILURE);
value=l->front->next->data;
p=l->front->next;
l->front->next=l->front->next->next;
l->front->data--;
if(l->rear==p)l->rear=l->front;
free(p);
return(value);
}
多多指教,希望给点积分啊。
#include<stdio.h>
#include<malloc.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
#define OK 1
#define EXIT_FAILURE 0
typedef int elemtype;
typedef struct
{
elemtype *elem;
int length;
int listsize;
}LNODE;
typedef LNODE *PTR;
void INIT_LIST(PTR l)
{
l->elem=(elemtype*)malloc(LIST_INIT_SIZE*sizeof(elemtype));
if(!l->elem)exit(EXIT_FAILURE);
l->length=0;
l->listsize=LIST_INIT_SIZE;
}
int INSERT_LIST(PTR l,int i,elemtype value)
{
elemtype *newbase;
elemtype *p;
if((i>(l->length)+1)||(i<1))exit(EXIT_FAILURE);
if((l->length)>=(l->listsize))
{
newbase=(elemtype*)realloc(l->elem,(LISTINCREMENT+LIST_INIT_SIZE)*sizeof(elemtype));
if(!newbase)exit(EXIT_FAILURE);
l->elem=newbase;
l->listsize=LIST_INIT_SIZE+LISTINCREMENT;
}
for(p=l->length+l->elem-1;p<=(l->elem)+i-1;p--)
{
*(p+1)=*p;
}
*(p-1)=value;
l->length++;
return OK;
}
elemtype DELETE_LIST(PTR l,int i)
{
elemtype *p;
elemtype *q;
elemtype value;
if((i<=0)||(i>l->length))exit(EXIT_FAILURE);
p=l->elem;
value=(*(p+i-1));
for(q=p+i-1;q<(p+l->length-1);q++)
{
*(q)=*(q+1);
}
l->length--;
return(value);
}
int LOCATE_LIST(PTR l,elemtype value)
{
elemtype *p;
elemtype *q;
int found=0;
p=l->elem;
for(q=p;q<=(p+l->length-1);q++)
{
if(*q==value)
{
found=q-p+1;
break;
}
}
return(found);
}
void main()
{
int i=1,num;
PTR L;
elemtype e,f;
INIT_LIST(L);
for(i=1;i<=10;i++)
{
printf("intput the data %d you what to insert,and the program will insert it to the list!!",i);
scanf("&e");
INSERT_LIST(L,i,e);
}
printf("input the data you wonder!!");
scanf("&f");
num=LOCATE_LIST(L,f);
if(!num) printf("not found(^_^!)");
else printf("the order of the data you wander is %d\n",num);
}
链表#include<stdio.h>
#include<malloc.h>
#define EXIT_FAILURE 0
#define OK 1
#define null 0
typedef int elemtype;
typedef struct LNODE
{
elemtype data;
struct LNODE *next;
}LNODE,*PTR;
void INIT_LIST(PTR l,int n)
{
LNODE *p;
int i;
elemtype elem;
l=(LNODE*)malloc(sizeof(LNODE));
l->next=null;
for(i=1;i<=n;i++)
{
printf("input what you want to insert for the number %d data",i);
scanf("&elem");
p=(LNODE*)malloc(sizeof(LNODE));
p->next=l->next;
p->data=elem;
l->next=p;
}
}
int INSERT_LIST(PTR l,int i,elemtype elem)
{
int j=1;
LNODE* p;
LNODE* q;
q=l;
while(q&&j<i)
{
q=q->next;
++j;
}
if(!q||j>i)exit(EXIT_FAILURE);
p=(LNODE*)malloc(sizeof(LNODE));
p->data=elem;
p->next=q->next;
q->next=p;
return OK;
}
elemtype DELETE_LIST(PTR l,int i)
{
elemtype value;
int j=1;
LNODE *p;
LNODE *q;
p=l;
while(p->next&&j<i)
{
p=p->next;
j++;
}
if(!(p->next)||(j>i))exit(EXIT_FAILURE);
q=p->next;
p->next=q->next;
value=q->data;
free(q);
return(value);
}
elemtype LOCATE_LIST (PTR l,elemtype value)
{
int j=1;
LNODE* p;
int found=0;
p=l->next;
while((p->data!=value)&&p->next)
{
p=p->next;
++j;
}
if(p->data==value)found=j;
return(found);
}
void main()
{
int e=10;
LNODE *t;
INIT_LIST(t,e);
}
栈
#include<stdio.h>
#include<malloc.h>
#define INIT_STACK_SIZE 10
#define STACKINCREMENT 10
#define EXIT_FAILURE 0
#define OK 1
typedef char elemtype;
typedef struct
{
elemtype* base;
elemtype* top;
int stacksize;
}STACK,*PTR;
int INIT_STACK(PTR l)
{
l->base=(elemtype *)malloc(INIT_STACK_SIZE * sizeof(elemtype));
if(!l->base)exit(EXIT_FAILURE);
l->stacksize=INIT_STACK_SIZE;
l->top=l->base;
return OK;
}
int PUSH(PTR l,elemtype value)
{
if((l->top-l->base)>=l->stacksize)
{
l->base=(elemtype*)relloc(l->base,(INIT_STACK_SIZE+STACKINCREMENT)*sizeof(elemtype));
if(!l->base)exit(EXIT_FAILURE);
l->top=l->base+l->stacksize;
l->stacksize+=STACKINCREMENT;
}
*l->top++=value;
return OK;
}
elemtype POP(PTR l)
{
elemtype e;
if(l->base==l->top)exit(EXIT_FAILURE);
e=*--l->top;
return(e);
}
int GETTOP(PTR l)
{
elemtype e;
if(l->base==l->top) return NULL;
e=*(l->top-1);
return e;
}
void main()
{
elemtype a,b,c;
PTR optr,opnd;
INIT_STACK(optr);
PUSH(optr,'#');
INITSTACK(opnd);
c=getchar();
while (c!='#'||GETTOP(optr)!='#')
{
if (!IN(c,OP))
{
PUSH(opnd,c);
c=getchar();
}
else
switch (PRECEDE(GETTOP(optr),c))
{
case '<':
PUSH(optr,c);
c=getchar();
break;
case '=':
POP(optr);
c=getchar();
break;
case '>':
theta=POP(optr);
a=POP(opnd);
b=POP(opnd);
PUSH(opnd,OPERATE(a,theta,b));
break;
}
}
}/*theta,OPERATE,IN*/代码中用了一个把表达式存储的例子,其中OPERATE,IN没有编写。
队列
#include<stdio.h>
#include<malloc.h>
#define EXIT_FAILURE 0
#define OK 1
typedef char elemtype;
typedef struct QNODE
{
elemtype data;
struct QNODE* next;
}QNODE,*QUEUEPTR;
typedef struct
{
QUEUEPTR front;
QUEUEPTR rear;
}LINKQUEUE,*LINKPTR;
int INIT_QUEUE (LINKPTR l)
{
l->front=l->rear=(QNODE*)malloc(sizeof(QNODE));
if (!l->front) exit(EXIT_FAILURE);
l->front->next=NULL;
l->front->data=0;
return OK;
}
int INSERT_QUEUE (LINKPTR l,elemtype value)
{
QUEUEPTR p;
p=(QNODE*) malloc (sizeof(QNODE));
if(!p) exit (EXIT_FAILURE);
l->rear->next=p;
l->rear=p;
p->data=value;
p->next=NULL;
(l->front->data)++;
return OK;
}
elemtype GETHEAD(LINKPTR l)
{
if(!(l->front==l->rear)) exit (EXIT_FAILURE);
return (l->front->next->data);
}
elemtype DELETE_QUEUE(LINKPTR l)
{
elemtype value;
QNODE *p;
if(!(l->front==l->rear)) exit(EXIT_FAILURE);
value=l->front->next->data;
p=l->front->next;
l->front->next=l->front->next->next;
l->front->data--;
if(l->rear==p)l->rear=l->front;
free(p);
return(value);
}
多多指教,希望给点积分啊。