最近開始學資料結構,準備把做的習題**屯部落格裡。
#pragma once
#include
//#include
using
namespace std;
//復合類
/*class list;
class linknode ;
class list ;
*///巢狀類
/*class list ;
linknode* first;
};*/
//用struct定義linknode
class
linknode
private
:int data;
linknode* link;};
class
list
;//插入演算法
bool list::
insert
(int i,
int& x)
newnode-
>link = first; first = newnode;
}else
if(current ==
null
)else
newnode-
>link = current-
>link; current-
>link = newnode;}}
return
true;}
//刪除演算法
bool list::
remove
(int i,
int& x)
else
if(current ==
null
|| current-
>link ==
null
) del = current-
>link;
current-
>link =del-
>link;
} x = del-
>data;
delete del;
return
true
;}
#pragma once
#include
#include
#include
using
namespace std;
class
linknode
;class
list
;class
linknode
friend
class
list
;private
:int data;
shared_ptr link;};
bool list::
insert
(int i,
int& x)
shared_ptr current=head_node;
for(
int a =
0; a < i; a++)if
(current ==
null
)else
}bool list::
delete
(int i,
int& x)
else
if(current==
null
||current-
>link==
null
) x = current-
>link-
>data;
current-
>link = current-
>link-
>link;
}return
true;}
void list::
print()
cout << endl;
}void list::
print
(int i)
shared_ptr list::
locate
(int i)
if(current ==
null
)return current;
}shared_ptr list::
findmaxlocation()
return max;
}int list::
findmax
(int
& x)
current = current-
>link;
curnode++;}
return max-
>data;
}int list::
findmax()
current = current-
>link;
}return max-
>data;
}vector<
int> list::
find_number
(int x)
return node;
}bool list::
create
(vector<
int> vec)
return
true;}
void list::
tidyup()
else
current = current-
>link;}}
void list::
sort()
max_before = max_find;
temp = current-
>link;
max = max_before-
>link;
current-
>link = max_before-
>link;
max_before-
>link = max-
>link;
max-
>link = temp;
current = max;
}}
只寫了乙個方向上的插入和輸出,刪除有點懶得寫了。
#pragma once
#include
#include
using
namespace std;
class
linknode
friend
class
dlist
;private
:int data;
shared_ptr l_link, r_link;};
class
dlist
bool
insert
(int i,
int& x)
;//將x插入到第i個結點之後
void
print()
;//輸出鍊錶
private
: shared_ptr head_node;};
bool dlist::
insert
(int i,
int& x)
shared_ptr current = head_node;
//插入空表
if(head_node-
>r_link == head_node)
else
return
false;}
//非空表
for(
int a =
0; a < i; a++)if
(current == head_node&&i!=0)
else
}void dlist::
print()
}
資料結構練習 雙向鍊錶
複習一下資料結構。說不准下個星期就用上了 只不過寫的很簡單,沒有封裝 doublelinklist.h ifndef guard doublelinklist h define guard doublelinklist h include struct listnode listnode getne...
資料結構基礎 鍊錶練習
例題6 4 破損的鍵盤 鍊錶 include include const int maxn 100000 5 int last,cur,next maxn 游標位於cur號字母的後面 char s maxn int main for int i next 0 i 0 i next i printf ...
資料結構鍊錶理解練習
有效輸入域檢測 bool chec int max,int k cout the number must 1 to max return false 輸出有效資料 void put const int a n int n cout for int i 0 i n i 刪除int del int a ...