單鏈表基本操作的實現--創建、插入、查找、刪除

單鏈表基本操作的實現--創建、插入、查找、刪除

其中,查找和刪除分爲 按值查找刪除和按序號查找刪除;


附上代碼(具體的代碼實現見註釋解析)

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;  
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章