導航:首頁 > 源碼編譯 > std隊列源碼

std隊列源碼

發布時間:2022-09-20 11:47:13

A. 利用兩個棧模擬實現一個隊的入隊和出隊運算 用c++實現

首先隊列queue的基本操作是:1. 插入 2. 刪除
用兩個堆棧stack1 , stack2來實現上述兩種操作無非就是:
隊列插入元素x,就往stack1的棧頂插入元素x。當要彈出隊列的時候,再把這個棧頂元素從stack1中pop掉,並插入到stack2中。
這樣一來,先插入的元素,對於stack2就在棧頂的位置了。如此一來,兩個堆棧就成功地實現了隊列了。

代碼如下:
#include <iostream>
#include <stack>
using namespace std;
template <typename T>
class Queue
{
private:
stack<T> s1,s2;
public:
Queue()
{
}

//「隊列」插入元素
void push(T element)
{
s1.push(element);
}

void pop()
{
// s2不空,則s2的棧頂元素為虛擬隊列最前面的元素
if(!s2.empty())
s2.pop();
// s2為空,說明s1的棧底元素為虛擬隊列最前面的元素
// 應現將s1中的所有元素pop到s2中,再輸出棧頂元素
else
{
while(!s1.empty())
{
T tmp = s1.top();
s1.pop();
s2.push(tmp);
}
s2.pop();
}
}

// front() 輸出隊列頂的元素,執行過程與pop() 函數相似,不再贅述.
T front()
{
if(!s2.empty())
return s2.top();
else
{
while(!s1.empty())
{
T tmp = s1.top();
s1.pop();
s2.push(tmp);
}
return s2.top();
}
}
};
int main() {
Queue<int> q;
q.push(1);
q.push(2);
q.push(4);
q.push(5);
q.pop();
cout << q.front() << endl;
return 0;
}

B. C/C++線程安全型隊列的實現

首先,互斥量這種線程相關的內容是平台相關的,我假設你用的是windows平台開發。
其次,說明一下我的開發環境,vs2008,控制台程序,空的工程。
最後給你貼代碼,分文件來看。

===頭文件QueueNode.h===
===你需要的節點數據可能不是整數,只要將typedef int QUEUEDATA這一句的int換成你想要的類型即可,但要注意,這個類型必須實現賦值操作符重載,相等比較操作符重載,以及復制構造函數===

#ifndef _QUEUE_NODE_H_
#define _QUEUE_NODE_H_

typedef int QUEUEDATA;

typedef struct node
{
QUEUEDATA data;
node* m_pNext;
}QUEUENODE;

#endif

===隊列頭文件Queue.h,有平台相關內容,請注意===
#ifndef _QUEUE_H_
#define _QUEUE_H_

#include "QueueNode.h"
#include <Windows.h>

class ThreadSafeQueue
{
public:
ThreadSafeQueue();
virtual ~ThreadSafeQueue();

bool InitQueue();
void EnQueue(const QUEUEDATA& data);
void DeQueue();
void Clear();
const QUEUENODE* Find(const QUEUEDATA& data) const;
void Print();

protected:
HANDLE m_hMutex;
QUEUENODE* m_pQueueHead;
};

#endif

===隊列函數實現文件Queue.cpp===
#include "Queue.h"
#include <iostream>

ThreadSafeQueue::ThreadSafeQueue()
{
m_pQueueHead = new QUEUENODE;
m_pQueueHead->m_pNext = 0;
}

ThreadSafeQueue::~ThreadSafeQueue()
{
Clear();
delete m_pQueueHead;
CloseHandle(m_hMutex);
}

bool ThreadSafeQueue::InitQueue()
{
m_hMutex = CreateMutex(0, FALSE, 0);
return (m_hMutex!=0);
}

void ThreadSafeQueue::EnQueue(const QUEUEDATA& data)
{
WaitForSingleObject(m_hMutex, INFINITE);
QUEUENODE* pNode = new QUEUENODE;
pNode->data = data;
pNode->m_pNext = 0;
QUEUENODE* pTemp = m_pQueueHead;
while (pTemp->m_pNext != 0)
{
pTemp = pTemp->m_pNext;
}
pTemp->m_pNext = pNode;
ReleaseMutex(m_hMutex);
}

