1. 程式人生 > >連結串列,不用類

連結串列,不用類

#include<iostream>
#define ok 0
#define error -1
using namespace std;
int num ;


struct ListNode {
	int data;
	ListNode*next;
};

void LL_display(ListNode*head) {
	ListNode *p = head->next;
	while (p != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
ListNode* LL_index(ListNode*head,int i) {
	if (i > num || i < 1) {
		return NULL;
	}
	int j = 1;
	for (ListNode*pG = head->next; pG->next, j <= i; pG = pG->next, j++) {
		if (j == i) {
			return pG;
		}
	}
}

int LL_get(ListNode*head,int i) {
	if (i > num || i < 1) {
		return error;
	}
	ListNode*num = LL_index(head,i);
	if (num == NULL)
		return error;
	else
		return num->data;
}

int LL_insert(ListNode*head,int i, int item) {
	if (i < 1 || i> num + 1)
		return error;
	ListNode*pI = new ListNode;
	if (!head->next&&i == 1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		num++;
		return ok;
	}
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	ListNode*pU = new ListNode;
	pU->next = pG->next;
	pG->next = pU;
	pU->data = item;
	num++;
	return ok;
}
int LL_del(ListNode*head,int i) {
	if (i > num || i<1)
		return error;
	ListNode*pI = new ListNode;
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return error;
	pI = pG->next;
	pG->next = pI->next;
	delete pI;
	num--;
	return ok;
}

int main() {
	ListNode*sa = new ListNode;//不 new 的話不會分配記憶體
	sa->data = -1;
	sa->next=NULL;
	num = 0;
	int len;
	cin >> len;
	int number;
	for (int i = 0; i < len; i++) {
		cin >> number;
		LL_insert(sa,i + 1, number);
	}
	LL_display(sa);

	int i, item, k;

	cin >> i >> item;
	k = LL_insert(sa,i, item);
	if (k == 0)
		LL_display(sa);
	else
		cout << "error" << endl;

	cin >> i >> item;
	k = LL_insert(sa,i, item);
	if (k == 0)
		LL_display(sa);
	else
		cout << "error" << endl;

	cin >> i;
	k = LL_del(sa,i);
	if (k == 0)
		LL_display(sa);
	else
		cout << "error" << endl;

	cin >> i;
	k = LL_del(sa,i);
	if (k == 0)
		LL_display(sa);
	else
		cout << "error" << endl;

	cin >> i;
	k = LL_get(sa,i);
	if (k == -1)
		cout << "error" << endl;
	else
		cout << k << endl;

	cin >> i;
	k = LL_get(sa, i);
	if (k == -1)
		cout << "error" << endl;
	else
		cout << k << endl;

	return 0;
}

 

 

 

 

迴圈連結串列:

#include<iostream>
using namespace std;
int num;


struct ListNode {
	int data;
	ListNode*next;
};

void display(ListNode*head) {
	ListNode *p = head->next;
	while (p != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
ListNode* index(ListNode*head, int i) {
	int j = 1;
	for (ListNode*pG = head->next; pG->next, j <= i; pG = pG->next, j++) {
		if (j == i) {
			return pG;
		}
	}
}

int get(ListNode*head, int i) {
	ListNode*n = index(head, i);
	if (n == NULL)
		return 0;
	else
		return n->data;
}

int insert(ListNode*head, int i, int item) {
	ListNode*pI = new ListNode;
	if (!head->next&&i == 1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		num++;
		return 1;
	}
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return 0;
	ListNode*pU = new ListNode;
	pU->next = pG->next;
	pG->next = pU;
	pU->data = item;
	num++;
	return 1;
}
int del(ListNode*head, int i) {
	ListNode*pI = new ListNode;
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return 0;
	pI = pG->next;
	pG->next = pI->next;
	delete pI;
	num--;
	return 1;
}

int onlyOne(ListNode*head) {
	if (head->next == head) {
		return 1;
	}
	else {
		return 0;
	}
}

int main() {
	int len;//人數
	while (scanf("%d", &len) == 1) {
		ListNode*sa = new ListNode;//不 new 的話不會分配記憶體
		sa->data = -1;
		sa->next = NULL;
		num = 0;
		int number;
		for (int i = 0; i < len; i++) {
			scanf("%d", &number);//輸入每個人持有的密碼
			insert(sa, i + 1, number);
		}//通過for迴圈一個一個連入連結串列
		ListNode* p1 = index(sa, len);
		ListNode* p2 = index(sa, len - 1);
		sa->data = p1->data;
		p2->next = sa;//首尾連線構成環鏈
		int m;
		int key;
		scanf("%d", &m);
		ListNode*start = sa;
		while (!onlyOne(start)) {
			ListNode*p = index(start, m);
			key = p->data;
			ListNode*q = index(start, m - 1);
			del(start, m);
			start = q;
			//迴圈連結串列可能轉幾圈遍歷所有,這樣刪掉的元素被 跳過,造成混亂 
			m = key;
			printf("%d ", key);
		}
		printf("%d\n", start->data);
	}
	return 0;
}

現在的問題是,迴圈終止條件,只有單個元素的環形連結串列的判斷

#include<iostream>
using namespace std;
int num;


struct ListNode {
	int data;
	ListNode*next;
};

void display(ListNode*head) {
	ListNode *p = head->next;
	while (p != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
ListNode* index(ListNode*head, int i) {
	if (i == 0) {
		return head;
	}
	int j = 1;
	for (ListNode*pG = head->next; pG->next, j <= i; pG = pG->next, j++) {
		if (j == i) {
			return pG;
		}
	}
}

int get(ListNode*head, int i) {
	ListNode*n = index(head, i);
	if (n == NULL)
		return 0;
	else
		return n->data;
}

int insert(ListNode*head, int i, int item) {
	ListNode*pI = new ListNode;
	if (!head->next&&i == 1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		num++;
		return 1;
	}
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return 0;
	ListNode*pU = new ListNode;
	pU->next = pG->next;
	pG->next = pU;
	pU->data = item;
	num++;
	return 1;
}
int del(ListNode*head, int i) {
	ListNode*pI = new ListNode;
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return 0;
	pI = pG->next;
	pG->next = pI->next;
	delete pI;
	num--;
	return 1;
}

int onlyOne(ListNode*head) {
	if (head->next == head) {
		return 1;
	}
	else {
		return 0;
	}
}

int main() {
	int len;//人數
	while (scanf("%d", &len) == 1) {
		ListNode*sa = new ListNode;//不 new 的話不會分配記憶體
		sa->data = -1;
		sa->next = NULL;
		num = 0;
		int number;
		for (int i = 0; i < len; i++) {
			scanf("%d", &number);//輸入每個人持有的密碼
			insert(sa, i + 1, number);
		}//通過for迴圈一個一個連入連結串列
		ListNode* p1 = index(sa, len);
		ListNode* p2 = index(sa, len - 1);
		sa->data = p1->data;
		p2->next = sa;//首尾連線構成環鏈
		int m;
		int key;
		scanf("%d", &m);
		ListNode*start = sa;
		while (!onlyOne(start)) {
			ListNode*p = index(start, m);
			key = p->data;
			ListNode*q = index(start, m - 1);
			del(start, m);
			start = q;
			//迴圈連結串列可能轉幾圈遍歷所有,這樣刪掉的元素被 跳過,造成混亂 
			m = key;
			printf("%d ", key);
		}
		printf("%d\n", start->data);
	}
	return 0;
}

成功,對 i = 0 ,的查詢沒有修改。屬於細節地方出問題,要善於運用debug。

精簡和註釋

 

#include<stdio.h>

//定義結構體
struct ListNode {
	int data;
	ListNode*next;
};

//查詢:返回連結串列中第i個元素的地址
ListNode* index(ListNode*head, int i) {
	if (i == 0) {
		return head;
	}
	int j = 1;
	for (ListNode*pG = head->next; pG->next, j <= i; pG = pG->next, j++) {
		if (j == i) {
			return pG;
		}
	}
}

//插入:在i位置插入元素
int insert(ListNode*head, int i, int item) {
	ListNode*pI = new ListNode;
	if (!head->next&&i == 1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		return 1;
	}
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return 0;
	ListNode*pU = new ListNode;
	pU->next = pG->next;
	pG->next = pU;
	pU->data = item;
	return 1;
}


//刪除:刪除i處元素
int del(ListNode*head, int i) {
	ListNode*pI = new ListNode;
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return 0;
	pI = pG->next;
	pG->next = pI->next;
	delete pI;
	return 1;
}

//判斷環連結串列是否只剩一下一個元素
int onlyOne(ListNode*head) {
	if (head->next == head) {
		return 1;
	}
	else {
		return 0;
	}
}

int main() {
	int len;//人數

	//用while() 實現多組輸入
	while (scanf("%d", &len) == 1) {
		//建立單向連結串列
		ListNode*sa = new ListNode;
		sa->data = -1;
		sa->next = NULL;
		int number;
		for (int i = 0; i < len; i++) {
			scanf("%d", &number);
			insert(sa, i + 1, number);
		}
		//構成環連結串列
		ListNode* p1 = index(sa, len);
		ListNode* p2 = index(sa, len - 1);
		sa->data = p1->data;
		p2->next = sa;
		//輸入第一個報數值
		int m;
		int key;
		scanf("%d", &m);
		ListNode*start = sa;
		//依次報數並出列
		while (!onlyOne(start)) {
			ListNode*p = index(start, m);
			key = p->data;
			ListNode*q = index(start, m - 1);
			del(start, m);
			start = q;
			m = key;
			printf("%d ", key);
		}
		printf("%d\n", start->data);
	}
	return 0;
}

 

 

多項式:

#include<stdio.h>

struct list{
	int coef;
	int power;
	list* next;
};


/*void display(ListNode*head) {
	ListNode *p = head->next;
	while (p != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}*/
ListNode* index(ListNode*head, int i) {
	if (i == 0) {
		return head;
	}
	int j = 1;
	for (ListNode*pG = head->next; pG->next, j <= i; pG = pG->next, j++) {
		if (j == i) {
			return pG;
		}
	}
}
 
int get(ListNode*head, int i) {
	ListNode*n = index(head, i);
	if (n == NULL)
		return 0;
	else
		return n->data;
}
 
int insert(ListNode*head, int i, int item) {
	ListNode*pI = new ListNode;
	if (!head->next&&i == 1) {
		head->next = pI;
		pI->data = item;
		pI->next = NULL;
		num++;
		return 1;
	}
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return 0;
	ListNode*pU = new ListNode;
	pU->next = pG->next;
	pG->next = pU;
	pU->data = item;
	num++;
	return 1;
}
int del(ListNode*head, int i) {
	ListNode*pI = new ListNode;
	int j = 0;
	ListNode*pG = head;
	while (pG->next&&j < i - 1) {
		pG = pG->next;
		j++;
	}
	if (!pG || j > i - 1)
		return 0;
	pI = pG->next;
	pG->next = pI->next;
	delete pI;
	num--;
	return 1;
}



int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
	}
	
	
}