#include<iostream>
using namespace std;
#define MAXSIZE 100
typedef struct Node
{
int data;
struct Node *next;
}LNode;
void creatList(LNode *&L)
{
L=new LNode;
L->next=NULL;
}
void inputList(LNode *&L,int n)
{
int i=0;
for(i;i<n;i++)
{
LNode *p;
p=new LNode;
cout<<"输入第"<<i+1<<"数据.";
cin>>p->data;
p->next=L->next;
L->next=p;
}
cout<<"输入成功!"<<endl;
return;
}
int LengthList(LNode* L)
{
int i=0;
while(L!=NULL)
{
i++;
L=L->next;
}
return i-1;
}
LNode *getElem(LNode *L,int e)
{
LNode *cp;
cp=L->next;
while(cp && cp->data!=e)
{
cp=cp->next;
}
return cp;
}
void output(LNode *&L)
{
LNode *cp;
cp=L->next; //cp为L的首元节点
int i=1;
for(cp;cp!=NULL;cp=cp->next)
{
cout<<"第"<<i<<"个数据为:";
cout<<cp->data<<endl;
i++;
}
cout<<endl;
return;
}
bool insertList(LNode *&L,int e,int i)
{
LNode *cp;
cp=L; //cp为L的头节点
int j=0;
while(cp && (j<i-1))
{
cp=cp->next;
++j;
}
if(!cp || j>i-1)
return false;
LNode *s;
s=new LNode;
s->data=e;
s->next=cp->next;
cp->next=s;
return true;
}
bool cancelList(LNode *&L,int i)
{
LNode *cp;
int j=0;
cp=L;
while((cp->next)&&(j<i-1))
{
cp=cp->next;
++j;
}
if(!(cp->next)||j>i-1)
return false;
LNode *q;
q=cp->next;
cp->next=q->next;
delete q;
return true;
}
int main()
{
LNode *L;
while(1)
{
cout<<"*****************************************************"<<endl;
cout<<"******1.单链表初始化 2.单链表数据填充********"<<endl;
cout<<"******3.单链表的长度 4.单链表的查找**********"<<endl;
cout<<"******5.单链表的插入 6.单链表的删除**********"<<endl;
cout<<"******7.单链表的显示 8.退出程序 **********"<<endl;
int ch;
cout<<"请输入你的选择:";
cin>>ch;
switch(ch)
{
case 1:
creatList(L);
cout<<"链表初始化成功."<<endl;
continue;
case 2:
int n;
cout<<"填充数据的个数:";
cin>>n;
inputList(L,n);
cout<<"数据填充成功!"<<endl;
continue;
case 3:
cout<<"顺单链的长度为:"<<LengthList(L)<<endl;
continue;
case 4:
int i;
cout<<"查找数据的位置为:";
cin>>i;
cout<<"该数据为:"<<getElem(L,i)<<endl;
continue;
case 5:
int e;
int j;
cout<<"插入的数据为:";
cin>>e;
cout<<"插入的位置为:";
cin>>j;
insertList(L,e,j);
cout<<"单链表插入成功!";
continue;
case 6:
int a;
cout<<"删除的位置为:";
cin>>a;
cancelList(L,a);
cout<<"数据删除成功!";
continue;
case 7:
output(L);
continue;
case 8:
break;
default:
cout<<"请输入正确的选项!"<<endl;
system("pause");
continue;
}
break;
}
return 0;
}
本代码采用的是C++进行编译,且采用的是单链表的前插法。
若用C++进行编译且采用后插法,代码则如下:
#include<iostream>
using namespace std;
#define MAXSIZE 100
typedef struct Node
{
int data;
struct Node *next;
}LNode;
void creatList(LNode *&L)
{
L=new LNode;
L->next=NULL;
}
void inputList(LNode *&L,int n)
{
int i=0;
LNode *cp;
cp=L;
for(i;i<n;i++)
{
LNode *p;
p=new LNode;
cout<<"输入第"<<i+1<<"数据.";
cin>>p->data;
p->next=NULL;
cp->next=p;
cp=p;
}
cout<<"输入成功!"<<endl;
return;
}
int LengthList(LNode* L)
{
int i=0;
while(L!=NULL)
{
i++;
L=L->next;
}
return i-1;
}
LNode *getElem(LNode *L,int e)
{
LNode *cp;
cp=L->next;
while(cp && cp->data!=e)
{
cp=cp->next;
}
return cp;
}
void output(LNode *&L)
{
LNode *cp;
cp=L->next; //cp为L的首元节点
int i=1;
for(cp;cp!=NULL;cp=cp->next)
{
cout<<"第"<<i<<"个数据为:";
cout<<cp->data<<endl;
i++;
}
cout<<endl;
return;
}
bool insertList(LNode *&L,int e,int i)
{
LNode *cp;
cp=L; //cp为L的头节点
int j=0;
while(cp && (j<i-1))
{
cp=cp->next;
++j;
}
if(!cp || j>i-1)
return false;
LNode *s;
s=new LNode;
s->data=e;
s->next=cp->next;
cp->next=s;
return true;
}
bool cancelList(LNode *&L,int i)
{
LNode *cp;
int j=0;
cp=L;
while((cp->next)&&(j<i-1))
{
cp=cp->next;
++j;
}
if(!(cp->next)||j>i-1)
return false;
LNode *q;
q=cp->next;
cp->next=q->next;
delete q;
return true;
}
int main()
{
LNode *L;
while(1)
{
cout<<"*****************************************************"<<endl;
cout<<"******1.单链表初始化 2.单链表数据填充********"<<endl;
cout<<"******3.单链表的长度 4.单链表的查找**********"<<endl;
cout<<"******5.单链表的插入 6.单链表的删除**********"<<endl;
cout<<"******7.单链表的显示 8.退出程序 **********"<<endl;
int ch;
cout<<"请输入你的选择:";
cin>>ch;
switch(ch)
{
case 1:
creatList(L);
cout<<"链表初始化成功."<<endl;
continue;
case 2:
int n;
cout<<"填充数据的个数:";
cin>>n;
inputList(L,n);
cout<<"数据填充成功!"<<endl;
continue;
case 3:
cout<<"顺单链的长度为:"<<LengthList(L)<<endl;
continue;
case 4:
int i;
cout<<"查找数据的位置为:";
cin>>i;
cout<<"该数据为:"<<getElem(L,i)<<endl;
continue;
case 5:
int e;
int j;
cout<<"插入的数据为:";
cin>>e;
cout<<"插入的位置为:";
cin>>j;
insertList(L,e,j);
cout<<"单链表插入成功!";
continue;
case 6:
int a;
cout<<"删除的位置为:";
cin>>a;
cancelList(L,a);
cout<<"数据删除成功!";
continue;
case 7:
output(L);
continue;
case 8:
break;
default:
cout<<"请输入正确的选项!"<<endl;
system("pause");
continue;
}
break;
}
return 0;
}