c++链表插入一个数据和删除指定位置数据、查询指定数据位置

本文详细介绍了使用C++实现顺序表的基本操作,包括初始化、插入、删除和查找等关键功能。通过具体代码示例,展示了如何在顺序表中进行元素的插入、删除以及查找过程,适合初学者理解和掌握数据结构的基础知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


```cpp
#include <iostream.h>
#include<string.h>
#include<iomanip.h>
#define MAXLEN 100
struct SeqList{
int data[MAXLEN];
int last;
};
/*初始化*/
void InitSeqList(SeqList *list)
{
	
    list->last=-1;
   
}

//在第i个位置上插入数据key
int InsertSeqList(SeqList *list,int i,int key)
{	
	int k;
	if(list->last==MAXLEN-1){/*表空间已满,不能插入*/
		cout<<" 表满 "<<endl;return 0;
	}
	if( i<0 || i>list->last+1 ){/*检查插入位置的合法性*/
		cout<<" 位置不合法 "<<endl;return 0;
	}
	/*将位置i~last上的元素向后移动*/
	for(k=list->last;k>=i;k--)
		list->data[k+1]=list->data[k];
	list->data[i]=key;  /*新元素插入*/
	list->last++;  /*last仍指向最后元素*/
	return 1;
	
	
}

//删除指定位置i上的数据
int DeleteSeqList(SeqList *list,int i)
{
	int k,a;
	if(list->last==-1){/*表空,不能删除*/
		cout<<" 表空 "<<endl;return 0;
	}
	if(i<0||i>list->last){/*检查删除位置的合法性*/
		cout<<" 位置不合法 "<<endl;return 0;
	}
	/*将位置i+1~ last上的元素向前移动*/
	a=list->last;
	for (k = i+1; list->last >= k ; k++ )
		list->data[k-1] = list->data[k];
	list->last=a-1; /*last仍指向最后元素*/	
	return 1; 
}

/*查找循环*/
int LocateSeqList(SeqList *list, int key)
{ 
	cout<<"查找的数据的位置:"<<endl;
	int i;
	for(i = 0; i<=list->last; i++)
		if(list->data[i]==key)
			cout<<i<<endl;			
	return -1;
}

/*顺序输出表*/
void Display(SeqList *list)
{
	for(int i=0;i<=list->last;i++)
		cout<<list->data[i]<<"   ";
     cout<<endl;
}
void main()
{	int a,b,c,d;
    SeqList list;
    InitSeqList(&list);//空表
	list.data[0]=12;
	list.data[1]=34;
	list.data[2]=17;
	list.data[3]=23;
	list.data[4]=31;
	list.data[5]=10;
	list.last=5;
	cout<<"输入前的数据:"<<endl;
	Display(&list);
	cout<<"请输入要插入的位置和数据:"<<endl;
	cin>>a>>b;
	InsertSeqList(&list,a,b);
	cout<<"输入后的数据:"<<endl;
	Display(&list);
	cout<<"请输入要删除的位置:"<<endl;
	cin>>c;
	DeleteSeqList(&list,c);
	cout<<"删除后的数据:"<<endl;
	Display(&list);
	cout<<"查找的数据:"<<endl;
	cin>>d;
	LocateSeqList(&list,d);
  }

结果
在这里插入图片描述

在创建链表并支持在特定位置插入删除元素以及查找元素位置操作时,你需要编写一些基本的数据结构(如单链表节点`ListNode`),同时维护链表相关的函数(例如`insert_at`, `delete_at`, `find_position`). 这里提供了一个简单的C++示例: **链表节点定义 (Node.h)** ```cpp #ifndef NODE_H #define NODE_H #include <iostream> struct ListNode { int data; ListNode* next; ListNode(int val = 0) : data(val), next(nullptr) {} }; #endif // NODE_H ``` **链表操作库 (ListOperations.cpp)** ```cpp #include "Node.h" // 插入节点到链表的特定位置 void insert_at(ListNode*& head, int index, int value) { if (index <= 0) { ListNode* newNode = new ListNode(value); newNode->next = head; head = newNode; return; } ListNode* current = head; for (int i = 0; i < index - 1 && current != nullptr; ++i) { current = current->next; } if (current == nullptr) { std::cout << "Invalid index\n"; return; } ListNode* newNode = new ListNode(value); newNode->next = current->next; current->next = newNode; } // 删除链表指定位置的节点 void delete_at(ListNode*& head, int index) { if (index <= 0) { ListNode* temp = head; head = head->next; delete temp; return; } ListNode* current = head; for (int i = 0; i < index - 1 && current != nullptr; ++i) { current = current->next; } if (current == nullptr || current->next == nullptr) { std::cout << "Invalid index or node not found\n"; return; } ListNode* toDelete = current->next; current->next = toDelete->next; delete toDelete; } // 查找给定值在链表中的位置(从0开始计数) int find_position(const ListNode* head, int target) { ListNode* current = head; int position = 0; while (current != nullptr && current->data != target) { current = current->next; position++; } return current ? position : -1; // 返回目标值存在的位置,若未找到返回-1 } ``` **主程序 (main.cpp)** ```cpp #include "Node.h" #include "ListOperations.cpp" int main() { // 创建链表插入节点,删除节点,查找位置操作... ListNode* head = nullptr; // ... 实现具体的链表操作 ... return 0; } ``` 记得将上述代码保存到单独的`.cpp`文件(比如`ListOperations.cpp`)对应的`.h`头文件中,然后在`main.cpp`或其他需要链表功能的模块中包含它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值