数据结构单链表的创建插入及相关操作

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜雨时’

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值