以下代碼僅供參考:
#include <iostream>
using namespace std;
typedef int Status;
struct node { //定義結(jié)點類型;
Status data;
node *next;
};
node *creat_linklist() { //創(chuàng)建鏈表;
node *head;
node *p = new node;
head = p;
p -> next = nullptr;
head -> next = nullptr;
return head;
}
node *push_front(node *head,Status num) { //頭插法;
node *p = new node;
p -> data = num;
p -> next = head -> next;
head -> next = p;
return head;
}
node *push_back(node *head,Status num) { //尾插法;
node *p = new node;
p -> data = num;
node *ptail;
for (ptail = head;ptail -> next;ptail = ptail -> next);
ptail -> next = p;
ptail = p;
return head;
}
int statistic_element(node *head) { //統(tǒng)計鏈表中元素的個數(shù);
int len = 0;
node *pi;
for (pi = head -> next;pi;pi = pi -> next) {
len++;
}
return len;
}
node *selectsort(node *head) { //鏈表的排序(元素從小到大);
node *pi,*pj;
node tnode;
if (!head -> next) {
return 0;
}
for (pi = head -> next;pi -> next;pi = pi -> next) {
for (pj = pi -> next;pj;pj = pj -> next) {
if (pi -> data > pj -> data) {
tnode = *pi;
*pi = *pj;
*pj = tnode;
node *tnode = pi -> next;
pi -> next = pj -> next;
pj -> next = tnode;
}
}
}
return head;
}
int find_node(node *head,Status num1) { //查找指定元素在鏈表中的個數(shù);
node *pi;
int count = 0;
//node *pr = nullptr;
if (head -> next) {
for (pi = head -> next;pi;pi = pi -> next) {
if (pi -> data == num1) {
count++;
}
}
}
return count;
}
node *delete_linklist(node *head,Status num1) { //刪除鏈表中的結(jié)點;
node *pi;
node *pfront,*pnext;
for (pi = head;pi -> next;pi = pi -> next) {
if (pi -> next -> data == num1) {
pfront = pi;
pnext = pi -> next;
break;
}
}
pfront -> next = pnext -> next;
delete pi -> next;
return head;
}
node *find_plast(node *head) { //查找鏈表中的尾元素;
node *plast = nullptr;
if (head -> next) {
for (plast = head;plast -> next;plast = plast -> next);
}
return plast;
}
node *insert_selectedlinklist(node *head,Status num2) { //按順序插入鏈表;
node *p = new node;
p -> data = num2;
if (head -> next == nullptr) {
head -> next = p;
p -> next = nullptr;
}
else {
node *plast = find_plast(head);
if (num2 > plast -> data) {
plast -> next = p;
p -> next = nullptr;
}
else if (p -> data < head -> next -> data) {
node *pt = head -> next;
head -> next = p;
p -> next = pt;
}
else {
node *pfront,*pnext;
node *pi;
for (pi = head -> next;pi -> next;pi = pi -> next) {
if (pi -> next -> data >= p -> data) {
pfront = pi;
pnext = pi -> next;
break;
}
}
pfront -> next = p;
p -> next = pnext;
}
}
return head;
}
void output(node *head) { //輸出鏈表中的元素;
node *pi;
for (pi = head -> next;pi;pi = pi -> next) {
cout << pi -> data << " ";
}
cout << endl;
}
int main() { //測試;
node *head = creat_linklist();
output(head);
Status num;
cout << "input num:" << endl;
while (cin >> num && num) {
//head = push_front(head,num);
head = push_back(head,num);
}
output(head);
int len = statistic_element(head);
cout << "len = " << len << endl;
head = selectsort(head);
cout << "selected:" << endl;
output(head);
Status num1;
cout << "What number do you want to delete?" << endl;
cin >> num1;
int count = find_node(head,num1);
if (count == 0) {
cout << "This linklist hasn't this num!" << endl;
}
else {
while (count--) {
head = delete_linklist(head,num1);
}
}
output(head);
cout << "input number???" << endl;
Status num2;
while (cin >> num2 && num2) {
head = insert_selectedlinklist(head,num2);
}
output(head);
return 0;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者