c++實現鏈表的抽象數據結構

#include
#include<stdio.h>
using namespace std;


static const int arr[]={11,2,31,4};
static const int length=sizeof(arr)/sizeof(int);


typedef struct listnode{
int data;
struct listnode * next;
}node,*pnode;


class LIST{
public:
LIST(){
head=NULL;
}
~LIST(){
while(head){
pnode tmp=head->next;
delete head;
head=NULL;
head=tmp;
}
}
pnode createList();                              //1.新建單鏈表
int sizeList(pnode head);                        //2.單鏈表長度
void print(pnode head) const;                    //3.打印
    int deleteList(pnode head, int value);           //4.刪除某個節點
int insertList(pnode head,int value);            //5.插入某個節點
pnode sort(pnode head);                          //7.單鏈表排序
pnode reverseList(pnode head);                   //8.單鏈表逆轉
pnode researchMid(pnode head);                   //9.單鏈表中間節點
private:
pnode head;
};




pnode LIST::createList() {
int i=0;
pnode head = new node;
head->data=-1;
pnode tmp=head;
for(;i<length;++i) {
pnode p1=new node;
p1->data=arr[i];
tmp->next=p1;
tmp=p1;
}
tmp->next=NULL;
return head;
}


void LIST::print(pnode head) const {
pnode tmp=head->next;
while(tmp){
cout<<tmp->data<<"  ";
tmp=tmp->next;
}
cout<<""<<endl;
}


int LIST::sizeList(pnode head) {
pnode tmp=head->next;
int size=0;
while(tmp){
size++;
tmp=tmp->next;
}
return size;
}


int LIST::deleteList(pnode head,int value) {
pnode p1=head;
pnode p2=p1->next;
while(p2&&p2->data!=value) {
p1=p2;
p2=p2->next;
}
if(p2){
p1->next=p2->next;
delete p2;
p2=NULL;
return 1;
}
else{
return -1;
}
}


int LIST::insertList(pnode head,  int value) {
pnode p0=new node;
p0->data=value;
pnode p1=head;
pnode p2=head->next;
while(p2 && value>p2->data ) {              //關鍵代碼
p1=p2;
p2=p2->next;
}
p1->next=p0;
p0->next=p2;
return 1;
}


pnode LIST::sort(pnode head){
if(NULL==head->next){
return NULL;
}
int size=sizeList(head);
int i,j;
for(i=1;i<=size;++i){                   //冒泡
pnode p=head;
for(j=0;j<=size-i;++j) {
if(p->data > p->next->data){
int tmp=p->data;
p->data=p->next->data;
p->next->data=tmp;
}
p=p->next;
}
}
return head;
}


pnode LIST::reverseList(pnode head) {
pnode p0=head->next;//原先鏈表模型: head->p0->p1
    pnode p1=NULL;
head->next=NULL;  //新建一個鏈表
while(p0){        //從鏈表的第一個結點開始遍歷
p1=p0->next;
p0->next=head->next; //頭插法繼續新建鏈表
head->next=p0;
p0=p1;
}
return head;
}


pnode LIST::researchMid(pnode head) {
if(NULL==head || NULL==head->next) {
return NULL;
}
pnode p0=head->next; //快指針
pnode p1=head->next; //慢指針
while(p0->next->next){
p1=p1->next;
p0=p0->next->next;
}
return p1;

}


#include"list.h"


int main(){
LIST *list=new LIST;
cout<<"create list: ";
pnode head = list->createList();
list->print(head);
if(list->deleteList(head,6)==1) {
list->print(head);
}
cout<<"after insert two number:  ";
if(list->insertList(head,6)==1) {
list->insertList(head,5);
list->print(head);
}
else{
cout<<"connot success insert number~"<<endl;
}
cout<<"list's size:  ";
cout<<list->sizeList(head)<<endl;
cout<<"after sort:  ";
list->sort(head);
list->print(head);
cout<<"reverse list:"<<endl;
list->reverseList(head);
list->print(head);
cout<<"mid value is: ";
pnode midNode=list->researchMid(head);
cout<<midNode->data<<endl;
system("pause");
return 1;
}

參考:

http://www.cnblogs.com/EricYang/archive/2009/09/06/1561353.html

http://blog.csdn.net/huzhen919/article/details/4296220

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章