void ThreadSafeQueue::DeQueue()
{
WaitForSingleObject(m_hMutex, INFINITE);
QUEUENODE* pNode = m_pQueueHead->m_pNext;
if (pNode != 0)
{
m_pQueueHead->m_pNext = pNode->m_pNext;
delete pNode;
pNode = 0;
}
ReleaseMutex(m_hMutex);
}

const QUEUENODE* ThreadSafeQueue::Find(const QUEUEDATA& data) const
{
WaitForSingleObject(m_hMutex, INFINITE);
QUEUENODE* pNode = m_pQueueHead->m_pNext;
while (pNode != 0)
{
if (pNode->data == data)
{
break;
}
pNode = pNode->m_pNext;
}
return pNode;
ReleaseMutex(m_hMutex);
}

void ThreadSafeQueue::Clear()
{
WaitForSingleObject(m_hMutex, INFINITE);
QUEUENODE* pNode = m_pQueueHead->m_pNext;
QUEUENODE* pTemp = 0;
while (pNode != 0)
{
pTemp = pNode->m_pNext;
delete pNode;
pNode = pTemp;
}
m_pQueueHead->m_pNext = 0;
ReleaseMutex(m_hMutex);
}

void ThreadSafeQueue::Print()
{
WaitForSingleObject(m_hMutex, INFINITE);
QUEUENODE* pNode = m_pQueueHead->m_pNext;
while (pNode != 0)
{
std::cout << pNode->data << "\t";
pNode = pNode->m_pNext;
}
std::cout << std::endl;
ReleaseMutex(m_hMutex);
}

===測試代碼文件main.cpp,包含了測試用可執行程序,兩個操作queue的線程,需要說明的是,我本來打算用WaitMultipleObjects函數來等待兩個線程都結束,但是沒搞清楚是什麼問題沒有卡住,不打算繼續糾纏它了,所以讓主線程Sleep了5秒鍾===
#include "Queue.h"
#include <iostream>

DWORD WINAPI HandleQueue(void* pParam);
DWORD WINAPI HandleQueue2(void* pParam);

int main()
{
ThreadSafeQueue queue;
queue.InitQueue();
HANDLE hThread[2] = {0};
DWORD threadID = 0;
hThread[0] = CreateThread(NULL, 0, HandleQueue, (void*)(&queue), NULL, &threadID);
hThread[0] = CreateThread(NULL, 0, HandleQueue2, (void*)(&queue), NULL, &threadID);

//WaitForMultipleObjects(2, hThread, TRUE, INFINITE);
Sleep(5000);
queue.Print();
queue.Clear();
return 0;
}

DWORD WINAPI HandleQueue(void* pParam)
{
ThreadSafeQueue* pQueue = reinterpret_cast<ThreadSafeQueue*>(pParam);
for (int i = 0; i < 100; i++)
{
std::cout << "HandleQueue EnQueue" << std::endl;
pQueue->EnQueue(i);
}
for (int i = 0; i < 50; i++)
{
std::cout << "HandleQueue DeQueue" << std::endl;
pQueue->DeQueue();
}
return 0;
}

DWORD WINAPI HandleQueue2(void* pParam)
{
ThreadSafeQueue* pQueue = reinterpret_cast<ThreadSafeQueue*>(pParam);
for (int i = 0; i < 100; i++)
{
std::cout << "HandleQueue2 EnQueue" << std::endl;
pQueue->EnQueue(i+100);
}
for (int i = 0; i < 50; i++)
{
std::cout << "HandleQueue2 DeQueue" << std::endl;
pQueue->DeQueue();
}
return 0;
}

新建一個空的控制台程序工程,向工程中加入這幾個文件,編譯之後可以直接運行。
第一個線程投入隊列100個元素,出隊50個元素,第二個線程同樣。最後主線程輸出隊列中最後的內容,然後清空。
隊列用鏈表實現,可以試想一下,如果線程同步沒有處理,指針操作時一定會引起崩潰

C. c++如何定義一個結構體隊列

使用標准模板庫(STL)中的std::queue就可以,不一定要用指針。比如:

