您的当前位置:首页正文

链表的插入删除查找

2024-11-13 来源:个人技术集锦

1.创建一个单链表,按顺序输入5个整数并打印输出。
2.插入一个元素,通过键盘输入插入位置和数据,打印输出。
3.删除一个元素,从键盘读取删除元素位置,打印输出
4.查找一个数据,找到了,删除它,找不到在链表末尾加上他

#include <iostream>
#include <string>

using namespace std;

int search_data;
struct listPoint 
{
	listPoint *next;
	listPoint *last;
	int data;

};


listPoint * Creat_Normal_list(int n)          //创建链表
{
	listPoint *head,*end,*normal;
	head = (listPoint*)malloc(sizeof(listPoint));
	end = head;
	for (int i = 0; i < n; i++)
	{
		normal = (listPoint*)malloc(sizeof(listPoint));
		cout<<"请输入数据:  ";
		cin>>normal->data;
		end->next = normal;
		normal->last = end;
		end = normal;
	}
	end->next = NULL;
	head->last = NULL;

	return head;

}

void add_one_Point(listPoint *list_0)  //添加数据
{
	int data ,n;
	cout<<"请输入插入位置";
	cin >> n;
	cout<<"请输入插入数据";
	cin >>data;
	listPoint *list=list_0;
	listPoint *normal;
	normal = (listPoint*)malloc(sizeof(listPoint));

		for (int i = 0; i < n; i++)
	{
		if(list->next!=NULL) list  = list->next ;
	}
		normal->data = data;
		normal->next = list->next;
		list -> next ->last = normal;
		list -> next = normal;
		normal-> last = list;
}

void add_oneLast_Point(listPoint *list_0,int n) //链表末尾添加数据
{
	listPoint *list=list_0;
	listPoint *normal;
	normal = (listPoint*)malloc(sizeof(listPoint));

		for (int i = 0; i < 5; i++)
	{
		if(list->next!=NULL) list  = list->next ;
	}
		normal->data = search_data;
		normal->last = list;
		normal->next = NULL;
		list -> next = normal;
}

void delete_one_Point(listPoint *list_0)  //删除运行中输入位置数据
{
	int n;
	cout<<"请输入删除位置";
	cin >> n;
	listPoint *list = list_0;
	for (int i = 0; i < n; i++){ 
   
	    if(list->next!=NULL) list  = list->next ;
	}

	list->next->last = list->last;
	list->last->next = list->next;
	free(list);

}
void delete_one_Point(listPoint *list_0,int n) //删除第n位置的数据
{
	listPoint *list = list_0;
	for (int i = 0; i < n; i++){ 
   
	    if(list->next!=NULL) list  = list->next ;
	}

	list->next->last = list->last;
	list->last->next = list->next;
	free(list);

}

listPoint* listn( listPoint* l1,int n)  //用于查找固定下表n数据,未使用,以后扩展功能用
{
	listPoint*l2=l1;
	for (int i = 1; i <= n; i++)
	{
		if (l2->next!=NULL)
		{
			l2 = l2->next;
			
		}
	}
	return l2;
}

void printInfo(listPoint* list1)   //用于打印输出数据
{
	listPoint *list = list1;
	cout<<"链表数据为:"<<endl;
	for (int i = 1; i <= 6; i++)
	{
		if (list->next!=NULL){
			list = list->next;
		cout<<list->data<<endl;
		}
		
	}
}

int search_Point(listPoint* list1)   //搜寻数据 如果未找到返回0;如果找到了返回所在位置
{
	listPoint* list = list1;
	cout<<"请输入查找的数据";
	cin >> search_data;
	for (int i = 1; i <= 5; i++){
		list = list->next;
		if(list->data == search_data)return i;
	}
	return 0;
}

int main()
{
	listPoint *list;
	list = Creat_Normal_list(5);
	printInfo(list);
	add_one_Point(list);
	printInfo(list);
	delete_one_Point(list);
	printInfo(list);
	int Pos = search_Point(list);
	if (Pos == 0){
		add_oneLast_Point(list,5);
		printInfo(list);
	}
	else{
	
	    delete_one_Point(list,Pos);
	     printInfo(list);

	}
	
	system("pause");
	return 0 ;
}
Top