Queue Procedure Qaddint queue element index rear if
- Slides: 31
佇列 (Queue)
加入元素 演算法:虛擬碼 Procedure Qadd(int queue[ ] , element ) { index rear ; if (rear >佇列的最後位置) 顯示佇列已滿的錯誤訊息; else { rear = rear+1; queue[ rear ] = element 傳回 queele [rear ]; } }
C語言:程式碼(一) void QAdd (int queue [ ] , int Max. Size , int rear , int x ) { if ( rear >= Max. Size - 1) printf(“佇列已滿了…”) ; else queue [+ + rear ]=x ; }
C語言:程式碼(二) void queue (int data) /* 此函數將data放入queue 之後端 */ { if (q. rear == Max. Queue-1) { printf (“ queue is full n”); exit(1); } else q. item[++q. rear] = data; //將q. rear +1後,再將新元素data 放在q中 }
刪除元素 演算法:虛擬碼 Procedure QDel(int queue[ ] ) { index front , rear ; if (front >= rear) 顯示佇列已空的錯誤訊息; else { front = front+1; 傳回 queue [front ]; } }
C語言:程式碼(一) void QDel (int queue [ ] , int front , int rear ) { if ( front >= rear ) printf(“佇列已空了,無資料可刪…”) ; else { front + +; printf(“ %d 已從佇列中刪除了 ” , queue [front ] ; } }
C語言:程式碼(二) int dequeue (void) /* 此函數自queue 之前端刪除元素 */ { if (q. front == q. rear) /* q. front == q. rear 表 queue 為空 */ { printf (“ queue is empty n”); exit(1); } else return ( q. item[++q. front] ); /* 將q. rear +1後,此時q. front 指向 queue 之第一個元素, 再傳回此值 */ }
範例 以下是一個以結構定義的Queue,包含加入及刪 除節點 #include <stdio。h> struct node { // 宣告一個結構 int number; struct node *next; }; void showmain(); struct node *add(struct node *); // 將新節點加入 Queue。 struct node *out(struct node *); // 將新節點輸出 Queue。
void main() { struct node *real = NULL,*front = NULL; // 將其預 設為NULL do{ showmain(); switch (getche()) { case '1': real = add(real); if (front == NULL) // 當front為NULL時,front=real { front = real; } break;
case '2': front = out(front); break; case '3': exit(0); default: printf("n. It is wrong,pleace input againn"); break; } }while (1); }
void showmain() { printf("<1>add a valuen"); printf("<2>output a valuen"); printf("<3>exitn"); } struct node *add(struct node *real) { struct node *New; New = (struct node *)malloc(sizeof(struct node)); printf("pleace input a number"); scanf("%d",&(New->number)); New->next = NULL; if (real != NULL) { real ->next = New; // 新節點接在real之後 } real = New; // real指向新節點 return (real); }
struct node *out(struct node *front) { struct node *freenode; if (front != NULL) { printf("n. The pop number is %dn",front->number); freenode = front; front = front ->next; free(freenode); // 釋放記憶體 return (front); } else { printf("n. No Noden"); return (NULL); } }
- Transmission drive
- Queue front rear
- Consider the linear queue with rear=4
- Queue front
- Queue front rear
- Priority queue vs queue java
- Insert element in priority queue
- Distinguish between a signal element and a data element.
- Signal element vs data element
- Optical fiber waveguides
- Primary index is dense or sparse
- Consistency index
- Simpsons index
- Bacteriological index formula
- Clustered index và non clustered index
- Physical quality of life index and human development index
- Optical fibre
- Define an unstable rear zone
- Rear right safety restraint system fault
- Various
- The flag should be in the rear of a procession and centered
- Rear tool post
- Hydraulic suspension
- Compared to a gear tooth on the rear sprocket
- The rear end of an arrow has a slotted plastic tip
- Hotchkiss rear suspension
- Tyre rotation
- Rear-disc auxiliary drum parking brakes
- Lane positions 1-5
- 1
- Parts of an arrow
- Rear window