#include<queue>
structMyStruct
{
intnum;
};
intmain()
{
//定義隊列
std::queue<MyStruct>q;
MyStructs1;
s1.num=1;
//插入隊列
q.push(s1);
//取出隊首元素
MyStructs1_=q.front();
//隊首元素從隊列中移除
q.pop();

return0;
}


隊列的特徵是先進先出(FIFO),所以push元素會放到隊尾,pop元素會取出隊頭。

除此之外STL還提供了雙端隊列(deque)。顧名思義,雙端隊列可以在隊列兩頭進行操作。所以提供了對應的*front和*back的操作。

以下是參考代碼:

#include<queue>
#include<iostream>
structMyStruct
{
intnum;
};
intmain()
{
//定義雙端隊列
std::deque<MyStruct>q;
MyStructs1;
s1.num=1;
//插入隊尾
q.push_back(s1);
MyStructs2;
s2.num=5;
//插入隊首
q.push_front(s2);
//取出隊首元素
MyStructs2_=q.front();
MyStructs1_=q.back();
std::cout<<"s2="<<s2_.num<<std::endl;
std::cout<<"s1="<<s1_.num<<std::endl;
//隊首元素從隊列中移除
q.pop_front();
//隊尾元素從隊列中移除
q.pop_back();

//判斷隊列是否為空
if(q.empty())
{
std::cout<<"Enpty"<<std::endl;
}
return0;
}


std::queue參考:

http://www.cplusplus.com/reference/queue/queue/

std::deque參考:

http://www.cplusplus.com/reference/deque/deque/

D. C++中"std::"是什麼意思

在C++中,std其實就是standard標準的意思。

例如std::cin就是標准輸入,std::cout就是標准輸出的意思。

拓展閱讀:

C++是C語言的繼承,它既可以進行C語言的過程化程序設計,又可以進行以抽象數據類型為特點的基於對象的程序設計,還可以進行以繼承和多態為特點的面向對象的程序設計。C++擅長面向對象程序設計的同時,還可以進行基於過程的程序設計,因而C++就適應的問題規模而論,大小由之。

C++不僅擁有計算機高效運行的實用性特徵,同時還致力於提高大規模程序的編程質量與程序設計語言的問題描述能力。

E. 本人使用Dev C++5.6.3,請問-std=c++11在源代碼哪裡插入

【工具】->【編譯器選項】->【代碼生成/優化】->【代碼生成】->然後在"語言標准"下拉列表選擇ISO C++ 11 或者 GNU C++ 11標准即可。

或者
【工具】->【編譯器選項】->【編譯器】 在「 編譯時加入一下命令"文本框中加入 -std=c++11

F. 幫忙些一個C語言隊列的程序, 要求只有一個文件"queue.c".

我在寫,但是你的圖片顯然看不到。。。。

