這種存儲結構,仍需要預先分配一個較大的空間,但在作爲線性表的插入和刪除操作時不需移動元素,僅需修改指針,故仍具有鏈式存儲結構的主要優點。
#include<stdio.h>
struct cirt
{
int a;
struct cirt *next;
};
void main(){
struct cirt *head = NULL;
struct cirt n1, n2, n3, n4, n5, n6;
n1.a = 100;
n2.a = 0;
n3.a = 125;
n4.a = 560;
n5.a = 320;
n6.a = 888;
head = &n1;
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = NULL;
struct cirt n8; //增加一個n8變量
n8.a = 12000;
n3.next = &n8;
n8.next = &n4;
n2.next = &n8; //刪除一個n3變量
n3.next = NULL;
while (head!=NULL)
{
printf("鬼啊%d\n", head->a);
head = head->next;
}
getchar();
return;
}
通過一個簡單的小實例,可以簡單的感受到鏈表其就像小孩玩具中的拼接玩具一樣去對需要的節數進行操作即可。類用於指定對象的形式,它包含了數據表示法和用於處理數據的方法。類中的數據和方法稱爲類的成員。函數在一個類被稱爲類的成員。
類定義是以關鍵字 class 開頭,後跟類的名稱。
#include <iostream>
using namespace std;
class Box
{
public:
double length; // 長度
double breadth; // 寬度
double height; // 高度
};
int main( )
{
Box Box1; // 聲明 Box1,類型爲 Box
Box Box2; // 聲明 Box2,類型爲 Box
double volume = 0.0; // 用於存儲體積
// box 1 詳述
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 詳述
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// box 1 的體積
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Box1 的體積:" << volume <<endl;
// box 2 的體積
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Box2 的體積:" << volume <<endl;
return 0;
}
運行結果:Box1 的體積:210
Box2 的體積:1560
今天開始學習C++,感受到了學習的壓力以及進度的不適應,明顯與C語言之間感覺相差感還是很大的。