线性表与数据结构

实验1  线性表

实验要求:

使用Visual C++ 2005开发环境,完成以下习题。

1.  按照课本第2.2节定义的线性表结构,完成对线性表结构的定义,以及对线性表的各种基本运算的实现(每种基本运算用一个函数来实现)。

基本运算包括:初始化InitList运算、求表长Length运算、插入新节点Insert运算、删除节点Delete运算、定位(按值查找)Locate运算、读表元Get运算。

并且在main函数中分别调用以上各种基本运算的函数来使用,以证明其功能已实现。实验效果展示可参考以下截图(也可自行设计,能展示清楚即可)。

此题的源程序保存为 2_a1.cpp。

 

 

 

程序1:

//2_a1.cpp 实验1题参考答案

#include<iostream>

using namespace std;

 

typedef int datatype;

const int maxsize = 100;

 

typedef struct 

{

datatype data[maxsize];

int n;

}sqlist; 

sqlist* InitList()

{

sqlist * L = new sqlist;

L->n=0;

return L;

int Length(sqlist * L)

{

return L->n;

}

int Insert(sqlist * L, datatype x, int i)

{

int j;

if(L->n==maxsize)

{

cout<<"表满,不能插入!"<<endl;

return -1;

}

if(i<1 || i>L->n+1)

{

cout<<"非法插入位置!"<<endl;

return 0;

}

for(j=L->n ; j>=i ; j--)

{

L->data[j] = L->data[j-1];

}

L->data[i-1]=x;

L->n++;

return 1;

}

int Delete(sqlist * L , int i)

{//从顺序表中删除第i个位置上的结点

int j;

if(L->n==0)

cout<<"表空,不能删除!(下溢)\n";

return -1;

}

if(i<1 || i>L->n)

{

cout<<"非法删除位置!\n";

return 0;

}

for(j=i+1 ; j<=L->n ; j++)

L->data[j-2] = L->data[j-1];

L->n--;

return 1;

}

int Locate(sqlist * L , datatype x)

{

int i=1;

while(i<=L->n && L->data[i-1]!=x)

i++;

if(i<=L->n)

return i;   //找到

else

return 0;  //未找到

}

datatype Get(sqlist * L, int i)

{

if(i<1 || i>L->n)

{

cout<<"非法获取位置!\n";

return 0;

}

return L->data[i-1];

}

 

void Display(sqlist * L)

{

cout<<"线性表中的数据元素依次是 : ";

int i;

for(i=0;i<L->n;i++)

{

cout<<L->data[i]<<"  ";

}

cout<<endl<<endl;

}

int main()

{

sqlist * List = InitList();

 

cout<<"调用InitList函数创建空表后:"<<endl;

cout<<"线性表的长度是:"<<Length(List)<<endl;

Display(List);

 

 

cout<<"以下四次调用Insert函数:"<<endl;

Insert(List,5,1);

cout<<"调用Insert函数,在当前第1个位置插入5之后:"<<endl;

Display(List);

 

Insert(List,6,1);

cout<<"调用Insert函数,在当前第1个位置插入6之后:"<<endl;

Display(List);

 

Insert(List,9,3);

cout<<"调用Insert函数,在当前第3个位置插入9之后:"<<endl;

Display(List);

 

Insert(List,8,3);

cout<<"调用Insert函数,在当前第3个位置插入8之后:"<<endl;

Display(List);

 

cout<<"调用Length函数,得到线性表的当前长度是:"<<Length(List)<<endl<<endl;

 

int r;

r=Locate(List,5);

cout<<"调用Locate函数,找到5在线性表中的位置是:"<<r<<endl<<endl;

 

 

Delete(List,2);

cout<<"调用Delete函数,删除第2个元素后:"<<endl;

Display(List);

 

cout<<"调用Length函数,得到线性表的当前长度是:"<<Length(List)<<endl<<endl;

 

datatype d;

d=Get(List ,3);

cout<<"调用Get函数获取得到该线性表的第3个元素是:"<<d<<endl<<endl;

 

return 0;

}

程序2

#include <iostream>

using namespace std;

const listsize=100;

typedef int DataType;

typedef struct

{

DataType data[listsize];

int length;

}SeqList;

 

SeqList L;

 

 

//函数声明区域

void createlist (SeqList *p);          //创建顺序表

void printlist (SeqList L);                 //输出顺序表

void sortlist (SeqList *p);                  //排列顺序表

void Delete (SeqList *p,int wz);                   //删除顺序表中的元素

