2. 单链表
// LinkSeqList.h
#include <iostream>
#include <cstring>
using namespace std;
struct node{
int data1;
char data2[10];
node *next;
};
typedef node* ptrN;
class CLinkSeqList{
public:
CLinkSeqList():
head(NULL),
p1(NULL),
p2(NULL),
a(0),
m_length(0),
m_count(0)
{
char *ch = new char[20];
cout <<"input -1 to exit : ";
while(1){
cin >>a;
if (a == -1) break;
cin >> ch;
p1 = new node;
m_length++;
p1->data1 = a;
strcpy(p1->data2, ch);
if (head == NULL)
{
head = p1;
p2 = p1;
}
else
{
p2->next = p1;
p2 = p1;
}
}
if (head) p2->next = NULL;
delete ch;
}
void outputNodesl()
{
ptrN nodeN;
int i = 1;
nodeN = head;
while (nodeN)
{
cout << i << " Node :" << nodeN->data1 <<'\t'<<nodeN->data2 <<endl;
nodeN= nodeN->next;
i++;
}
}
ptrN accessNode(int n)
{
ptrN tmpNode;
if (head == NULL)
return NULL;
else
{
tmpNode = head;
}
for (m_count = 0; m_count <n-1; m_count++){
if( tmpNode->next == NULL)
return NULL;
tmpNode = tmpNode->next;
}
return tmpNode;
}
ptrN accessTrail()
{
ptrN tmpNode;
if (head == NULL)
return NULL;
else
{
tmpNode = head;
}
while (tmpNode->next)
{
tmpNode = tmpNode->next;
}
return tmpNode;
}
bool insertNode(ptrN newNode, int n)
{
ptrN pPre, pBac;
if (head == NULL){
head = newNode;
head->next = NULL;
}
else if (n == 1){
pBac = head;
head = newNode;
head->next = pBac;
}
else{
pPre = accessNode(n-1);
if (pPre && pPre->next ){
pBac = pPre->next;
pPre->next = newNode;
newNode->next = pBac;
}
else if (pPre &&pPre->next == NULL){
pPre = accessTrail();
pPre->next = newNode;
newNode->next = NULL;
}
else
return false;
}
return true;
}
bool deleteNode(int n){
ptrN pCur, pPre, pBac;
if (head == NULL){
return false;
}
else if (n == 1){
pCur = head;
head = head->next;
delete pCur;
}
else{
pCur = accessNode(n);
if (pCur && pCur->next){
pPre = accessNode(n-1);
pBac = (pPre->next)->next;
pPre->next = pBac;
delete pCur;
}
else if (pCur&& pCur->next == NULL){
pPre = accessNode(n-1);
pPre->next = NULL;
delete pCur;
}
else if (pCur==NULL)
{
cout <<"There is no Node to delete in sequence link list"<<endl;
return false;
}
}
return true;
}
~CLinkSeqList()
{
ptrN tmpNode;
while (head)
{
tmpNode = head;
head = head->next;
delete tmpNode;
}
cout<<"clear all Nodes in Sequence list"<<endl;
}
int Length() {return m_length;}
private:
ptrN p1, p2, head;
int a;
int m_length;
int m_count;
};
// Test_LinkSeqList.cpp
#include <iostream>
#include <cstring>
#include "LinkSeqList.h"
using namespace std;
int main()
{
cout << " Create Non Seqenced Link List :"<<endl;
CLinkSeqList list;
cout << " CLinkSeqList Max Lenght = " << list.Length() <<endl;
cout <<"output all nodes in link list:"<<endl;
list.outputNodesl();
cin.get();
cout <<"Insert a node"<<endl;
ptrN tmpNode = new node;
tmpNode->data1 = 5;
strcpy(tmpNode->data2, "Insert Node");
list.insertNode(tmpNode, 3);
cout <<"output all nodes in link list:"<<endl;
list.outputNodesl();
cin.get();
cout << "delete 2nd node"<<endl;
list.deleteNode(2);
cout <<"output all nodes in link list:"<<endl;
list.outputNodesl();
return 0;
}
Create Non Seqenced Link List :
input -1 to exit : 10 China
20 Korea
30 American
40 France
50 German
-1
CLinkSeqList Max Lenght = 5
output all nodes in link list:
1 Node :10 China
2 Node :20 Korea
3 Node :30 American
4 Node :40 France
5 Node :50 German
Insert a node
output all nodes in link list:
1 Node :10 China
2 Node :20 Korea
3 Node :5 Insert Node
4 Node :30 American
5 Node :40 France
6 Node :50 German
delete 2nd node
output all nodes in link list:
1 Node :10 China
2 Node :5 Insert Node
3 Node :30 American
4 Node :40 France
5 Node :50 German
clear all Nodes in Sequence list
浙公网安备 33010602011771号