单链表基本操作的实现--创建、插入、查找、删除
程序员文章站
2024-03-06 08:57:07
...
单链表基本操作的实现--创建、插入、查找、删除
其中,查找和删除分为 按值查找删除和按序号查找删除;
附上代码(具体的代码实现见注释解析)
linkedlist.h(链表的头文件)
#pragma once
#include <iostream>
class Node
{
public:
int data;
Node* next;
};
class LinkedList
{
private:
Node *head;
public:
LinkedList() {
head = new Node;
head->data = 0;
head->next = NULL;
}
~LinkedList(){delete head;}
void create(int n); //创建新链表
bool insert (int val,int index,LinkedList *linklist );
bool deleta (int val,LinkedList *linklist);// 按值删除
bool deleta2(int index,LinkedList *linklist); //按序删除
int search (int val, LinkedList *linklist); //按值查找
Node* search2(int index,LinkedList *linklist); //按序查找
void print(LinkedList *linklist);
};
linklist.cpp
#include<iostream>
#include "linklist.h"
using namespace std;
void LinkedList::create(int n)//创建新链表
{
Node *pnew, *ptemp;
ptemp = head;
int val;
for(int num = 1; num<=n; num++)
{
cout << "请输入第" << num << "结点" << endl;
pnew = new Node();
cin >> val;
pnew->data = val;
pnew->next = NULL;
ptemp->next = pnew;
ptemp = pnew;
}
ptemp->next = NULL;
}
//插入, 在index位置处插入节点node,以位置1为开始
void LinkedList::print(LinkedList *linklist)
{
Node *ptemp;
cout << "遍历打印" << endl;
ptemp = head->next;
while(ptemp!= NULL) //ptemp ->next 错误
{
cout << ptemp->data << " ";
ptemp = ptemp->next;
}
cout << endl;
}
int LinkedList::search(int val, LinkedList *linklist) // 返回找到的位置,0即为未找到
{
Node *ptemp;
ptemp = head->next;
int num = 1;
while(ptemp != NULL)
{
if(ptemp->data == val)
{
cout << "已经找到该元素,位置在" << num << endl;
return num;
}
else
{
num++;
ptemp = ptemp->next;
}
}
cout <<"没有找到该元素" << endl;
return 0;
}
//按序号查找,返回该值的结点
Node* LinkedList::search2(int index,LinkedList *linklist)
{
Node *ptemp ;
ptemp = head->next;
if(index == 1)
{
//cout << "此时序号为"<< index << "值为" << ptemp->data << endl;
return ptemp;
}
else
{
for(int num = 1; num < index; num++)
ptemp = ptemp->next;
//cout << "此时序号为"<< index << "值为" << ptemp->data << endl;
return ptemp;
}
}
bool LinkedList::insert (int val,int index,LinkedList *linklist )
{
if(index <= 0)
return false;
Node *phead,*ptemp,*pnew;
pnew = new Node();
phead = head->next;
ptemp = search2(index-1, linklist);
pnew->data = val;
pnew->next = ptemp->next;
ptemp->next = pnew;
cout <<"insert successfully" << endl;
return true;
}
//删除某个值;
bool LinkedList::deleta (int val,LinkedList *linklist)
{
Node *ptemp,*pdel,*pnew;
int num = 1;
ptemp = head->next;
int index = search(val, linklist);
if(index == 0)
{
cout << "没有找到该值" << endl;
return false;
}
else if(index == 1)
{
ptemp = head->next;
pnew = ptemp->next;
head->next = pnew;
cout << "删除成功" << endl;
return true;
}
else
{
while(num != index-1)
{
ptemp = ptemp->next;
num ++;
}
pnew = ptemp->next->next;
ptemp->next = pnew;
cout << "删除成功" << endl;
return true;
}
}
//按序删除
bool LinkedList::deleta2(int index,LinkedList *linklist)
{
Node *ptemp, *pnew;
if(index <= 0)
{
cout << "删除失败" << endl;
return false;
}
else if(index == 1)
{
ptemp = head->next;
pnew = ptemp->next;
head->next = pnew;
cout << "删除成功" << endl;
return true;
}
else
{
ptemp = search2(index-1, linklist);
pnew = ptemp->next->next;
ptemp->next = pnew;
cout << "删除成功" << endl;
return true ;
}
}
测试程序:
#include<iostream>
#include "linklist.h"
using namespace std;
int main() {
LinkedList linkedlist;
cout << "创建链表" << endl;
linkedlist.create(4);
linkedlist.print(&linkedlist);
cout << endl;
cout << "按值查找" << endl;
linkedlist.search(2,&linkedlist);
cout << endl;
cout << "按序号查找" << endl;
Node *ptemp = linkedlist.search2(3,&linkedlist);
cout << "此序号值为" << ptemp->data << endl;
cout << endl;
cout << "插入" << endl;
linkedlist.insert(5,3,&linkedlist);
linkedlist.print(&linkedlist);
cout << endl;
cout << "按序号删除" << endl;
linkedlist.deleta2(1,&linkedlist);
linkedlist.print(&linkedlist);
cout << endl;
cout << "按值删除" << endl;
linkedlist.deleta(2,&linkedlist);
linkedlist.print(&linkedlist);
system("pause");
return 0;
}