㈠ 試分別用線性表的向量存儲結構和鏈表存儲結構來實現約瑟夫(Josephu)問題。請一並詳細註解,謝謝
給分吧。。呵呵
#include<stdio.h>
struct Josephu_node{
int node_nu;
struct Josephu_node * pre;
struct Josephu_node * next;
};
struct Josephu_node * Josephu_node_add(struct Josephu_node * head,int nu)
{
struct Josephu_node * newnode;
if(head == NULL)
{
head= (struct Josephu_node *)malloc(sizeof(struct Josephu_node));
head->node_nu = 1;
head->pre = head;
head->next = head;
printf("Creat Head:%d",head->node_nu);
}else{
newnode = (struct Josephu_node *)malloc(sizeof(struct Josephu_node));
newnode->node_nu = nu;
newnode->pre = head->pre;
newnode->next = head;
head->pre->next = newnode;
head->pre = newnode;
printf(" ,%d",head->pre->node_nu);
}
return head;
}
struct Josephu_node *Joseph_node_del(struct Josephu_node * del_node)
{
struct Joseph_node * del_node_next=del_node->next;
if(del_node != NULL)
{
if(del_node->next != del_node){
del_node->pre->next = del_node->next;
del_node->next->pre = del_node->pre;
}else{
del_node_next = NULL;
}
printf(" %d,",del_node->node_nu);
free(del_node);
del_node = NULL;
return del_node_next;
}
return NULL;
}
int main(int argc ,char ** argv)
{
struct Josephu_node * head=NULL;
struct Josephu_node * node_tmp;
int nu_people=0;
int out_nu =0;
int i;
printf("Please input the nu of the People : ");
scanf("%d",&nu_people);
printf("Please input the rule out nu: ");
scanf("%d",&out_nu);
for(i=0;i<nu_people;i++)
{
head = Josephu_node_add(head,i+1);
}
printf("\n");
i = 0;
node_tmp = head;
printf("Out of the table: ");
while(node_tmp != node_tmp->pre)
{
if(i == out_nu-1)
{
node_tmp = Joseph_node_del(node_tmp);
i=0;
continue;
}
i++;
node_tmp = node_tmp->next;
}
node_tmp = Joseph_node_del(node_tmp);
printf("\n");
return 0;
}
㈡ 以鄰接矩陣為存儲結構,分別寫出基於DFS和BFS遍歷
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
template <class VertexType>
class Graph
{
public:
vector<VertexType> vertex; //存放節點數據
int ** edge;//鄰邊矩陣
int vcount; //頂點個數
int ecount; //邊個數
Graph():edge(NULL),vcount(0),ecount(0){}//構造函數
void CreateGraph()//創建矩陣
{
VertexType item; //頂點信息
int v ,u,w; //邊信息
cout << "請輸入頂點的個數: ";
cin >> vcount;
cout << "請輸入頂點" << endl;
for(int i = 0; i < vcount; ++i)
{
cin >> item;
vertex.push_back(item); //將頂點信息存放到向量中
}
edge = new int*[vcount]; //創建二維矩陣
for( i = 0; i < vcount; ++i )
{
edge[i] = new int[vcount];
for( int j = 0 ; j < vcount ; j++ )
edge[i][j] = 0;
}
cout << "請輸入邊的個數: ";
cin >> ecount;
cout << "請輸入邊的信息(始點,終點,權重)" << endl;
for( i = 0; i < ecount; ++i)
{
cin >> v >> u >> w;
edge[v][u] = w;
edge[u][v] = w;
cout<<"繼續輸入"<<endl;
}
}
void ShowGraph()
{
cout << "頂點的個數:" << vcount << endl;
cout << "邊的個數:" << ecount << endl;
for(int i = 0; i < vcount; ++i)
{
for(int j = 0; j < vcount; ++j)
cout << edge[i][j] << " ";
cout << endl;
}
}
void DFS(const int v)//深度優先搜索
{
cout << "深度優先搜索" << endl;
int * visited = new int[vcount]; //標志數組
for( int i = 0 ; i < vcount ; i++ )
visited[i] = 0;
DepthFS( v, visited );
cout << endl;
delete[] visited;
}
void DepthFS( const int v , int * visited )//深度優先搜索
{
cout << vertex[v] << " ";
visited[v] = 1;
for(int i = 0; i < vcount; i++ )
{
if( visited[i] == 0 && edge[v][i] != 0 )
{
DepthFS( i, visited );
}
}
}
void BFS(const int v)//廣度優先搜索
{
cout << "廣度優先搜索" << endl;
int * visited = new int[vcount];
for( int i = 0 ; i < vcount ; i++ )
visited[i] = 0;
queue<int> Q;
Q.push(v); //入隊列
while(!Q.empty())
{
int u = Q.front();
Q.pop(); //出隊列
cout << vertex[u] << " ";
visited[u] = 1;
for(int j = 0; j < vcount; ++j)
{
if( visited[j] == 0 && edge[u][j] != 0 )
{
visited[j] = 1;
Q.push(j);
}
}
}
cout << endl;
delete[] visited;
}
};
int main()
{
Graph<char> graph;
graph.CreateGraph();
graph.ShowGraph();
cout << "請輸入搜索的起始節點:-1表示終止";
int n;
cin>>n;
while(n!=-1)
{
cout << "請輸入搜索的起始節點:";
cin >> n;
graph.DFS(n);
graph.BFS(n);
}
return 0;
}
代碼還應該多自己寫呦,不要走捷徑
㈢ 向量的存儲方法的是什麼
向量通常的存儲方法是順序存儲,每個元素在存儲中佔用的空間大小相同,若第一個元素存放的位置是LOC(k1), 每個元素佔用的元素大小為s,則元素ki的存放位置為:
LOC(ki)= LOC(k1)+s * (i-1)
㈣ 已知一顆具有n個結點的完全二叉樹以向量作為存儲結構,設計一個非遞歸演算法,實現對該樹的先序遍歷。
public class haha {
public haha(int tree[]) {
int p = 0;
int k = 0;
while(k != tree.length){
if(p < tree.length){
System.out.print(tree[p]+" ");
p = 2*p+1;
k++;
}
else {
p = p/2;
if(p%2==0){
p=p/4-1;
p = 2*p+2;
}
else { p =p+1;
if(p >= tree.length)
p = 2*p+1;
}
}
}
}
public static void main (String[] args) {
int tree[] = {1,2,3,4,5,6,7,8};
new haha(tree);
}
}
㈤ 3、 一棵n個結點的完全二叉樹以向量(數組)作為存儲結構,試設計非遞歸演算法對該完全二叉樹進行前序遍歷。
#include<stdio.h>
#defineARRAY_MAX12
intmain()
{
inttree[ARRAY_MAX];
for(inti=0;i<ARRAY_MAX;i++)
tree[i]=i+1;
intflag=0;//記錄當前葉子的遍歷位置,0剛遍歷到這個葉子,1已經遍歷完成該葉子的左兒子,2已經遍歷完成該葉子的右兒子
intcount=1;//假設tree不為空
while(!(count==1&&flag==2))
{
if(flag==0)
{
printf("%d",tree[count-1]);
if(count*2>ARRAY_MAX)
flag=1;
else
count=count*2;
}
elseif(flag==1)
{
if(count*2+1>ARRAY_MAX)
flag=2;
else
{
count=count*2+1;
flag=0;
}
}
elseif(flag==2)
{
if(count%2==0)
flag=1;
else
flag=2;
count=count/2;
}
}
getchar();
return0;
}
以上代碼MicrosoftVisualC++6.0中編譯通過,輸出的數列為以下下二叉樹的前序遍歷
連5分都不給,真小氣......
㈥ c數據結構判斷題
1.錯 2.對 3.對 4.錯 5.錯 6.對(不是向量,是線性) 7.錯
㈦ C語言:向量存儲結構結構來實現約瑟夫(Josephu)問題。
http://..com/question/408568942.html?oldq=1
㈧ 數據結構裡面的向量是什麼結構
向量數據亦稱「矢量數據」。電子計算機中表示地理空間數據的一種數據結構。它用一系列定義起始點和終止點的線段和它們之間的連接關系來反映地理對象的空間分布。
通過記錄線段端點的坐標,向量數據結構能更精確地反映地理事物的位置、長度和面積。空間點實體在向量數據中表示為一對坐標;線實體表示為一串坐標;面實體也表示為一串坐標,並且首尾點重合。
線性表、棧、隊、串都屬於線性結構,而線性表根據不同的存儲結構又可分為順序表和鏈表。
(8)以向量作為存儲結構擴展閱讀
向量一般以數組實現,可能用定長數組實現,存放元素個數有限制,也可能用動態長度數組實現,一旦元素裝滿後會再次申請更大的空間並將原有數據拷貝過去,C++STL中的vector就是後一種。
向量的記法:印刷體記作黑體(粗體)的字母(如a、b、u、v),書寫時在字母頂上加一小箭頭「→」。如果給定向量的起點(A)和終點(B),可將向量記作AB(並於頂上加→)。在空間直角坐標系中,也能把向量以數對形式表示,例如xOy平面中(2,3)是一向量。
㈨ 棧以向量存儲是什麼意思
C 啊,現在空棧是指針在最大下標以上,自然是進棧就需要往下減,並且合法下標只是1..n