順序隊列實現c語言,c語言順序隊列的基本操作

本文目錄一覽:

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;

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/304847.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2025-01-01 11:06
下一篇 2025-01-01 11:06

相關推薦

  • Python棧操作用法介紹

    如果你是一位Python開發工程師,那麼你必須掌握Python中的棧操作。在Python中,棧是一個容器,提供後進先出(LIFO)的原則。這篇文章將通過多個方面詳細地闡述Pytho…

    編程 2025-04-29
  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • Python操作數組

    本文將從多個方面詳細介紹如何使用Python操作5個數組成的列表。 一、數組的定義 數組是一種用於存儲相同類型數據的數據結構。Python中的數組是通過列表來實現的,列表中可以存放…

    編程 2025-04-29
  • Python中的隊列定義

    本篇文章旨在深入闡述Python中隊列的定義及其應用,包括隊列的定義、隊列的類型、隊列的操作以及隊列的應用。同時,我們也會為您提供Python代碼示例。 一、隊列的定義 隊列是一種…

    編程 2025-04-29
  • RabbitMQ和Yii2的消息隊列應用

    本文將探討RabbitMQ和Yii2之間的消息隊列應用。從概念、安裝和配置、使用實例等多個方面詳細講解,幫助讀者了解和掌握RabbitMQ和Yii2的消息隊列應用。 一、Rabbi…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python操作MySQL

    本文將從以下幾個方面對Python操作MySQL進行詳細闡述: 一、連接MySQL資料庫 在使用Python操作MySQL之前,我們需要先連接MySQL資料庫。在Python中,我…

    編程 2025-04-29
  • Python磁碟操作全方位解析

    本篇文章將從多個方面對Python磁碟操作進行詳細闡述,包括文件讀寫、文件夾創建、刪除、文件搜索與遍歷、文件重命名、移動、複製、文件許可權修改等常用操作。 一、文件讀寫操作 文件讀寫…

    編程 2025-04-29

發表回復

登錄後才能評論