用c/c++自己动手实现一个链表LinkList

链表的特点

  • 便于插入删除,这些操作只要改变指针的指向
  • 遍历查找O(n)
  • 排序复杂度也极大
  • 所以适合插入删除频繁的程序。

实现思想

  • 自建基本的节点数据结构
  • 明确基本操作:初始化、插入、删除、按位置查找、按值查找等等

实现代码

  • 注:vs2008
// LinkList.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>

#include <iostream>


typedef void * ElemType;//便于扩展存储的类型,c++可以把任意内置类型的指针强制转换成void*,void* 也可赋值给他们。

typedef struct __ListNode{
	ElemType data;
	__ListNode* next;
}*LinkNode;

class LinkList{
public:
	LinkList();
public:
	void Init();//初始化,建头节点
	bool InsertAtLoc(ElemType, int);//在某合理位置插入,缺省值-1代表尾插
	ElemType FindAtLoc(int);//查找合理位置的值,没有返回NULL
	int FindLocByValue(ElemType);//查找某值的位置,未找到返回-1
	int GetLength();
	bool IsEmpty();
	bool DeleteAtLoc(int);//删除某位置的值
	void DestroyList();//删除整个链表

private:
	LinkNode head;
	LinkNode tail;
	int ListCount;
};
LinkList::LinkList() {
	Init();
}
void LinkList::Init() {
	head = (LinkNode)malloc(sizeof(struct __ListNode));
	head->next = NULL;
	head->data = NULL;
	tail = head;
	ListCount = 0;
}
bool LinkList::InsertAtLoc(ElemType value, int loc = -1) {
	if(loc>ListCount||loc==0||loc<-1)
	{
		return false;
	}
	LinkNode new_node = (LinkNode)malloc(sizeof(struct __ListNode));
	if(new_node == NULL)
	{
		return false;
	}
	new_node->data = value;
	if(loc == -1)
	{//缺省值,插入到最后
		tail->next = new_node;
		new_node->next = NULL;
		tail = new_node;
		ListCount++;
		return true;
	}

	LinkNode p = head;
	int cnt = 0;
	while(p->next!=NULL)
	{
		if(cnt == loc)//在尾节点之前插入
		{
			new_node->next = p->next;
			p->next = new_node;
		}
		p = p->next;
		cnt++;
	}
	if (cnt == loc)//尾节点之后插入
	{
		new_node->next = p->next;
		p->next = new_node;
		tail = new_node;
	}
	ListCount++;
	return true;
}

ElemType LinkList::FindAtLoc(int loc) {
	if (loc<0||loc>ListCount)
	{
		return NULL;
	}
	LinkNode p = head;
	int cnt = 0;
	while(p->next!=NULL)
	{
		if(loc == cnt)
		{//在尾节点之前找到
			return p->data;
		}
		cnt++;
		p = p->next;
	}
	if (cnt == loc)//在尾节点找到
	{
		return p->data;
	}
	return NULL;
}
int LinkList::FindLocByValue(ElemType elem)
{
	LinkNode p= head;
	int cnt=0;
	while(p->next!=NULL)
	{
		if (p->data == elem)//在尾节点前面找到
		{
			return cnt;
		}
		cnt++;
		p=p->next;
	}
	if (p->data == elem)//在尾节点找到
	{
		return cnt;
	}
	return -1;//未找到
}
bool LinkList::DeleteAtLoc(int loc) {
	if(loc>ListCount||loc<0)
	{
		return false;
	}
	LinkNode p = head;
	int cnt = 0;
	LinkNode pre = NULL;
	while(p->next!=NULL)
	{
		if(loc == cnt)//删除节点在尾节点之前
		{
			pre->next = p->next;
			free(p);
			ListCount--;
			return true;
		}
		pre =p;
		p = p->next;
		cnt++;
	}
	if (loc == cnt)//删除尾节点
	{
		pre->next =NULL;
		tail = pre;//删除的在最后,尾指针要改变
		free(p);
		ListCount--;
		return true;
	}
	return false;//删除失败
	
}
void LinkList::DestroyList() {
	for (int i= 1;i<=ListCount;++i)
	{
		DeleteAtLoc(1);//一直删第一个即可
	}
}

int LinkList::GetLength()
{
	return ListCount;
}

