本文目录一览:
C语言.实现一个顺序存储的循环队列。
typedef struct _queue
{
int head;
int tail;
int len;
int capacity;
int * arr;
}QUEUE, *PQUEUE;
int Queue_Create(QUEUE ** que, int _len)
{
if (_len 0 || _len 10000)
{
printf("len is err !\n");
return -1;
}
PQUEUE temp = (PQUEUE)malloc(sizeof(QUEUE));
if (NULL == temp)
{
printf("malloc err !\n");
return -2;
}
memset(temp, 0, sizeof(QUEUE));
temp-arr = (int *)malloc(sizeof(int)* _len);
if (NULL == temp-arr)
{
printf("malloc err !\n");
return -3;
}
memset(temp-arr, 0, sizeof(int)* _len);
temp-head = 0;
temp-tail = 0;
temp-len = 0;
temp-capacity = _len;
*que = temp;
return 0;
}
int Queue_En(PQUEUE que, int num)
{
if (NULL == que)
{
printf("que is NULL\n");
return -1;
}
que-arr[que-tail] = num;
que-len++;
que-tail = (que-tail + 1) % que-capacity;
return 0;
}
int Queue_IsFull(PQUEUE que)
{
if (NULL == que)
{
printf("que is NULL\n");
return 0;
}
if ((que-tail + 1) % que-capacity == que-head)
{
return 1;
}
return 0;
}
int Queue_Empth(PQUEUE que)
{
if (NULL == que)
{
printf("que is NULL\n");
return 0;
}
if (0 == que-len)
{
return 1;
}
return 0;
}
int Queue_Out(PQUEUE que, int * num)
{
if (Queue_Empth(que))
{
return -1;
}
*num = que-arr[que-head];
que-len--;
que-head = (que-head + 1) % que-capacity;
return 0;
}
int Queue_Free(PQUEUE * que)
{
if (NULL == que || NULL == *que)
{
return -1;
}
PQUEUE temp = *que;
if (temp-arr != NULL)
{
free(temp-arr);
}
free(temp);
*que = NULL;
printf("free success !\n");
return 0;
}
void main()
{
PQUEUE queue = NULL;
int ret = Queue_Create(queue, 10);
if (0 == ret)
{
printf("create success !\n");
}
int num = 0;
Queue_En(queue, 99);
Queue_En(queue, 88);
Queue_En(queue, 77);
Queue_En(queue, 9);
Queue_En(queue, 8);
Queue_En(queue, 7);
for (int i = 0; i queue-len; i++)
{
printf("%d\n", queue-arr[i]);
}
Queue_Out(queue, num);
printf("%d\n", num);
Queue_Free(queue);
system("pause");
}
C语言 顺序队列插入
#includestdio.h
#define N 10
void main()
{
int i,j,k,n,a[N];
printf("请输入第1个数字:");
scanf("%d",a[0]);
for(i=1;iN;i++){
printf("请输入第%d个数字:",i+1);
scanf("%d",n);
for(j=0;ji;j++)
if(na[j]){
for(k=i;kj-1;k--)
a[k]=a[k-1];
a[j]=n;
break;
}
if(j==i)
a[j]=n;
}
for(i=0;i10;i++)
printf("%3d",a[i]);
}
C语言用数组实现顺序队列求大神帮助
#include "stdio.h" main() { int que[200] = {0} ; int start = 0, end = 0; int i; char ch; printf("please enter 6 num:"); for(i=0;i6;i++) scanf("%d",que[i]); end = 5; for(i=start;i=end;i++) printf("%d ",que[i]); printf("\n"); while(1) { printf("What do you want to do? \n'i' to insert\n'd' to delete\n'q' to quit\n"); switch(ch = getch()) { case 'i': end++; printf("please enter the num:"); scanf("%d",que); for(i=start;i=end;i++) printf("%d ",que[i]); printf("\n"); break; case 'd': start++; for(i=start;i=end;i++) printf("%d ",que[i]); printf("\n"); break; case 'q': exit(0); } } }
C语言编程题,实现一个顺序存储的循环队列。
#includestdio.h
#includestdbool.h
#includemalloc.h
typedef
int
typedata;
struct
node
{
struct
node
*prev,
*next;
typedata
data;
};
typedef
struct
node
node;
typedef
struct
node*
link;
//
============init_head===============
//
//头节点的初始化
link
init_head(void)
{
link
head
=
(link)malloc(sizeof(node));
if(head
!=
NULL)
{
head-prev
=
head-next
=
head;
}
return
head;
}
//
============newnode
================
//
//创建新节点
link
newnode(typedata
data)
{
link
new
=
(link)malloc(sizeof(node));
if(new
!=
NULL)
{
//前趋指针和后趋指针都指向自己
new-prev
=
new-next
=
new;
new-data
=
data;
}
return
new;
}
//
=================is_empty================
//
bool
is_empty(link
head)
{
//为空时,头节点的前趋指针和后趋指针都指向head(头节点)
if((head-next==head)
(head-prev==head))
return
true;
return
false;
}
//
================insert_tail
==================
//
void
insert_tail(link
head,
link
new)
{
if(is_empty(head))
{
//第一个节点插入
head-next
=
head-prev
=
new;
new-next
=
new-prev
=
head;
return
;
}
//除了第一个节点插入
new-prev
=
head-prev;
new-next
=
head;
new-prev-next
=
new;
new-next-prev
=
new;
}
//
================show
====================
//
void
show(link
head)
{
//为空时,直接返回
if(is_empty(head))
return
;
//遍历整个链
link
tmp
=
head-next;
while(tmp
!=
head)
{
printf("%d\t",
tmp-data);
tmp
=
tmp-next;
}
printf("\n");
}
//
==============insert_opint
===============
//
void
insert_opint(link
end_node,
link
p)
{
p-prev
=
end_node;
p-next
=
end_node-next;
end_node-next-prev
=
p;
end_node-next
=
p;
}
//
================insertion_sort===========
//
//顺序排序
void
insertion_sort(link
head)
{
if(is_empty(head))
return;
//把队列分拆,头节点和第一个节点为一个已排序的队列,
//其他的节点逐个比较已排序队列插
link
p
=
head-next-next;
head-prev-next
=
NULL;
head-next-next
=
head;
head-next-prev
=
head;
head-prev
=
head-next;
while(p
!=
NULL)
{
link
end_node
=
head-prev;
if(p-data
end_node-data)
{
link
tmp
=
p;
p
=
p-next;
insert_tail(head,
tmp);
}
else
{
while(end_node!=head
p-dataend_node-data)
end_node
=
end_node-prev;
link
tmp
=
p;
p
=
p-next;
insert_opint(end_node,
tmp);
}
}
}
int
main(void)
{
link
head
=
init_head();
if(head
==
NULL)
{
printf("falure\n");
return
0;
}
typedata
data;
while(1)
{
if(scanf("%d",
data)
!=
1
)
break;
link
new
=
newnode(data);
if(new
==
NULL)
{
printf("falure\n");
return
0;
}
insert_tail(head,
new);
show(head);
}
printf("the
figure
is:\n");
show(head);
insertion_sort(head);
show(head);
return
0;
}