Data structure
pointer
linked list
make stack with array
make stack with std::stack
make stack with linked list
problems
Pointer
定義:指標變數
儲存其他變數(資料)存在的記憶起位置
1
int n
0x100
0x100
int *ptr
0x234
Type | Name | Value | Position |
---|---|---|---|
int | n | 1 | 0x100 |
int* | ptr | 0x100 | 0x234 |
int i; //int
int *ptr;//int pointer
# PRESENTING CODE
int i; //int
int *ptr;//int pointer
ptr = &i;
# PRESENTING CODE
int i; //int
int *ptr;//int pointer
ptr = &i;//position of i
int i; //int
int *ptr;//int pointer
ptr = &i;
# PRESENTING CODE
int i = 1; //int
int *ptr;//int pointer
ptr = &i;//position of i
int r = *ptr;//r=1
//value of the variable at ptr
*ptr = 5;//*ptr=i=5
r = 4;//r=4!=i
1
int i
0x100
0x100
int *ptr
0x234
Type | Name | Value | Position |
---|---|---|---|
int | i | 1 | 0x100 |
int* | &i | 0x100 | |
int* | ptr | 0x100 | 0x234 |
int | *ptr | 1(i) | 0x100 |
int |
|
1(i) | 0x100 |
1
int r
0x100
Independent
Linked list
null
0
null
First
curr
null
1
null
First
curr
0
Insert
prev
null
1
null
First
curr
0
Remove
prev
null
1
null
First
curr
0
Search(1)
prev
2
null
1
null
First
curr
0
Search(1)
prev
2
Array | Linked list | |
---|---|---|
長度 | 不能 | 可以 |
push | O(n) | O(1) |
insert | O(n) | O(1) |
remove | O(n) | O(1) |
循序取得 | O(n) | O(n) |
隨機取得 | O(1) | O(n) |
Stack
定義:
Stack是一種資料結構,遵循著後進先出的原則,最晚放入堆疊的資料會被最先取出(LIFO Last-In-First-Out)
0
1
2
3
4
0
1
2
3
4
0
1
5
6
7
須製作:
Make Stack with Array
會遇到什麼問題:
長度無法預測,需判斷是否已滿
#include <iostream>
#define stack_length 10
using namespace std;
int stack[stack_length];
int curr = -1;
bool push(int n) {
if (curr < stack_length - 1) {
stack[++curr] = n;
return true;
}
return false;
}
bool pop() {
if (curr >= 0) {
curr--;
return true;
}
return false;
}
int get() {
if (curr >= 0) {
return stack[curr];
}
return -1;
}
bool isEmpty() {
return curr == -1;
}
bool isFull() {
return curr == stack_length - 1;
}
int main() {
int i;
while (cin>>i){
switch (i) {
case -1:
cout<< pop()<<endl;
break;
case -2:
cout<<get()<<endl;
break;
case -3:
cout<<isEmpty()<<endl;
break;
case -4:
cout<<isFull()<<endl;
break;
default:
cout<<push(i)<<endl;
}
}
return 0;
}
# PRESENTING CODE
Make Stack with Linked list
#include <iostream>
using namespace std;
struct node {
int data;
node *next;
};
node *curr;
void init() {
curr = nullptr;
}
bool push(int n) {
node *temp = curr;
curr = new node;
curr->next = temp;
curr->data = n;
}
bool pop() {
if (curr != nullptr) {
node *temp = curr;
curr = curr->next;
delete temp;
return true;
}
return false;
}
int get() {
if (curr != nullptr) {
return curr->data;
}
return -1;
}
bool isEmpty() {
return curr == nullptr;
}
int main() {
init();
int i;
while (cin >> i) {
switch (i) {
case -1:
cout << pop() << endl;
break;
case -2:
cout << get() << endl;
break;
case -3:
cout << isEmpty() << endl;
break;
default:
cout << push(i) << endl;
}
}
return 0;
}
# PRESENTING CODE
Make Stack with std::stack
#include <iostream>
#include <stack>
using namespace std;
stack<int> s;
bool push(int n) {
s.push(n);
}
bool pop() {
if (!s.empty()) {
s.pop();
return true;
}
return false;
}
int get() {
if (!s.empty()) {
return s.top();
}
return -1;
}
int main() {
int i;
while (cin >> i) {
switch (i) {
case -1:
cout << pop() << endl;
break;
case -2:
cout << get() << endl;
break;
case -3:
cout << s.empty() << endl;
break;
default:
cout << push(i) << endl;
}
}
return 0;
}
# PRESENTING CODE
Problems