bool LinkList::IsEmpty()
{
	return GetLength()==0?true:false;//或者判断尾指针是不是空指针
}
int _tmain(int argc, _TCHAR* argv[])
{
	LinkList l;
	int t1,t2;
	t1 = 16;
	t2 = 8;
	ElemType data1 = (void*)(&t1);
	//测试插入和查找
	l.InsertAtLoc(data1);
	data1 = (void*)(&t2);
	l.InsertAtLoc(data1);
	void* p = l.FindAtLoc(1);
	std::cout<<*(int*)p<<std::endl;
	//测试删除和查找
	l.DeleteAtLoc(2);
	p = l.FindAtLoc(1);
	std::cout<<*(int*)p<<std::endl;
	int cur = l.FindLocByValue((void*)(&t2));
	//测试删除整个链表
	l.DestroyList();
	int ret = l.GetLength();
	bool b = l.IsEmpty();
	system("pause");
	return 0;
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include"iostream" #include"string" #include<iomanip> #include<fstream> using namespace std; typedef int Status; typedef int ElemType; #define OK 1 #define ERROR 0 typedef struct LNode//定义 { ElemType data;//结点的数据域 struct LNode *next;//结点的指针域 }LNode,*LinkList; //LinkList为结构体LNode的指针类型 Status InitList(LinkList &L)//初始 { L=new LNode; L->next=NULL; return OK; } void GreateList_L(LinkList &L,int n)//后插法 输入元素 { /*L=new LNode;//生成新的结点作为头结点,用头指针L指向头结点 L->next=NULL;//头结点的指针域为空*/ LNode *r; LNode *p; r=L; //!!!!错误,定义指针 for(int i=0;i<n;++i) { p=new LNode; cin>>p->data; p->next=NULL; r->next=p; r=p; } } /*void CreateList_L(LinkList &L,int n)//前插法 { cout<<"输入录取数据个数 "; cin>>n; cout<<"输入数据"; L=new LNode;//生成新的结点作为头结点,用头指针L指向头结点 L->next=NULL;//头结点的指针域为空 for(int i=0;i<n;++i) { p=new LNode; cin>>p->data; p->next=L->next; L->next=p; } }*/ Status GetElem(LinkList L,int i,int &e)//取值 { LNode *p; p=L->next;int j=1; while(p&&j<i) { p=p->next; ++j; } if(!p||j>i) return ERROR; e=p->data; return OK; } LNode *LocateElem(LinkList L,ElemType e)//查找 { int j=1; LNode *p; p=L->next; while(p&&p->data!=e) { p=p->next; } return p; } Status LisInsert(LinkList &L,int i,ElemType e)//插入 { LNode *p; p=L->next;int j=0; while(p&&(j<i-1)) { p=p->next; j++; } if(!p&&(j>i-1)) return ERROR; LNode *s; s=new LNode; s->data=e; s->next=p->next; p->next=s; return OK; } Status ListDelete(LinkList &L,int i)//删除 { LNode *p; p=L->next;int j=0; while(p&&(j<i-1)) { p=p->next; j++; } if(!(p->next)&&(j>i-1)) return ERROR; LNode *q; q=p->next; p->next=q->next; delete q; return OK; } int main() { LNode *L; int i,temp,n,choose=-1; int e; cout << "1. 创建信息表\n"; cout << "2. 查找元素\n"; cout << "3. 插入元素\n"; cout << "4. 删除元素\n"; cout << "0. 退出程序\n\n"; while(choose!=0) { cout << "请选择:"; cin >> choose; switch (choose) { case 1: cout<<"请输入元素个数:"; cin>>n; cout<<"\n"<<"请输入元素"; GreateList_L(L,n); cout<<"\n"; break; case 2:cout<<"请输入查找的元素:"; cin>>e; LNode *s; s=LocateElem(L,e); cout<<"这个元素的地址是"<<s<<"\n\n"; break; case 3:cout<<"请输入需要插入的数:"; cin>>e; cout<<"\n"<<"请输入需要插入的位置"; cin>>i; if(LisInsert(L,i,e)) cout<<"插入成功\n\n"; else cout<<"插入失败\n\n"; break; case 4: cout<<"请输入删除的位置:"; cin>>i; if(ListDelete(L,i)) cout<<"删除成功\n\n"; else cout<<"删除失败\n\n"; break; } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值