1. 程式人生 > >C++學習之連結串列

C++學習之連結串列

1.1建立與遍歷連結串列

#include<iostream>
using namespace std;

struct Student {
	long number;
	float score;
	Student*next;
};

Student * head;//定義頭結點

Student*Create() {
	Student*pS;
	Student*pEnd;
	pS = new Student;
	cin >> pS->number >> pS->score;
	head = NULL;
	pEnd = pS;

	while (pS->number != 0) {
		if (head == NULL)
			head = pS;//如果頭結點內容為空,則將其指標指向首結點
		else
			pEnd->next = pS;
		pEnd=pS;
		pS = new Student;
		cin >> pS->number >> pS->score;
	}
	pEnd->next = NULL;
	delete pS;
	return head;
}
void ShowList(Student*head) {
	cout << "now the items of list are.\n";
	while (head) {
		cout << head->number << "," << head->score << endl;
		head = head->next;
	}
}
int main() {
	ShowList(Create());
}

建立三個結構體指標:head, pEnd, pS ,用來表示頭結點,和兩個用於傳遞和連線的結點指標。當 pS 指向的結點被賦值之後, pEnd 便會將目前指向的結點的尾指標指向 pS ,之後再將自身指向 pS 用於取代,之後 pS 重新分配空間,指向下一個將要被賦值的地方,完成傳遞。

 

1.2 刪除連結串列結點

#include<iostream>
using namespace std;

struct Student {
	long number;
	float score;
	Student*next;
};

Student * head;//定義頭結點

Student*Create() {
	Student*pS;
	Student*pEnd;
	pS = new Student;
	cin >> pS->number >> pS->score;
	head = NULL;
	pEnd = pS;

	while (pS->number != 0) {
		if (head == NULL)
			head = pS;
		else
			pEnd->next = pS;
		pEnd=pS;
		pS = new Student;
		cin >> pS->number >> pS->score;
	}
	pEnd->next = NULL;
	delete pS;
	return head;
}
void ShowList(Student*head) {
	cout << "now the items of list are.\n";
	while (head) {
		cout << head->number << "," << head->score << endl;
		head = head->next;
	}
}

void Delete(Student*head, long number) {
	Student*p;
	if (!head) {
		cout << "\nList null!\n";
		return;
	}
	if (head->number == number) {
		p = head;
		head = head->next;
		delete p;
		cout << number << " the head of list have been deleted.\n";
		return;
	}
	for (Student*pGuard = head; pGuard->next; pGuard = pGuard->next) {
		if (pGuard->next->number == number) {
			p = pGuard->next;
			pGuard->next = p->next;
			delete p;
			cout << number << " have been deleted.\n";
			return;
		}
	}
	cout << number << " is not found!\n";
}

int main() {
	Create();
	Delete(head, 54);
	ShowList(head);
}

 

1.3插入連結串列結點

#include<iostream>
using namespace std;

struct Student {
	long number;
	float score;
	Student*next;
};

Student * head;//定義頭結點

Student*Create() {
	Student*pS;
	Student*pEnd;
	pS = new Student;
	cin >> pS->number >> pS->score;
	head = NULL;
	pEnd = pS;

	while (pS->number != 0) {
		if (head == NULL)
			head = pS;
		else
			pEnd->next = pS;
		pEnd=pS;
		pS = new Student;
		cin >> pS->number >> pS->score;
	}
	pEnd->next = NULL;
	delete pS;
	return head;
}
void ShowList(Student*head) {
	cout << "now the items of list are.\n";
	while (head) {
		cout << head->number << "," << head->score << endl;
		head = head->next;
	}
}

void Insert(Student*head, Student*stud) //在原連結串列按資料從小到大排列的情況下依次序插入資料
{
	if (head == NULL) {
		head = stud;
		stud->next = NULL;
		return;
	}
	if (head->number > stud->number) {
		stud->next = head;
		head = stud;
		return;
	}
	Student*pGuard = head;
	while (pGuard->next&&pGuard->next->number < stud->number)
		pGuard = pGuard->next;
	stud->next = pGuard->next;
	pGuard->next = stud;

}

int main() {
	Student ps;
	ps.number = 36;
	ps.score = 3.8;
	head=Create();
	Insert(head,&ps);//head是全域性變數
	ShowList(head);
}

 

1.4 Josephus 問題

// Josephus 問題
#include<iostream>
#include<iomanip>
using namespace std;
struct jose {
	int code;
	jose*next;
};
int main() {
	int numOfBoys, interval;
	cout << "Please ubput the number of boys,\n"//小孩數
		<< "         interval of counting:\n";//數小孩個數
	cin >> numOfBoys >> interval;
	//建立小孩結構陣列
	jose*pJose = new jose[numOfBoys];
	jose*pCurrent = pJose;//當前結點指標(第一個陣列元素pJose[0])
	//初始化結構陣列:構成環鏈、小孩編號、輸出編號
	int itemsInLine = 0;//輸出項數
	for (int i = 1; i <= numOfBoys; i++) {
		pCurrent->next = pJose + i % numOfBoys;
		pCurrent->code = i;
		pCurrent = pCurrent->next;//迴圈給陣列賦值,並且連結,構成環形連結串列
		if (itemsInLine++ % 10 == 0)//輸出格式:每10個數字換行
			cout << endl;
		cout << setw(4) << i;//輸出格式,4個字元靠右顯示
	}
	itemsInLine = 0;//格式
	jose*pivot;//定義哨兵指標
	pCurrent = &pJose[numOfBoys - 1];//做好數的準備,若以第一個元素為起始
	while (pCurrent->next != pCurrent) {//若只剩最後一個元素,即獲勝者,括號中判定為false,環中只一個元素,則尾指標指向首
		for (int j = 0; j < interval; j++) {
			pivot = pCurrent;
			pCurrent = pivot->next;//傳遞
		}
		if (itemsInLine++ % 10 == 0)
			cout << endl;
		cout << setw(4) << pCurrent->code;
		pivot->next = pCurrent->next;
		pCurrent = pivot;//點到的小孩脫鏈
	}
	cout << "\n\nthe winner is "
		<< pCurrent->code << endl;
	delete[]pJose;
	return 0;
}

 

在C++中,setw(int n)用來控制輸出間隔。//轉

設定輸出幾個字元的格式(靠右),比如int i = 10,你要輸出i如果setw(20),那麼 i 的前面有18個空格。

例如:cout<<'s'<<setw(8)<<'a'<<endl;則在螢幕顯示sa//s與a之間有7個空格,setw()只對其後面緊跟的輸出產生作用,如上例中,表示'a'共佔8個位置,不足的用空格填充。

若輸入的內容超過setw()設定的長度,則按實際長度輸出。setw()預設填充的內容為空格,可以setfill()配合使用設定其他字元填充。

cout<<setfill('*')<<setw(5)<<'a'<<endl;則輸出:****a //4個*和字元a共佔5個位置。