void search (SeqList *p,int ys);                 //查找顺序表中元素的信息

void insert (SeqList *p,DataType ys,int wz);                //插入元素

void esc ();                                    //退出

 

 

 

//函数定义区域

 

void createlist (SeqList *p)//建立顺序表

{

int i,n;

cout <<"请输入要创建顺序表的元素个数:";

cin >>n;

cout <<"请依次输入"<<n<<"个元素:";

for (i=1;i<=n;i++)

{

cin >>p->data[i];

}

p->length=n;

}

 

 

void printlist (SeqList L)//输出

{

int i;

cout<<"顺序表的元素为:";

for (i=1;i<=L.length;i++)

cout <<L.data[i]<<" ";

cout <<endl;

cout<<"当前顺序表长度为:"<<L.length<<endl;

}

 

 

void sortlist (SeqList *p)//排序函数

{

int i,j,t;

for(i=0;i<p->length-1;i++)

{

for(j=0;j<p->length;j++)

if((p->data[j])>(p->data[j+1]))

{

t=p->data[j];

p->data[j]=p->data[j+1];

p->data[j+1]=t;

}

}

 

}

    

void insert (SeqList *p,DataType ys,int wz)//插入元素函数

{

cout<<"请输入要插入的元素:";

cin>>ys;

    cout<<"请输入要插入的位置:";

cin>>wz;

if(wz<1)

wz=1;

else if(wz>p->length)

wz=p->length+1;

 

for(int i=p->length;i>=wz;i--)

p->data[i+1]=p->data[i];

p->data[wz]=ys;

p->length=(p->length)+1;

}

 

 

void Delete (SeqList *p,int wz)//删除函数

{

cout<<"请输入要删除元素的位置:";

cin>>wz;

int i;

if (wz<1 || wz>p->length)

cout<<"删除位置不对!"<<endl;

else

for(i=wz;i<=p->length;i++)

p->data[i]=p->data[i+1];

p->length--;

}

 

 

void search (SeqList *p,int ys)//查询函数

{

cout<<"请输入要查找的元素:";

cin>>ys;

for(int i=0;i<=p->length;i++)

if(p->data[i]==ys)

cout<<"元素值为:"<<ys<<endl<<"所在位置为:"<<i<<endl;

}

 

void esc()//退出函数

{exit (1);}

 

 

int main()

{

T:cout<<"***************顺序表****************"<<endl;

  cout<<'\t'<<"1、创建顺序表"<<endl;

  cout<<'\t'<<"2、从小到大排列顺序表"<<endl;

  cout<<'\t'<<"3、插入元素"<<endl;

  cout<<'\t'<<"4、删除元素"<<endl;

  cout<<'\t'<<"5、查找元素"<<endl;

  cout<<'\t'<<"6、退出"<<endl;

  cout<<"**************************************"<<endl;

cout<<"请输入数字(1~7)选择相应的功能"<<endl;

int wz;

DataType ys;

char aa;

cin >>aa;

switch(aa)

{

case'1':createlist(&L); printlist (L);break;

    case'2':sortlist(&L); printlist (L);break;

case'3': insert(&L,ys,wz); printlist (L);break;

case'4':Delete (&L,wz);printlist(L);break;

case'5':search(&L,ys);break;

case'6':esc();

}

goto T;

return 0;

}

2. 一个顺序表中存放字符(只有数字字符和英文字符),编写算法删除所有的数字字符。此题的源程序保存为 2_a2.cpp。

程序1

#include<iostream>

using namespace std;

 

typedef char datatype;

const int maxsize = 100;

 

typedef struct 

{

datatype data[maxsize];

int n;

}sqlist; 

 

sqlist* InitList()

{

sqlist *L=new sqlist;

L->n=0;

return L;

 

int Length(sqlist * L)

{

return L->n;

}

 

int Insert(sqlist * L, datatype x, int i)

{

int j;

if(L->n==maxsize)

{

cout<<"表满,不能插入!"<<endl;

return -1;

}

if(i<1 || i>L->n+1)

{

cout<<"非法插入位置!"<<endl;

return 0;

}

for(j=L->n ; j>=i ; j--)

{

L->data[j] = L->data[j-1];

}

L->data[i-1]=x;

L->n++;

return 1;

}

int Delete(sqlist * L , int i)

{//从顺序表中删除第i个位置上的结点

int j;

if(L->n==0)

cout<<"表空,不能删除!(下溢)\n";

return -1;

}

if(i<1 || i>L->n)

{

cout<<"非法删除位置!\n";

return 0;

}

for(j=i+1 ; j<=L->n ; j++)

L->data[j-2] = L->data[j-1];

L->n--;

return 1;

}

 