寫好了,測試無誤,代碼如下。我不能看到你的圖片,因而,無法知道你的person結構體裡面到底有些什麼,我示意地給出了名字、年齡、優先順序,你可以自主往裡面添加,就增加幾個單詞而已啦。MAX_HEAP_SIZE我不太明白用作何種意義,就當作最大不能超過MAX_HEAP_SIZE個人來處理了,不合適你可以很容易修改。(聯系方式[email protected]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_CHARACTERS 30
#define MAX_HEAP_SIZE 100
#define MAX_PRIORITY 100
#define MIN_PRIORITY 0

/* define struct of a person */
struct person{
char *name;
int age;
int priority;
struct person *Next;
};
typedef struct person Person;
typedef struct person * Element;

/* implement the queue data structure */
/* using an additional head to simplify logic */
Element Head = (Element)malloc(sizeof(Person));
Element Rear = Head;
int PersonCount = 0;

/* function declarations */
Element GetInput(); /* Obtain the user's input */
int Enqueue(Element);/* Enqueue a person */
void Dequeue();/* Dequeue the person at the top of heap */
void printPerson(Element);/* Print a person's information */
void freeElem(Element);/* Free an element */
void Dump();/* Dump all persons' information in the heap */
void Exit();/* Free heap and exit */

int main()
{
while(true)
{
printf("\nPlease select a menu option:\n"\
"1) Enqueue a person\n"\
"2) Dequeue the next person\n"\
"3) Dump the contents of the heap\n"\
"4) Quit the program\n"\
"Enter an option: ");
int choice;
scanf("%d", &choice);
switch(choice)
{
case 1:
Element elem;
if((elem = GetInput())!= NULL)
if(Enqueue(elem) == 0)
printf("Succeed!\n");
break;
case 2:
Dequeue();
break;
case 3:
Dump();
break;
case 4:
Exit();
break;
default:
fprintf(stderr, "Invalid choice!\n");
break;
}
}
}

Element GetInput()
{
/* if heap is full, not allow to enqueue */
if (PersonCount > MAX_HEAP_SIZE)
{
fprintf(stderr, "Heap is full!\n");
return NULL;
}
/* input like: Michael 20 90 */
printf("Input person's information in order of Name, Age and Priority :\n");
Element elem = (Element)malloc(sizeof(Person));
elem->name = (char*)malloc(MAX_CHARACTERS);
fflush(stdin);
scanf("%s%d%d", elem->name, &elem->age, &elem->priority);
/* check the validation of input */
if (elem->age <= 0 || elem->age > 130 || elem->priority > MAX_PRIORITY || elem->priority < MIN_PRIORITY)
{
fprintf(stderr, "Invalid personal information! Abort!\n");
freeElem(elem);
return NULL;
}
elem->Next = NULL;
return elem;
}

int Enqueue(Element elem)
{
/* if heap is null, deal with it */
if(Head == Rear)
{
Head->Next = Rear = elem;
return 0;
}
/* if heap contains more than one element */
Element tmp;
for (tmp = Head; tmp != Rear; tmp = tmp->Next)
{
/* implement the priority queue */
if(elem->priority >= tmp->Next->priority)
{
elem->Next = tmp->Next;
tmp->Next = elem;
return 0;
}
}
/* if the element being enqueued has the lowest priority */
if(tmp == Rear)
{
tmp->Next = Rear = elem;
return 0;
}
return 1;
}
void Dequeue()
{
/* if the queue is empty */
if (Head == Rear)
{
fprintf(stderr, "The queue is empty!\n");
return;
}
Element tmp = Head->Next;
/* print person's information */
printPerson(tmp);
Head->Next = tmp->Next;
/* free element */
freeElem(tmp);
}
void printPerson(Element elem)
{
if(NULL == elem)
{
fprintf(stderr, "Element is NULL!\n");
exit(EXIT_FAILURE);
}
printf("Name: %s\t Age: %d\t Priority: %d\n", elem->name, elem->age, elem->priority);
}
void freeElem(Element elem)
{
free(elem->name);
free(elem);
}
void Dump()
{
if (Head == Rear)
{
fprintf(stderr, "The queue is empty!\n");
return;
}
int index = 1;/* index starts from 1 */
Element tmp;
for (tmp = Head->Next; tmp != Rear; tmp = tmp->Next)
{
printf("Index: %d\tName: %s\t Age: %d\t Priority: %d\n", index, tmp->name, tmp->age, tmp->priority);
}
printf("Index: %d\tName: %s\t Age: %d\t Priority: %d\n", index++, tmp->name, tmp->age, tmp->priority);
}
void Exit()
{
/* if the queue if empty, free the head */
if (Head == Rear)
{
free(Head);
exit(0);
}
/* free all elements */
Element curr = Head->Next;
free(Head);
while (curr != Rear)
{
Head = curr->Next;
freeElem(curr);
curr = Head;
}
/* at last, free the rear element */
freeElem(Rear);
exit(0);
}

這個是數組優先順序隊列的實現:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* define debug */
#ifdef __DEBUG__
#include <stdarg.h>
void debug(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
#else
void debug(const char *fmt, ...)
{
}
#endif

/* common constraints */
#define MAX_CHARACTERS 30
#define MAX_HEAP_SIZE 100
#define MAX_PRIORITY 100
#define MIN_PRIORITY 0

/* define struct of a person */
typedef struct person{
char name[MAX_CHARACTERS+1];/* better to use char *name */
int priority;
} Person, *Element;

/* function declarations */
int isFull(int, int);/* decide whether the queue is full */
Element GetInput(); /* Obtain the user's input */
int Enqueue(Element *, Element, int *, int *);/* Enqueue a person */
void Dequeue(Element *, int *);/* Dequeue the person at the top of heap */
void Dump(const Element *, int, int);/* Dump all persons' information in the heap */
void Exit(Element *, int, int);/* Free heap and exit */

int main()
{
/* define the queue and allocate memory. Local to main function. */
/* can store MAX_HEAP_SIZE-1 elements */
Element *queue = (Element *)malloc(MAX_HEAP_SIZE * sizeof(Element *));
/* pointers to the queue, initially they point to zero */
int front, rear;
front = rear = 0;
/* for user's choice */
while(true)
{
printf("\nPlease select a menu option:\n"\
"1) Enqueue a person\n"\
"2) Dequeue the next person\n"\
"3) Dump the contents of the heap\n"\
"4) Quit the program\n"\
"Enter an option: ");
int choice;
fflush(stdin);
scanf("%d", &choice);
switch(choice)
{
case 1:
/* if heap is full, not allow to enqueue */
if (isFull(front, rear))
{
fprintf(stderr, "Heap is full!\n");
break;
}
/* if not, process the enqueue operation */
Element elem;
if((elem = GetInput())!= NULL)
if(Enqueue(queue, elem, &front, &rear) == 0)
printf("Insertion Successful.\n");
break;
case 2:
/* if the queue is empty */
if(rear == front)
{
printf("The heap is empty. No removal has occurred.");
break;
}
Dequeue(queue, &front);
break;
case 3:
Dump(queue, front, rear);
break;
case 4:
Exit(queue, front, rear);
break;
default:
fprintf(stderr, "Invalid choice!\n");
}
}
}

int isFull(int front, int rear)
{
/* circular queue is used here */
return (rear+1) % MAX_HEAP_SIZE == front;
}

Element GetInput()
{
/* allocate memory for a person on the heap */
Element elem = (Element)malloc(sizeof(Person));
/* input like:
* Enter the name: Bill Smith
* Enter the priority: 77
*/
printf("\nEnter the name: ");
fflush(stdin);
fgets(elem->name, MAX_CHARACTERS, stdin);
elem->name[strlen(elem->name)-1] = '\0';/* deal with the '\n' in the string*/
printf("Enter the priority: ");
fflush(stdin);
scanf("%d", &elem->priority);
/* check the validation of input */
if (elem->priority > MAX_PRIORITY || elem->priority < MIN_PRIORITY)
{
fprintf(stderr, "Insertion failed because of an invalid priority.\n");
free(elem);
return NULL;
}
return elem;
}
/* calling function is responsible for queue check */
int Enqueue(Element *queue, Element elem, int *front, int *rear)
{
/* if queue if empty */
if (front == rear)
{
*rear = (*rear + 1) % MAX_HEAP_SIZE;/* has a circular meaning */
return 0;
}
/* queue is not empty, compare before insertion*/
/* first write to the rear of the queue */
*rear = (*rear + 1) % MAX_HEAP_SIZE;
queue[*rear] = elem;
int pointer = *rear;
/* exchange to proper position of the queue */
Element tmp;
int next;
while(true)
{
if(pointer == 0)
next = MAX_HEAP_SIZE;
else
next = pointer - 1;
if(queue[pointer] > queue[next])
{
/* exchange */
tmp = queue[pointer];
queue[pointer] = queue[next];
queue[next] = tmp;
}
else
return 0;/* done with enqueue */
}
return 1; /* something happened unexpectedly */
}
/* the calling function is responsible for queue check */
void Dequeue(Element *queue, int *front)
{
*front = (*front + 1) % MAX_HEAP_SIZE;
printf("%s (Priority %d) has been removed.\n",\
(*queue[*front]).name, (*queue[*front]).priority);
}

void Dump(const Element *queue, int front, int rear)
{
/* if the queue is empty */
if(rear == front)
{
printf("The heap is empty.\n");
return;
}
printf("Index\tPriority\t\tName\n-----\t---------\t----------------------\n");
rear = (rear+1)%MAX_HEAP_SIZE;
int index = 0;/* index starts from 0 */
while((front = (front+1)%MAX_HEAP_SIZE) != rear)
{
printf("%d\t%d\t\t%s\n", index, (*queue[front]).priority, (*queue[front]).name);
++index;
}
}
void Exit(Element *queue, int front, int rear)
{
printf("\nProgram terminating...");
if(rear == front)
{
free(queue);
exit(0);
}
/* free the elements */
rear = (rear+1)%MAX_HEAP_SIZE;
while((front = (front+1)%MAX_HEAP_SIZE) != rear)
{
free(queue[front]);
}
/* free the array of pointers to elements */
free(queue);
exit(0);
}

G. std::queue怎麼用啊,怎麼遍歷元素啊

int main(int argc, char *argv[])
{
queue<int> myQ;
for(int i=0; i<10; i++)
myQ.push(i);
for(int i=0; i<myQ.size(); i++)
{
cout << myQ.front()<<endl;
myQ.pop();
}
return 0;
}queue是STL的隊列,有FIFO的特性。上面的程序是將0~9十個數字壓入隊列,然後依次出對queue的成員方法比較少,常用的也就那麼幾個,注意,要包含頭文件<queue>對於priority_queue,他的原則是優先權大的先出隊,也就是說,你在創建一個priority_queue的時候是可以指定每個元素的優先順序的,優先順序越大,出隊越早,而queue只是傳統意義上簡單的隊列。

H. 《STL源碼分析》中如何priority_queue使用greater函數對象

首先查看手冊,priority_queue的定義如下:

template<classT, classContainer=std::vector<T>, classCompare=std::less<typenameContainer::value_type>>classpriority_queue;

然後繼續看模板的三個參數的說明

—————————以下直接摘抄的—————

Template parameters

T - The type of the stored elements.The behavior is undefined ifTis not the same type asContainer::value_type.(since C++17)

Container - The type of the underlying container to use to store the elements. The container must satisfy the requirements ofSequenceContainer, and its iterators must satisfy the requirements ofLegacyRandomAccessIterator. Additionally, it must provide the following functions with the usual semantics:

Compare - AComparetype providing a strict weak ordering.

—————————以上直接摘抄的—————

故可知,使用priority_queue需要給三個類來實現模板,其中第三個類就是那個比較函數,你問的,為什麼要priority_queue<int, vector<int>, greater<int> > q1;已經回答完畢。

另外,可以參考std::less的定義,更深入學習第三個類的含義。已附在引用部分,自行查閱。


std::priority_queuestd::less

PS:第一個那傢伙回答的什麼東西!我本來是不想回答的。。。看見那傢伙胡謅一氣,氣不過。

I. C++中"std::"是什麼意思起什麼作用

std是一個類(輸入輸出標准),它包括了cin成員和cout成員,using name space std ;以後才能使用它的成員。#include<iostream.h>中不存在類std,但是他又cin,out的相關函數,不需要使用命名空間了。而第二種標准#include<iostream>,它包含了一個類,在類的使用之前要預處理一下,using namespace std;就是這個功能,然後你就可以使用cin,cout這兩個成員函數了,假設你不使用預處理(using namespace std;),麻煩加上std::cin或者std::cout再去使用它的成員函數(頭文件中存在這個類)

J. 用隊列實現按層次創建二叉樹的源代碼,最好是C語言

隊列??你每輸入一個節點將其存入隊列中,再輸入它的左孩子,它的左孩子也會入隊,我們取的時候應先取該節點的左孩子,

LZ一定要用隊列也行,但絕對不是正確的選擇!

隊列如下:

#include <iostream>
using namespace std;
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}*bitree,tree;
int number=0;
void createbitree(bitree &t)
{
char c;
int i=0,r=0,f=0;//r,f分別指向隊首和隊尾
bitree p=NULL,temp=NULL,pre=NULL,s[100];
s[0]=NULL;
int lflag[100]={0};
int rflag[100]={0};
printf("請輸入根節點:");
t=new tree;
t->lchild=t->rchild=NULL;
scanf("%c",&t->data);
temp=pre=t->lchild;
s[++i]=t;
f=i;
p = t;
while(f!=r)
{
if(p->lchild==NULL&&lflag[i]==0)
{
printf("請輸入%c的左孩子:",p->data);
fflush(stdin);
scanf("%c",&c);
if(c!='#')
{
p->lchild = new tree;
p = p->lchild;
p->lchild=p->rchild=NULL;
p->data = c;
s[++f]=p;
i = f;
lflag[i]=rflag[i]=0;
}
else
lflag[i]=1;
}
else if(p->rchild==NULL&&rflag[i]==0)
{
printf("請輸入%c的右孩子:",p->data);
fflush(stdin);
scanf("%c",&c);
if(c!='#')
{
p->rchild = new tree;
p = p->rchild;
p->lchild=p->rchild=NULL;
p->data = c;
s[++f]=p;
i=f;
lflag[i]=rflag[i]=0;
}
else
{
rflag[i]=1;
p=s[++r];
i=r;
}
}
else
{
p=s[++r];
i=r;
}
}
}
void preorder(bitree &t)//遍歷二叉樹,輸出函數
{
if (t!=0)
{
cout<<t->data<<",";
preorder(t->lchild);
preorder(t->rchild);
}
}
void main()
{
bitree t;
t=0;
createbitree(t);
cout<<"the value of the preorder value is:";
preorder(t);
}

在此,強烈建議LZ用棧,更符合樹的輸入層次:

#include <iostream>
using namespace std;
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}*bitree,tree;
int number=0;
void createbitree(bitree &t)
{
char c;
int i=0;
bitree p=NULL,temp=NULL,pre=NULL,s[100];
s[0]=NULL;
int lflag[100]={0};
int rflag[100]={0};
printf("請輸入根節點:");
t=new tree;
t->lchild=t->rchild=NULL;
scanf("%c",&t->data);
temp=pre=t->lchild;
s[++i]=t;
p = t;
while(s[i]!=NULL)
{
if(p->lchild==NULL&&lflag[i]==0)
{
printf("請輸入%c的左孩子:",p->data);
fflush(stdin);
scanf("%c",&c);
if(c!='#')
{
p->lchild = new tree;
p = p->lchild;
p->lchild=p->rchild=NULL;
p->data = c;
s[++i]=p;
lflag[i]=rflag[i]=0;
}
else
lflag[i]=1;
}
else if(p->rchild==NULL&&rflag[i]==0)
{
printf("請輸入%c的右孩子:",p->data);
fflush(stdin);
scanf("%c",&c);
if(c!='#')
{
p->rchild = new tree;
p = p->rchild;
p->lchild=p->rchild=NULL;
p->data = c;
s[++i]=p;
lflag[i]=rflag[i]=0;
}
else
{
rflag[i]=1;
p=s[--i];
}
}
else
p=s[--i];
}
}
void preorder(bitree &t)//遍歷二叉樹,輸出函數
{
if (t!=0)
{
cout<<t->data<<",";
preorder(t->lchild);
preorder(t->rchild);
}
}
void main()
{
bitree t;
t=0;
createbitree(t);
cout<<"the value of the preorder value is:";
preorder(t);
}

不懂追問!你的疑問往往是我要學習的地方!

閱讀全文

與std隊列源碼相關的資料

熱點內容
美團的伺服器是什麼 瀏覽:357
axure原型設計精髓pdf 瀏覽:376
svox文件夾有用嗎 瀏覽:506
怎樣才可以給軟體添加密鑰 瀏覽:587
光纖通信原理pdf 瀏覽:207
c需要用什麼編譯器 瀏覽:702
python設置斷點調試 瀏覽:313
pc手柄怎麼連接安卓 瀏覽:33
dll解壓不成功 瀏覽:343
連接地址伺服器失敗是什麼 瀏覽:399
台達dvp14ss2編程電纜 瀏覽:133
單片機開發板設置技巧 瀏覽:343
阿里雲伺服器怎麼配置git 瀏覽:414
androidcameraid 瀏覽:430
活塞式空氣壓縮機原理 瀏覽:791
vt編輯編制編譯 瀏覽:807
抖音優質創作者推薦程序員 瀏覽:75
攝像機多控神器讓拍攝輕松解壓 瀏覽:422
杭州的伺服器地址 瀏覽:277
全醫葯學大詞典pdf 瀏覽:809