链表的创建,显示,删除,插入,寻找

#include <iostream>
using namespace std;
struct note
{
	int a;
	note *next;
};
//全局变量 
note *head = new note;
note *tail = new note;
int n;

void creatlist()
{
	cout << "请输入所建链表的数量:";
	cin >> n;
	head = NULL;
	tail = NULL;
	
	int num;
	note *temp =new note;
	if (temp == NULL)
	{
		cout << " 申请失败";
	}
	cout <<  "input num = "; 
	cin >> num;
	temp->a = num;
	temp->next = NULL;
	if (head == NULL)
	{
		head = temp;
		tail = temp;
	} 
	for (int i = 0; i < n-1; i ++)
	{
	
		temp = new note;
		if (temp == NULL)
		{
			cout << " 申请失败";
		}
		cout <<  "input num = "; 
		cin >> num;
		temp->a = num;
		temp->next = NULL;
		tail->next = temp;
		tail = temp;
	}
}

void outputList()
{
	note *temp = head;
	while (temp!=NULL)
	{
		cout << temp->a<< "\t";
		temp = temp->next;
	}
	cout << endl;
}

void deleteLIst()
{
	cout << "请输入你要删除的数字:";
	cin >> n;
	note *temp = head;
	note *fore = NULL;
	
	while(temp!=NULL && temp->a != n)
	{
		fore = temp;
		temp = temp->next ;
	}
	
	if (fore == NULL)
	{
		head = temp->next ;
		delete temp;
	}
	
	else if (temp == NULL)
	{
		delete temp;
	}
	
	else if (temp->a==n)
	{
		fore->next = temp->next;
		delete temp; 
	}
	
	else 
	{
		cout << "没有找到;";
	}
	outputList();
	cout << "已经删除;"<< endl;
}

void insert( )
{
	cout << "请输入你要插入的数字:";
	cin >> n;
	note *temp = head;
	note *fore = NULL;
	note *cur = new note;
	
	while(temp!=NULL && temp->a < n )
	{
		fore = temp;
		temp = temp->next ;
	}
	
	if (fore == NULL)
	{
		cur->a = n;
		cur->next = temp;
		head = cur;
	}
	
	else if (temp == NULL)
	{
		temp->next = cur;
		cur->a = n;
		cur->next = NULL; 
	}
	
	else
	{
		cur->a = n;
		fore->next = cur;
		cur->next = temp;
	}
	cout << "插入成功:"; 
	outputList();
}

void findLIst()
{
	cout << "请输入你要查找的数字:";
	cin >> n;
	note *temp = head;
	note *fore = NULL;
	
	while(temp!=NULL && temp->a != n)
	{
		fore = temp;
		temp = temp->next ;
	}
	
	if (temp->a == n)
	{
		cout << n << "在链表中"<< endl;
	}
	else 
	{
		cout << n << "不在链表中"<< endl;
	}
}

void miue();
void select();

int main()
{
	miue();
}

void select()
{
	int selet ;
	while(1)
	{
		cout << "请输入你要进行的选择:";
		cin >> selet;
		switch(selet)
		{
			case 1:creatlist();break;
			case 2:outputList();break;
			case 3:deleteLIst();break;
			case 4:insert( );break; 
			case 5:findLIst();break; 
		}
		miue();
	}

} 

void miue()
{
	cout << "***********链表************" << endl;
	cout << "1--------------创建链表" << endl;
	cout << "2--------------显示链表" << endl;
	cout << "3--------------删除数字" << endl;
	cout << "4--------------插入数字" << endl;
	cout << "5--------------寻找数字" << endl;
	cout << "***************************" << endl;
	select();
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值