单链表

来源:互联网 发布:anker知乎 编辑:程序博客网 时间:2024/06/15 11:10

题目:  单链表相关算法的实验验证
[实验目的]
验证单链表及其上的基本操作;单链表的封装
[实验内容及要求]
1、定义单链表类。
2、实验验证如下算法的正确性、各种功能及指标:
1)创建单链表;
2)插入操作:分别在当前结点后、表头、表尾插入值为x 的结点;
3)删除操作:分别删除表头结点、表尾结点和当前结点的后继结点;
4)存取操作:分别存取当前结点的值和单链表中第k 个结点的值;
5)查找操作:查找值为x 的元素在单链表中出现的位置(是链表中的第几个元素)。
3、当前结点的引入是为了加速单链表上的操作,使用当前指针指向,至少实现以下功能:
1)初始化:哨位结点 ;
2)查找操作后:若找到,当前指针指向找到的结点,否则不动;
3)插入操作后:当前指针指向新插入的结点,便于连续插入;
4、为便于观察程序的运行结果,设计输出函以图形或表格或其它直观的形式展现、存储计算结果。
5、测试程序时,对所有输入变量取遍各种有代表性的值。
6、为了增强程序的可读性,程序中要有适当的注释。

//main.cpp

#include<iostream>#include"SLList.h"#include<windows.h> #include<conio.h>using namespace std;int main(){    system("color 4f");SLList list;int item=0,temp=0;cout<<"please input 12 data!"<<endl;for(int i=0;i<12;i++){    cin>>temp;    list.Insert(temp);}cout<<endl;system("cls");cout<<"所存储的单链表表是:"<<"\n\n\n";list.Print(); cout<<"存取当前节点的值:"<<"\n\n\n";list.Read(item);cout<<"当前节点的值是:"<<item<<"\n\n\n";cout<<"存取第3个节点的值:"<<"\n\n\n";list.Read(3,item);cout<<"第三个节点的值是:"<<item<<"\n\n\n";cout<<"按任意键继续.........";getch();system("cls");cout<<"插入前的线性表是:"<<"\n\n\n";list.Print(); cout<<"在当前节点后面插入一个值为8的节点:"<<"\n\n";item=8;list.Insert(item);cout<<"插入后的线性表是:"<<"\n\n\n";    list.Print();        cout<<"在表头节点插入一个值为7的节点:"<<"\n\n";item=7;list.InsertFromHead(item);cout<<"插入后的线性表是:"<<"\n\n\n";list.Print();cout<<"在表尾节点插入一个值为6的节点:"<<"\n\n";item=6;list.InsertFromTail(item);cout<<"插入后的线性表是:"<<"\n\n\n";list.Print();cout<<"按任意键继续.........";getch();system("cls");cout<<"所存储的线性表是:"<<"\n\n\n";list.Print();    cout<<"查找值为8的元素的下标:"<<"\n\n\n";int array[30]={0};    item=8;    list.Search(item,array);     cout<<"其下标分别为 :";    for(int i=0;i<30;i++)       if(array[i]!=0)          cout<<" ["<<array[i]<<"]  ";    cout<<"\n\n\n\n";    cout<<"按任意键继续.........";getch();system("cls");cout<<"删除前的线性表是:"<<"\n\n\n";list.Print();cout<<"删除当前节点的后继结点:"<<"\n\n";list.Delete(item);cout<<"当前节点的值是:"<<item<<"\n\n";cout<<"删除后的线性表是:"<<"\n\n\n";    list.Print();        cout<<"删除表头节点:"<<"\n\n";list.DeleteFromHead(item);cout<<"表头节点的值是:"<<item<<"\n\n";cout<<"删除后的线性表是:"<<"\n\n\n";    list.Print();        cout<<"删除表尾节点:"<<"\n\n";    list.DeleteFromTail(item);cout<<"表尾节点的值是:"<<item<<"\n\n";cout<<"删除后的线性表是:"<<"\n\n\n";    list.Print();    cout<<"按任意键继续.........";getch();system("cls");return 0;}

//SLList.h

#ifndef SLLIST_H#define SLLIST_Hstruct SLNode{public:int data;SLNode*next;SLNode(SLNode*nextnode=NULL):next(nextnode) { }    SLNode(const int item,SLNode*nextnode=NULL):data(item),next(nextnode) { };} ;class SLList{private:SLNode*head,*tail,*currptr;int length;public:SLList();~SLList(){clear();}int Length()const {return length;}bool IsEmpty()const{return head->next==NULL;}void clear();bool Pre();bool Next();void SetStart(){currptr=head;}void SetEnd(){currptr=tail;}bool Insert(const int& item);bool InsertFromHead(const int& item){SetStart();Insert(item);}bool InsertFromTail(const int& item){SetEnd();Insert(item);}bool Delete(int& item);bool DeleteFromHead(int& item){SetStart();Delete(item);}     bool DeleteFromTail(int& item){SetEnd();Pre();Delete(item);}    bool Search(const int& item,int (&array)[30]);    bool Read(int& item);    bool Read(int k,int& item);    void Print(); };#endif

//SLList.cpp


#include<iostream>#include"SLList.h"using namespace std;SLList::SLList(){head=tail=currptr=new SLNode();length=0;}void SLList::clear(){while(!IsEmpty()){currptr=head->next;head->next=currptr->next;delete currptr;}delete head;}bool SLList::Pre(){if(IsEmpty()||currptr==head){cout<<"no previous node!"<<endl;return false;}SLNode*temp=head;while(temp->next!=currptr)   temp=temp->next;currptr=temp;return true;}bool SLList::Next(){if(IsEmpty()||currptr==tail){cout<<"no next node!"<<endl;return false;}currptr=currptr->next;return true;}bool SLList::Insert(const int& item){currptr->next=new SLNode(item,currptr->next);if(currptr==tail) tail=currptr->next;currptr=currptr->next;length++;return true;}bool SLList::Delete(int& item){if(IsEmpty()||currptr==tail){cout<<"no next node!"<<endl;return false;}SLNode*temp=currptr->next;currptr->next=temp->next;item=temp->data;if(temp==tail)  tail=currptr;delete temp;length--;return true;}bool SLList::Search(const int& item,int (&array)[30]){if(IsEmpty()) return false;SLNode*temp=head->next;int i=1;while(temp!=NULL){if(temp->data!=item){temp=temp->next;    i++;}   else        {        array[i]=i;        currptr=temp;        temp=temp->next;    i++;        }} }bool SLList::Read(int& item){if(IsEmpty()||currptr==head) {cout<<"no data!"<<endl;return false;}item=currptr->data;return true;}bool SLList::Read(int k,int& item){if(IsEmpty()||currptr==head) {cout<<"no data!"<<endl;return false;}if(k<1||k>length){cout<<"invaild number!"<<endl;return true;}SLNode*temp=head;for(int i=0;i<k;i++) temp=temp->next;item=temp->data;currptr=temp;return true;}void SLList::Print(){SLNode*temp=head->next;cout<<"*****************************************"<<endl;for(int i=0;i<length;i++)    {        cout<<"*"<<temp->data<<"*";        temp=temp->next;    }cout<<"\n*****************************************\n\n"<<endl;}



0 0
原创粉丝点击