void Display(sqlist * L)

{

cout<<"线性表中的数据元素依次是 : ";

int i;

for(i=0;i<L->n;i++)

{

cout<<L->data[i]<<"  ";

}

cout<<endl<<endl;

}

 

int IsDigit(char c)

{

if(c>='0' && c<='9')

return 1;

else

return 0;

}

void DeleteDigit(sqlist * L)//删除顺序表中的数字字符方法1

{

int i;

for(i=0 ; i<L->n ; )

{

if( IsDigit(L->data[i]) )

Delete(L,i+1);

else

i++;

}

}

 

void DeleteDigit2(sqlist * L)//删除顺序表中的数字字符方法2

{

int i,s;

s=0;

for(i=0;i<L->n;i++)

{

if( IsDigit(L->data[i]))

s++;

else if(s>0)

L->data[i-s] = L->data[i];

}

L->n = L->n - s;

}

int main()

{

cout<<"以下先验证 删除顺序表中的数字字符方法1"<<endl;

sqlist * List = InitList();

 

Insert(List,'a',1);

Insert(List,'2',2);

Insert(List,'3',3);

Insert(List,'c',4);

Insert(List,'s',5);

Insert(List,'8',6);

Insert(List,'6',7);

Display(List);

 

DeleteDigit(List);

cout<<"删除数字字符后。"<<endl;

Display(List);

 

 

cout<<"以下验证 删除顺序表中的数字字符方法2"<<endl;

sqlist * List2 = InitList();

 

Insert(List2,'a',1);

Insert(List2,'2',2);

Insert(List2,'3',3);

Insert(List2,'c',4);

Insert(List2,'s',5);

Insert(List2,'8',6);

Insert(List2,'6',7);

Display(List2);

 

DeleteDigit(List2);

cout<<"删除数字字符后。"<<endl;

Display(List2);

return 0;

}

程序2

/*2. 一个顺序表中存放字符(只有数字字符和英文字符),编写算法删除所有的数字字符。

此题的源程序保存为 2_a2.cpp*/

#include <iostream>

using namespace std;

typedef char datatype;

const char maxsize=100;

typedef struct{

datatype data[maxsize];

int n;

}sqlist;

int intsert(sqlist *L)

{int j,i;

cout <<"请输入字符顺序表个数:";

cin >>i;

for(j=0;j<i;j++)

{cout<<"请输入第"<<j+1<<"个数字或字母";

cin>>L->data[j];

}

L->n=i;

return L->n;

}

int Delete(sqlist *L)

{int j,a;

for(j=0;j<L->n;j++)

{a=L->data[j];

if(a<48||a>57){cout<<L->data[j]<<" ";}

else if(a>=48||a<=57)

{cout<<"";

}

}

return 1;

}

int Display(sqlist *L)

{int i;

if(L->n==0)

{cout<<"表空,不能删除!(下溢)\n";return -1;}

for(i=0;i<L->n;i++)

cout<<L->data[i]<<" ";

return 1;

}

void Initlist(sqlist *L)

{L->n=0;}

int main()

{sqlist *list=new sqlist;

Initlist(list);

intsert(list);

cout<<"输入的字符顺序表是";

Display(list);

cout<<endl;

cout<<"删除字符顺序表所有的数字字符";

cout<<endl;

cout<<"删除后字符顺序表是";

Delete(list);

cout<<endl;

return 0;

}

3. 按照课本第2.3.1节定义的单链表结构,完成对单链表结构的定义,以及对单链表的各种基本运算的实现(每种基本运算用一个函数来实现)。

基本运算包括:建表Create运算、初始化InitList运算、求表长Length运算、插入新节点Insert运算、删除节点Delete运算、按序号查找Get运算、定位(按值查找)Locate运算、输出单链表中所有结点的数据元素值Display运算、销毁Destroy运算。

并且在main函数中分别调用以上各种基本运算的函数来使用,以证明其功能已实现。此题的源程序保存为 2_a3.cpp。

程序1:

#include<iostream>

using namespace std;

typedef char datatype;

typedef struct node* pointer;

struct node

{

datatype data;

pointer next;

};

typedef node* lklist;

 

lklist Creat() 

