C++数据结构学习
双向链表练习 构建有头结点和尾结点的双向链表,为了使交换的操作更加方便,所以增加了冗余的头结点和尾结点。
MyList实现的功能
方法 功能 add 添加元素,int类型 insert 指定下标,插入元素 swap 交换链表里的两个数 remove 从链表移除某个数
#pragma once
#include <iostream>
#include <list>
using namespace std;
typedef struct Node
{
double num;
Node * pre;
Node * next;
} ;
class MyList
{
public :
MyList ( ) ;
~ MyList ( ) ;
bool add ( double num) ;
bool remove ( double num) ;
bool insert ( double num, int index) ;
bool swap ( int a, int b) ;
Node * findNodeByNum ( int num) ;
void showList ( ) ;
private:
int size= 0 ;
Node * currentNode;
Node * head;
Node * tail;
}
#include "MyList.h"
MyList: : MyList ( ) {
this-> size = 0 ;
currentNode = new struct Node;
tail = new struct Node;
tail-> num = 0 ;
tail-> next = NULL ;
tail-> pre = currentNode;
currentNode-> num = 0 ;
currentNode-> pre = NULL ;
currentNode-> next = tail;
head = currentNode;
}
bool MyList: : swap ( int a, int b) {
Node * p = findNodeByNum ( a) ;
Node * q = findNodeByNum ( b) ;
if ( p == NULL || q == NULL ) {
cout << "p or q is not exist,swap fail!!" << endl;
return false;
}
Node * tempNode= new struct Node;
if ( p-> next== q) {
tempNode-> pre = p-> pre;
tempNode-> next = q-> next;
tempNode-> pre-> next = q;
tempNode-> next-> pre = p;
q-> pre = tempNode-> pre;
q-> next = p;
p-> pre = q;
p-> next = tempNode-> next;
}
else if ( q-> next== p) {
tempNode-> pre = q-> pre;
tempNode-> next = p-> next;
tempNode-> pre-> next = p;
tempNode-> next-> pre = q;
p-> pre = tempNode-> pre;
p-> next = q;
q-> pre = p;
q-> next = tempNode-> next;
}
else {
tempNode-> next = p-> next;
tempNode-> pre = p-> pre;
p-> next = q-> next;
p-> pre = q-> pre;
q-> next = tempNode-> next;
q-> pre = tempNode-> pre;
p-> pre-> next = p;
p-> next-> pre = p;
q-> pre-> next = q;
q-> next-> pre = q;
}
delete tempNode;
return true;
}
bool MyList: : add ( double num) {
if ( findNodeByNum ( num) != NULL ) {
cout << "num is already exist,add fail" << endl;
return false;
}
Node * next = new struct Node;
next-> num = num;
next-> next = tail;
tail-> pre = next;
next-> pre = currentNode;
currentNode-> next = next;
currentNode = next;
size++ ;
return true;
}
bool MyList: : remove ( double num) {
Node * moveNode = findNodeByNum ( num) ;
if ( moveNode == NULL ) {
cout << "num isn't exist,delete fail" << endl;
return false;
}
char confirm;
cout << "Are you sure to remove num?(Press 'y' to confirm)" << endl;
cin >> confirm;
if ( confirm == 'y' || confirm == 'Y' ) {
moveNode-> pre-> next = moveNode-> next;
moveNode-> next-> pre = moveNode-> pre;
delete moveNode;
cout << "remove successful." << endl;
return true;
}
return false;
}
bool MyList: : insert ( double num, int index) {
if ( index > size|| index< 0 ) {
cout << "size of list:" << size << endl;
cout << "index beyond,insert fail!" << endl;
return false;
}
if ( findNodeByNum ( num) != NULL ) {
cout << "the same num has exist,insert fail!" << endl;
return false;
}
if ( index == 0 ) {
Node * newNode = new struct Node;
newNode-> num = 0 ;
newNode-> next = head;
newNode-> pre = NULL ;
head-> num = num;
head-> pre = newNode;
head = newNode;
}
else if ( index== size) {
add ( num) ;
}
else {
Node * newNode = new struct Node;
newNode-> num = num;
Node * findedNode= head;
for ( int i = 0 ; i < index; i++ )
{
findedNode = findedNode-> next;
}
newNode-> next = findedNode-> next;
findedNode-> next-> pre = newNode;
newNode-> pre = findedNode;
findedNode-> next = newNode;
}
return true;
}
Node * MyList: : findNodeByNum ( int num) {
Node * scanNode= head-> next;
bool isFind= false;
while ( scanNode-> next!= NULL ) {
if ( scanNode-> num == num) {
isFind = true;
return scanNode;
}
scanNode = scanNode-> next;
}
cout << "List has no :" << num << endl;
return NULL ;
}
void MyList: : showList ( ) {
Node * scanNode= head-> next;
if ( size == 0 ) {
cout << "the list is empty!" << endl;
}
cout << "Now the list:" << endl;
while ( scanNode-> next!= NULL ) {
cout << scanNode-> num<< "\t" ;
scanNode = scanNode-> next;
}
cout << endl;
}
MyList: : ~ MyList ( ) {
currentNode = head;
while ( currentNode-> next!= NULL ) {
currentNode = currentNode-> next;
delete currentNode-> pre;
}
delete currentNode;
}
#include <iostream>
#include "MyList.h"
using namespace std;
int main ( )
{
std: : cout << "Hello World!\n" ;
MyList listA;
int option= 0 ;
while ( 1 ) {
listA. showList ( ) ;
cout << "1:add num to list.(num)" << endl;
cout << "2:insert num to list.(num index)" << endl;
cout << "3:swap two num in the list.(num1 num2)" << endl;
cout << "4:remove num from list.(num)" << endl;
cout << "5:exit" << endl;
cout << "choose operation:" ;
cin >> option;
double num;
bool isExit = false;
switch ( option)
{
case 1 :
cout << "input the num to add:" ;
cin >> num;
listA. add ( num) ;
break ;
case 2 :
int index;
cout << "input the num and the index to insert:" ;
cin >> num>> index;
listA. insert ( num, index) ;
break ;
case 3 :
double numA, numB;
cout << "input two num to swap:" ;
cin >> numA>> numB;
listA. swap ( numA, numB) ;
break ;
case 4 :
cout << "input the num to remove:" ;
cin >> num;
listA. remove ( num) ;
break ;
case 5 :
isExit = true;
break ;
default :
break ;
}
if ( isExit) { break ; }
}
}