单链表的建立(链式存储)

数据结构编程练习(二)

功能1:在构造函数完成带头结点单链表的初始化。

功能2:输入数据,利用尾插法完成链表生成。

功能3:求单链表表长。

功能4:求链表中第i个元素,要求返回第i个元素是否存在的结果,并返回第i个元素值及其结点地址。

功能5:在第i个结点前插入值为x的结点。

功能6:删除链表中第i个元素结点,需返回第i个结点是否存在的状态,并返回删除结点中存储的数据。

功能7:在析构函数中完成链表内存释放,声明一个对象,截图描述并说明其构造函数、析构函数调用顺序及过程。


代码实现:

#include "iostream" 
using namespace std;
struct node
{
	int data;
	node *next;
};
class list
{
public:
	list();
	~list();
	int create_L();
	int length();
	int get_element(int i);
	int insert(int i,int &x); 
	int del_element(int i);
	int print();
private:
	int count;
	node *head;
};
//单链表的初始化 
list::list()
{
	head=new node;
	head->next=NULL;
	count=0;
}
//析构函数释放内存 
list::~list()
{
	node *n=head;
	while(count--)
	{
		node *p=n->next;
		delete n;
		n=p;
	}
}
//尾插法建立单链表 
int list::create_L()
{
	int m;
	cout<<"请输入所要建立的单链表的长度:";
	cin>>m;
	cout<<"请输入各元素:";
	node *n=head;
	while(m--)
	{
	    node *p=new node;
	    n->next=p;
	    p->next=NULL;
	    cin>>p->data;
	    n=p;
	    count++;
	}
	return 0;
} 
//求单链表的长度
int list::length()
{
	return count;
} 
//取第i个位置的元素
int list::get_element(int i)
{
	if(i<=0||i>count)
	{
		cout<<"不在范围"<<endl;
		return 0; 
	}
	else
	{
		node *n=head;
	    while(i--)
		    n=n->next;
	    cout<<"该位置的元素为:"<<n->data<<endl; 
	    return 1;
	}
} 
//在第i个结点前插入值为x的结点
int list::insert(int i,int &x)
{
	if(i<=0||i>count)
	{
		cout<<"不在范围"<<endl;
		return 0;
	}
	else
	{
		node *n=head;
		i--;
		while(i--)
			n=n->next;
		node *p=new node;
		p->data=x;
		p->next=n->next;
		n->next=p;
		count++;
		return 1;
	}
}
//删除第i个位置元素
int list::del_element(int i)
{
	if(i<=0||i>count)
	{
		cout<<"不在范围"<<endl;
		return 0; 
	}
	else
	{
		node *n=head;
		i--;
		while(i--)
		    n=n->next;
		cout<<n->next->data<<"已被删除"<<endl;
		n->next=n->next->next;
		count--;
		return 1; 
	}
} 
//输出单链表 
int list::print()
{
	node *n=head;
	cout<<"当前单链表为:";
	while(n->next!=NULL)
	{
		node *p=n->next;
		cout<<p->data<<" ";
		n=p;
	}
	cout<<endl;
	return 0;
}
int main()
{
	list obj1;
	int n,x;
	obj1.create_L();
	obj1.print();
	cout<<"单链表的长度为:"<<obj1.length()<<endl;
	cout<<"请输入所要查找的位置:";
	cin>>n; 
	obj1.get_element(n);
	obj1.print();
	cout<<"请输入所要插入的位置以及所要插入的元素:";
	cin>>n>>x;
	obj1.insert(n,x);
	obj1.print();
	cout<<"请输入所要删除的位置:";
	cin>>n;
	obj1.del_element(n);
	obj1.print();
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是整数单链表建立、查找、插入、删除、输出程序(C++实现): ```cpp #include <iostream> using namespace std; // 定义链表节点结构体 struct ListNode { int val; ListNode* next; ListNode(int x): val(x), next(NULL) {} }; // 建立链表函数 ListNode* createList() { ListNode* head = new ListNode(0); // 创建头结点 ListNode* cur = head; // 创建当前结点指针,初始化指向头结点 int x; while (cin >> x && x != -1) { // 输入-1表示结束 ListNode* newNode = new ListNode(x); // 创建新结点 cur->next = newNode; // 将新结点链接到当前结点后面 cur = cur->next; // 当前结点指针指向新结点 } return head; } // 查找链表中是否存在某个元素 bool findElement(ListNode* head, int x) { ListNode* cur = head->next; // 从第一个结点开始查找 while (cur != NULL) { if (cur->val == x) { return true; // 找到了 } cur = cur->next; // 继续往后查找 } return false; // 没找到 } // 向链表中插入一个元素 void insertElement(ListNode* head, int pos, int x) { ListNode* cur = head; // 从头结点开始查找 for (int i = 0; i < pos && cur != NULL; i++) { // 找到要插入位置的前一个结点 cur = cur->next; } if (cur == NULL) { // 如果没有找到,则插入失败 cout << "插入失败,位置不存在!" << endl; return; } ListNode* newNode = new ListNode(x); // 创建新结点 newNode->next = cur->next; // 将新结点链接到当前结点的后面 cur->next = newNode; // 将当前结点链接到新结点后面 } // 从链表中删除一个元素 void deleteElement(ListNode* head, int x) { ListNode* cur = head; // 从头结点开始查找 while (cur->next != NULL && cur->next->val != x) { // 找到要删除的结点的前一个结点 cur = cur->next; } if (cur->next == NULL) { // 如果没有找到,则删除失败 cout << "删除失败,元素不存在!" << endl; return; } ListNode* temp = cur->next; // 找到要删除的结点 cur->next = temp->next; // 将要删除的结点从链表中删除 delete temp; // 释放要删除的结点的空间 } // 遍历链表并输出所有元素 void traverseList(ListNode* head) { ListNode* cur = head->next; // 从第一个结点开始遍历 while (cur != NULL) { cout << cur->val << " "; cur = cur->next; } cout << endl; } int main() { ListNode* head = createList(); // 建立链表 traverseList(head); // 输出链表元素 int x, pos; cout << "请输入要查找的元素:"; cin >> x; if (findElement(head, x)) { cout << "元素 " << x << " 存在!" << endl; } else { cout << "元素 " << x << " 不存在!" << endl; } cout << "请输入要插入的元素和位置(位置从0开始):"; cin >> x >> pos; insertElement(head, pos, x); traverseList(head); cout << "请输入要删除的元素:"; cin >> x; deleteElement(head, x); traverseList(head); return 0; } ``` 程序中,我们首先定义了一个链表节点结构体 `ListNode`,其中包含一个整数值 `val` 和一个指向下一个节点的指针 `next`。然后,我们实现了链表的建立、查找、插入、删除和遍历等基本操作。在主函数中,我们通过调用这些函数来对链表进行操作,从而实现了整数单链表建立、查找、插入、删除和输出
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值