1、顺序结构下实现循环队列
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#define ElemType int
#define MAXSIZE 9
typedef struct Queue //队列节点结构体
{
ElemType *base; //节点数据域
int front; //头指针
int rear; //尾指针
}Queue;
void InitQueue(Queue *Q) //初始化
{
Q->base = (ElemType*)malloc(sizeof(ElemType)*MAXSIZE);
assert(Q->base != NULL);
Q->front = Q->rear = 0;
}
void EnQueue(Queue *Q,ElemType x) //进队操作
{
if(((Q->rear+1)%MAXSIZE) == Q->front)
return;
Q->base[Q->rear] = x;
Q->rear = (Q->rear+1)%MAXSIZE;
}
void DeQueue(Queue *Q) //出队操作
{
if(Q->front == Q->rear)
return;
ElemType p = Q->base[Q->front];
Q->front++;
}
ElemType GetHead(Queue *Q) //获取队头元素
{
if(Q->front == Q->rear)
return 0;
ElemType p = Q->base[Q->front];
//Q->front++;
return p;
}
int Length(Queue *Q) //获取队列长度
{
int len = Q->rear - Q->front;
return len;
}
void ClearQueue(Queue *Q) //清除队列
{
Q->front = Q->rear = 0;
}
void DestroyQueue(Queue *Q) //摧毁队列
{
free(Q->base);
Q->base = NULL;
}
void ShowQueue(Queue *Q) //输出队列
{
for(int i=Q->front;i!=Q->rear;)
{
printf("%d ",Q->base[i]);
i = (i+1)%MAXSIZE;
}
printf("\n");
}
int main()
{
Queue Q;
InitQueue(&Q);
for(int i=1;i<10;i++)
{
EnQueue(&Q,i);
}
ShowQueue(&Q);
DeQueue(&Q);
EnQueue(&Q,10);
ShowQueue(&Q);
DeQueue(&Q);
EnQueue(&Q,11);
ShowQueue(&Q);
//printf("Length = %d",Length(&Q));
return 0;
}
2、基于顺寻存储串结构相关操作的实现(主函数中操作可自行更改)
#include<stdio.h>
#include<string.h>
#define MAXSTRLEN 20
#define u_char unsigned char
typedef u_char SString[MAXSTRLEN+1];
void InitString(SString S) //初始化字符串
{
S[0] = '\0';
}
void StrAssign(SString S,char *str) //赋值字符串操作
{
int len = strlen(str);
for(int i = 0;i<len;i++)
{
S[i] = str[i];
}
S[len] = '\0';
}
int StrLength(SString S) //求字符串长度
{
int len = 0;
while(*S != '\0')
{
len++;
S++;
}
return len;
}
void StrCopy(SString S,SString T) //复制串操作
{
int len = StrLength(T);
for(int i=0;i<len;i++)
{
S[i] = T[i];
}
S[len] = '\0';
}
bool StrEmpty(SString S) //字符串结构判空操作
{
return S[0] == '\0';
}
int StrCompare(SString S,SString T) //字符串比较操作
{
int result = 0;
while(*S != '\0' || *T != '\0')
{
result = *S - *T;
if(result != 0)
break;
S++;
T++;
}
if(result > 0)
result = 1;
else if(result <0)
result = -1;
return result;
}
void StrConcat(SString T,SString s1,SString s2) //字符串连接操作
{
int len1 = StrLength(s1);
int len2 = StrLength(s2);
int i;
/*
//此处代码较为繁琐,简化后代码在下方
if(len1+len2<MAXSTRLEN)
{
for(i=0;i<len1;i++)
{
T[i] = s1[i];
}
for(int j=0;j<len2;j++)
{
T[i+j] = s2[j];
}
T[len1+len2] = '\0';
}
else if (len1<MAXSTRLEN)
{
for(i=0;i<len1;i++)
{
T[i] = s1[i];
}
for(int j=0;j<MAXSTRLEN-len1;j++)
{
T[i+j] = s2[j];
}
T[MAXSTRLEN] = '\0';
}
else
{
for(int j=0;j<len1;++j)
{
T[j] = s1[j];
}
T[MAXSTRLEN] = '\0';
}
*/
//此处为上述代码简化后,减少了代码长度
int lenj = 0,length = 0;
if(len1+len2<MAXSTRLEN)
{
length = len1+len2;
lenj = len2;
}
else if(len1 < MAXSTRLEN){
length = MAXSTRLEN;
lenj = MAXSTRLEN-len1;
}
else{
length = len1;
lenj =0;
}
for(i=0;i<len1;i++)
{
T[i] = s1[i];
}
for(int j=0;j<lenj;j++)
{
T[i+j] = s2[j];
}
T[length] = '\0';
}
void SubString(SString S,SString sub,int pos,int len) //在S字符串中截取子串
{
//求子串
int s_len = StrLength(S);
if(pos < 0 || pos>=s_len || len <0 || len > s_len)
return ;
int j=pos;
for(int i=0;i<len;i++)
{
sub[i] = S[j+i];
}
sub[len] = '\0';
}
//void StrIndex(SString S,SString T,int pos);
//void StrReplace(SString S,SString T,SString V)
//{}
void StrInsert(SString S,int pos,SString T) //在字符串中某位置插入子串
{
int s_len = StrLength(S);
int t_len = StrLength(T);
int i;
/*
//
if(s_len+t_len<=MAXSTRLEN)
{
for(i=s_len-1;i>=pos;--i)
{
S[i+t_len] = S[i];
}
int j = pos;
for(i=0;i<t_len;++i)
{
S[j+i] = T[i];
}
S[s_len+t_len] = '\0';
}
else if(s_len<MAXSTRLEN)
{
t_len = MAXSTRLEN - s_len;
for(i=s_len-1;i>=pos;--i)
{
S[i+t_len] = S[i];
}
int j = pos;
for(i=0;i<t_len;++i)
{
S[j+i] = T[i];
}
S[s_len+t_len] = '\0';
}
*/
if(s_len+t_len<=MAXSTRLEN)
{
t_len = StrLength(T);
}
else if(s_len+t_len>MAXSTRLEN)
{
t_len = MAXSTRLEN - s_len;
}
for(i=s_len-1;i>=pos;--i)
{
S[i+t_len] = S[i];
}
int j = pos;
for(i=0;i<t_len;++i)
{
S[j+i] = T[i];
}
S[s_len+t_len] = '\0';
}
void StrDelete(SString S,int pos,int len) //删除字串
{
int s_len = StrLength(S);
if(pos+len>MAXSTRLEN)
{
printf("数据输入不合法!!!");
return;
}
for(int i = pos;i<s_len;i++)
{
S[i] = S[i+len];
}
S[s_len-len] = '\0';
}
void StrClear(SString S) //清空字符串
{
S[0] = '\0';
}
void PrintString(SString S) //输出字符串
{
printf("%s \n",S);
}
int main()
{
SString S;
InitString(S);
//SString T;
//InitString(T);
//char *str = "abcdrh";
StrAssign(S,"abcdefhjkl");
//删除子串操作
StrDelete(S,2,3);
PrintString(S);
//插入子串
//SString T;
//InitString(T);
//StrAssign(T,"jjj");
//StrInsert(S,1,T);
//PrintString(S);
//截取子串
//SString sub;
//InitString(sub);
//SubString(S,sub,4,2);
//PrintString(sub);
//连接两个字符串部分
//SString Y;
//InitString(Y);
//StrConcat(Y,S,T);
//PrintString(Y);
//比较字符串部分
//int res = StrCompare(S,T);
//printf("%d \n",res);
//PrintString(S);
//复制字符串部分
//StrCopy(T,S);
//PrintString(T);
return 0;
}