{//尾插法建表,有头结点

pointer head,rear,s; char ch;

head=new node; //生成头结点

rear=head; //尾指针初值

cout<<"请依次输入链表中的元素,每个元素是一个字符,以输入$表示结束:"<<endl;

while(cin>>ch,ch!='$')

{  //读入并检测结束

s=new node;  s->data=ch; //生成新结点

rear->next=s; rear=s;    //插入表尾,改尾指针

}

rear->next=NULL; //尾结点的后继为空

return head;

}

 

lklist InitList() 

{

pointer head;

head=new node;

head->next=NULL;

return head;

int Length(lklist L)

 {

int j; 

pointer p;

j=0; 

p=L->next; //从首结点开始

while(p!=NULL) //逐点检测、计数

{

j++; 

p=p->next;

}

return j;

}

pointer Get(lklist head,int i) //0in

{

int j;

pointer p;

if(i<0) return NULL;//位置非法,无此结点

j=-1; //计数器

p=head; //从头结点(0号)开始搜索

while(p!=NULL) {

j++;if(j==i) break;

p=p->next; //未到第i点,继续

}

return p; //含找到、未找到两种情况

}

 

int Insert(lklist head,datatype x,int i)

{

pointer q,s;

q=Get(head,i-1); //找第i-1个点

if(q==NULL)   //无第i-1点,即i<1i>n+1

    {

cout<<"非法插入位置!\n";

return 0;

}

s=new node; //生成新结点

s->data=x;

s->next=q->next;  //新点的后继是原第i个点

q->next=s; //原第i?1个点的后继是新点

return 1; //插入成功

}

 

 

int Delete(lklist head,int i) 

{

pointer p,q;

q=Get(head,i-1); //找待删点的直接前趋

if(q==NULL || q->next==NULL)//i<1i>n              

    {cout<<"非法删除位置!\n";return 0;}

p=q->next; //保存待删点地址

q->next=p->next; //修改前趋的后继指针

delete p; //释放结点

return 1; //删除成功

}

 

 

 

pointer Locate(lklist head,datatype x) 

{

pointer p;

p=head->next; //从首结点开始搜索

while(p!=NULL) 

{

if(p->data==x) break;

p=p->next; //到下一个点

}

return p;  //含找到、未找到两种情况

}

 

int Locate2(lklist head,datatype x) {

int j;

pointer p;

j=0; //计数器

p=head->next; //从首结点开始扫描

while(p!=NULL) {

j++;

if(p->data==x) break;//找到,退出

p=p->next;    //没找到,继续

}

if(p!=NULL) return j; //找到x

else return -1; //没有x,查找失败

}

 

void Destory(lklist & L)

{//删除所有结点

pointer p,q;

p=L;

while(p!=NULL) 

{

q=p->next;       //保存待删点后继

delete p;        //释放空间

p=q;

}

L=NULL;           //头指针置空

}

 

 

void Display(lklist head)

{

if(head==NULL)

{

cout<<"链表不存在!"<<endl;

return ;

}

pointer p;

p=head->next;

cout<<"链表中的元素依次是:";

while(p!=NULL)

{

cout<<p->data<<' ';

p=p->next;

}

cout<<endl;

}

int main()

{

lklist Lk;

cout<<"调用Creat函数,创建链表:"<<endl;

Lk=Creat();

Display(Lk);

 

 

cout<<"调用Length函数,链表的长度是:"<<Length(Lk)<<endl;

 

Insert(Lk, 'x',3);

cout<<"调用Insert函数,在第3个位置插入字符x之后。";

Display(Lk);

 

cout<<"调用Get函数,获取链表的第4个元素得到是:";

pointer p = Get(Lk,4);

cout<<p->data<<endl;

 

cout<<"调用Locate函数,查找链表中的字符d,得到在链表的第"<<Locate2(Lk,'d')<<"的位置。"<<endl;

 

Delete(Lk, 2);

cout<<"调用Delete函数,删除第2个位置的字符之后。";

Display(Lk);

 

Destory(Lk);

cout<<"调用Destroy函数,销毁链表后。";

Display(Lk);

 

return 0;

}

程序2

/*3. 按照课本第2.3.1节定义的单链表结构,完成对单链表结构的定义,以及对单链表的各种基本运算的实现(每种基本运算用一

个函数来实现)。基本运算包括:建表Create运算、初始化InitList运算、求表长Length运算、插入新节点Insert运算、删除节

Delete运算、按序号查找Get运算、定位(按值查找)Locate运算、输出单链表中所有结点的数据元素值Display运算、

销毁Destroy运算。并且在main函数中分别调用以上各种基本运算的函数来使用,以证明其功能已实现。此题的源程序保存为 2_a3.cpp*/

程序1

#include<iostream>

using namespace std;

typedef char datatype;

typedef struct node* pointer;

struct node

{

datatype data;

pointer next;

};

typedef node* lklist;

 

lklist Creat() 

{//尾插法建表,有头结点

pointer head,rear,s; char ch;

head=new node; //生成头结点

rear=head; //尾指针初值

cout<<"请依次输入链表中的元素,每个元素是一个字符,以输入$表示结束:"<<endl;

while(cin>>ch,ch!='$')

{  //读入并检测结束

s=new node;  s->data=ch; //生成新结点

rear->next=s; rear=s;    //插入表尾,改尾指针

}

rear->next=NULL; //尾结点的后继为空

return head;

}

 

lklist InitList() 

{

pointer head;

head=new node;

head->next=NULL;

return head;

int Length(lklist L)

 {

int j; 

pointer p;

j=0; 

p=L->next; //从首结点开始

while(p!=NULL) //逐点检测、计数

{

j++; 

p=p->next;

}

return j;

}

pointer Get(lklist head,int i) //0in

{

int j;

pointer p;

if(i<0) return NULL;//位置非法,无此结点

j=-1; //计数器

p=head; //从头结点(0号)开始搜索

while(p!=NULL) {

j++;if(j==i) break;

p=p->next; //未到第i点,继续

}

return p; //含找到、未找到两种情况

}

 

int Insert(lklist head,datatype x,int i)

{

pointer q,s;

q=Get(head,i-1); //找第i-1个点

if(q==NULL)   //无第i-1点,即i<1i>n+1

    {

cout<<"非法插入位置!\n";

return 0;

}

s=new node; //生成新结点

s->data=x;

s->next=q->next;  //新点的后继是原第i个点

q->next=s; //原第i?1个点的后继是新点

return 1; //插入成功

}

 

 

int Delete(lklist head,int i) 

{

pointer p,q;

q=Get(head,i-1); //找待删点的直接前趋

if(q==NULL || q->next==NULL)//i<1i>n              

    {cout<<"非法删除位置!\n";return 0;}

p=q->next; //保存待删点地址

q->next=p->next; //修改前趋的后继指针

delete p; //释放结点

return 1; //删除成功

}

 

 

 

pointer Locate(lklist head,datatype x) 

{

pointer p;

p=head->next; //从首结点开始搜索

while(p!=NULL) 

{

if(p->data==x) break;

p=p->next; //到下一个点

}

return p;  //含找到、未找到两种情况

}

 

int Locate2(lklist head,datatype x) {

int j;

pointer p;

j=0; //计数器

p=head->next; //从首结点开始扫描

while(p!=NULL) {

j++;

if(p->data==x) break;//找到,退出

p=p->next;    //没找到,继续

}

if(p!=NULL) return j; //找到x

else return -1; //没有x,查找失败

}

 

void Destory(lklist & L)

{//删除所有结点

pointer p,q;

p=L;

while(p!=NULL) 

{

q=p->next;       //保存待删点后继

delete p;        //释放空间

p=q;

}

L=NULL;           //头指针置空

}

 

 

void Display(lklist head)

{

if(head==NULL)

{

cout<<"链表不存在!"<<endl;

return ;

}

pointer p;

p=head->next;

cout<<"链表中的元素依次是:";

while(p!=NULL)

{

cout<<p->data<<' ';

p=p->next;

}

cout<<endl;

}

int main()

{

lklist Lk;

cout<<"调用Creat函数,创建链表:"<<endl;

Lk=Creat();

Display(Lk);

 

 

cout<<"调用Length函数,链表的长度是:"<<Length(Lk)<<endl;

 

Insert(Lk, 'x',3);

cout<<"调用Insert函数,在第3个位置插入字符x之后。";

Display(Lk);

 

cout<<"调用Get函数,获取链表的第4个元素得到是:";

pointer p = Get(Lk,4);

cout<<p->data<<endl;

 

cout<<"调用Locate函数,查找链表中的字符d,得到在链表的第"<<Locate2(Lk,'d')<<"的位置。"<<endl;

 

Delete(Lk, 2);

cout<<"调用Delete函数,删除第2个位置的字符之后。";

Display(Lk);

 

Destory(Lk);

cout<<"调用Destroy函数,销毁链表后。";

Display(Lk);

 

return 0;

}

程序2

#include <iostream>

using namespace std;

typedef char datatype;

class list;

class listnode

{

friend class list;

datatype data;

listnode *link;

public:

listnode(){link=NULL;}

    listnode(char&item,listnode *next=NULL)

{data=item;

link=next;

}

};

 

class list

{public:

list(){listnode *q=new listnode;first=last=q;}

void Create(list L);

void Insert( datatype num);

listnode *Get( int i);

void Locate(datatype value);

int Insertvalue(datatype  value,int i);

int Delete(int i);

int Deletevalue(int value);

void Display();

void Length();

void Destroy();

int as(int i);

private:

listnode *first,*last;

int length;

};

 

void list::Create(list L)

{int i,l;datatype x;

cout<<"请输入链表的长度: ";

cin>>l;

cout<<"请输入字符: ";

for(i=1;i<l+1;i++)

{cin>>x;

L.Insert(x);

}

L.length++;

}

 

 

void list::Insert(datatype num)

{listnode *newnode=new listnode;

newnode->data=num;

newnode->link=NULL;

if(first==NULL)

{first=newnode;

last=newnode;

}

else last->link=newnode;

last=newnode;

}

 

 

listnode *list::Get(int i)

{if(i<-1)return NULL;

if(i==-1)return first;

listnode *p=first->link;

int j=0;

while(p!=NULL&&j<i)

{p=p->link;

j++;

}

return p;

}

 

 

 

 

int list::Insertvalue(datatype value,int i)

{listnode *p=Get(i-1);

if(p==NULL)return 0;

listnode *newnode=new listnode(value,p->link);

if(p->link==NULL)

last=newnode;

p->link=newnode;

length++;

return 1;

}

 

 

int list::as(int i)

{listnode *p=Get(i);

cout<<p->data<<endl;

return 1;

}

 

 

int list::Delete(int i)

{int value;

listnode *p=Get(i-1),*q;

if(p==NULL||p->link==NULL)

return NULL;

q=p->link;

p->link=q->link;

value=q->data;

if(q==last)last=p;

delete q;

length--;

return value;

}

 

 

int list::Deletevalue(int value)

{listnode *p=first,*q;

while(p->link!=NULL&&p->link->data!=value)

p=p->link;

if(p->link==NULL&&p->data!=value)

return NULL;

q=p->link;

p->link=q->link;

if(q==last) last=p;

delete q;

length--;

return value;

}

 

 

void list::Length()

{length=0;

listnode*p=first;

while(p->link!=NULL)

{p=p->link;

length++;

}

cout<<"the length is: "<<length<<endl;

}

 

 

void list::Display()

{listnode *p;

p=first->link;

while(p!=NULL)

{cout<<p->data<<" ";

p=p->link;}

cout<<endl;

}

void list::Locate(datatype value)

{listnode *p;

p=first->link;

int j=0;

while(p!=NULL)

{j++;

if(p->data==value)break;

p=p->link;

}

cout<<j<<endl;

}

 

 

 

 

 

void list::Destroy() 

{

listnode *p,*q;

p=first -> link;

while(p!=NULL) 

{q=p;

p = p -> link;

delete q;

}

}

 

 

 

void main()

{int i,as=1,sa;listnode *p;

datatype value;

list a;

cout<<"  1.创建单链表\n";

cout<<"  2.求单链表长度\n";

cout<<"  3.单链表插入新节点\n";

cout<<"  4.单链表删除节点\n";

cout<<"  5.输出单链表中所有结点的数据元素值\n";

cout<<"  6.销毁单链表\n";

cout<<"  7.按序号查找数据元素值\n";

cout<<"  8.按数据元素值查找序号\n";

cout<<"  9.退出\n";

while(as)

{cout<<"请选择: ";

cin>>sa;

switch(sa){

case 1:a.Create(a);cout<<"创建单链表是";a.Display();cout<<endl;break;

case 2:a.Length();break;

case 3:cout<<"请输入新节点数据元素值、位置: ";cin>>value>>i; a.Insertvalue(value,i);cout<<"新节点输入后单链表是";a.Display();cout<<endl;break;

case 4:cout<<"请输入删除第几个节点: "; cin>>i;a.Delete(i);break;

case 5:a.Display();break;

case 6:a.Destroy();break;

case 7:cout<<"请输入查找序号";cin>>i;a.as(i);break;

case 8:cout<<"请输入查找数据元素值";cin>>value;a.Locate(value);break;

case 9:as=0;break;

           }

     }

}

 

转载于:https://www.cnblogs.com/laiguixin/archive/2011/11/21/2257641.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值