隊列實現 (雙向迴圈鏈表 C++),隊列
隊列是很簡單的,但是用數組實現可能更好點。。(其實我覺得數組在多個隊列的時候更難)
然後我是第一次寫雙向迴圈鏈表。指向太亂了。
我這裡是按照自己的想法,建立了一個前端節點,一個尾節點,然後按照隊列順序正向插入到兩個節點之間。輸出和彈出隊列的時候從後面操作。
下面上代碼:
//// main.cpp// queue//// Created by Alps on 14-7-28.// Copyright (c) 2014年 chen. All rights reserved.//#include <iostream>#define ElementType intusing namespace std;struct Node;typedef Node* PtrToNode;typedef PtrToNode Queue;struct Node{ ElementType X; PtrToNode Pre; PtrToNode Next;};Queue createQueue(void){ Queue Q; Queue Q2; Q2 = (Queue)malloc(sizeof(Queue)); Q = (Queue)malloc(sizeof(Queue)); Q->X = 0; Q->Next = Q2; Q->Pre = Q2; Q2->Next = Q; Q2->Pre = Q; return Q;}int isEmpty(Queue Q){ return Q->Next->Next == Q;}void intoQueue(Queue Q, ElementType element){ Queue tmp; Queue tmp1; tmp1 = (Queue)malloc(sizeof(Queue));// Queue switchTmp; tmp = (Queue)malloc(sizeof(Queue)); tmp->X = element; tmp->Next = Q->Next; Q->Next->Pre = tmp; Q->Next = tmp; tmp->Pre = Q;}void outQueue(Queue Q){ Queue tmp; tmp = Q->Pre->Pre; Q->Pre->Pre = tmp->Pre; tmp->Pre->Next = Q->Pre; free(tmp);}ElementType headQueue(Queue Q){ if (Q == NULL) { printf("please create queue first!\n"); return 0; } if (!isEmpty(Q)) { return Q->Pre->Pre->X; }else{ printf("The queue is empty!\n"); return 0; }}int makeEmpty(Queue Q){ if (Q == NULL) { printf("please create queue first!\n"); return -1; } while (!isEmpty(Q)) { outQueue(Q); } return 0;}void Print(Queue Q){ if (!isEmpty(Q)) { Queue tmp = Q->Pre->Pre; while (tmp != Q) { printf("%d ",tmp->X); tmp = tmp->Pre; } printf("\n"); }}int main(int argc, const char * argv[]){ Queue Q = createQueue(); if (isEmpty(Q)) { printf("The queue is empty !\n"); }else{ printf("The queue is not empty!\n"); } intoQueue(Q, 2); intoQueue(Q, 4); intoQueue(Q, 6); Print(Q); outQueue(Q); Print(Q); makeEmpty(Q); Print(Q);// printf("%d\n",headQueue(Q)); return 0;}
這個代碼比較亂 = = ,多包涵了,我以後想想簡單點的方法實現。其實單鏈表也可以,但是那樣操作就不是O(1)了,所以才用雙鏈表。
C語言中怎定義雙向鏈表?單向鏈表?隊列?
你知道雙向鏈表、單向鏈表、隊列都是什麼嗎?
如果不知道,我就算寫給你,你也不認識。
用C語言編寫一個程式,建立雙向迴圈鏈表,並實現它的插入操作、刪除操作
望笑納~
//CreateList_L.cpp
//To create a LinkList
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2
typedef struct DuLNode
{ int data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode,*DuLinkList;
// 初始條件:L已存在。操作結果:返回L中資料元素個數
int ListLength(DuLinkList L)
{
int i=0;
DuLinkList p=L->next; // p指向第一個結點
while(p!=L) // p沒到表頭
{
i++;
p=p->next;
}
return i;
}
// 由雙鏈迴圈線性表L的頭結點出發,正序輸出每個資料元素
void ListTraverse(DuLinkList L)
{
DuLinkList p=L->next;
while(p!=L)
{
cout<<p->data<<"\t";
p=p->next;
}
cout<<endl;
}
// 由雙鏈迴圈線性表L的頭結點出發,逆序輸出每個資料元素
void ListTraverseBack(DuLinkList L)
{
DuLinkList p=L->prior;
while(p!=L)
{
cout<<p->data<<"\t";
p=p->prior;
}
cout<<endl;
}
//To Creatre a DuLinkList L with HeadNode
void CreateList_DuL(DuLinkList &L)
{
int n;
int i;
DuLNode *p;
L=(DuLinkList)malloc(sizeof(DuLNode));
L->next=L->prior=L;
cout<<"CreateList_L"<<endl<<"================"<<endl;
cout<<"Please input the Init DuLinkNode Number: <eg. 5> ";
cin>>n;
cout<<"Please input th......餘下全文>>