单链表设计与实现C++

这篇博客详细介绍了单链表的数据结构,并提供了C++实现的初始化、指定位置插入和删除、获取链表长度、头插法、尾插法、头删法以及迭代反转链表的方法。通过示例代码展示了如何操作链表,包括插入数据、删除数据以及对链表进行反转等操作。
摘要由CSDN通过智能技术生成

数据结构课程设计之单链表设计与实现。

链表的结构体:

typedef struct  LNode 
{
	int    data;//数据域
	struct LNode   *next;//指针域
} LNode, *LinkList; 

链表的初始化(带头节点):

bool initList(LinkList& list)
{
	list = new LNode();
	list->next = NULL;
	return true;
}

链表的指定位置插入:

bool insertList(LinkList& list, int pos,int e)
{
   if(list == NULL)
	   return false;

   LNode* pre = findPosNode(list,pos-1);
   if(pre == NULL)
	   return false;
   
   LNode* newNode = new LNode();
   newNode->data = e;
   newNode->next = pre->next;
   pre->next = newNode;
   return true;
}

链表的指定位置删除:

bool deleteList(LinkList& list, int pos,int& e)
{
   if(list == NULL)
	   return false;

   LNode* pre = findPosNode(list,pos-1);
   if(pre == NULL)
	   return false;
   
   LNode* delNode = pre->next;
   pre->next = pre->next->next;
   e = delNode->data;
   delete delNode;

   return true;
}

 得到数据节点的个数:

int getListLength(LinkList& list )
{
	 
	int cnt = 0; /* 初始化计数器 count */

	LNode* p = list->next;  /* p指向表的第一个数据结点 */
	while ( p != NULL) { 
		p = p->next; 
		cnt++; /* 当前p指向的是第cnt个结点*/
	}   
	return cnt;
}

头插法:

void insertListByHead(LinkList& list, int x)
{
	
		LNode* newNode = new LNode();
		newNode->data = x;
		newNode->next = list->next;  
		list->next = newNode;	
}

 尾插法:

void insertListByTail(LinkList& list,int x)
{
	if(list == NULL)//判断单链表存不存在
		return;
    LNode* tail = list; 
	while(tail->next != NULL)//判断是不是尾节点
		tail = tail->next;  //往后面走一步
		LNode* newNode = new LNode(); //依次创建新节点
		newNode->data = x;
		newNode->next = NULL;
		tail->next = newNode; //把新节点链到尾节点后面
		tail = newNode;       //指定新的尾节点
}

 头删法:

int deleteListByHead(LinkList& list,int x)
{
	
		LNode* delNode = list->next;
		list->next=list->next->next;
        x=delNode->data;
		delete delNode;
		return x;
	
}

迭代实现链表反转:

重复上面的步骤

直至走完循环。 

执行L -> next = preNode;语句,完成反转。

LNode* reverse(LinkList& list) {
      LNode *preNode = NULL, *curNode = list->next;
      while (curNode) {
         LNode *nextNode = curNode -> next;
          curNode -> next = preNode;
          preNode = curNode;
          curNode = nextNode;
      }
    list -> next = preNode;
    return list;
 }

最终代码如下:

#include <iostream>
using namespace std;
//链表结构体 
typedef struct  LNode 
{
	int    data;//数据域
	struct LNode   *next;//指针域
} LNode, *LinkList;  
//初始化链表 
bool initList(LinkList& list)
{
	list = new LNode();
	list->next = NULL;
	return true;
}
//获取pos位置的结点 
LNode* findPosNode( LinkList& list, int pos)
{
	LNode* p  = list;
	int count = 0;

	while(p != NULL)
	{
		if(count == pos)
			return p;  
		count++;	
		p = p ->next;
	}

	return NULL;
}
//指定位置插入 
bool insertList(LinkList& list, int pos,int e)
{
   if(list == NULL)
	   return false;

   LNode* pre = findPosNode(list,pos-1);
   if(pre == NULL)
	   return false;
   
   LNode* newNode = new LNode();
   newNode->data = e;
   newNode->next = pre->next;
   pre->next = newNode;
   return true;
}
//指定位置删除 
bool deleteList(LinkList& list, int pos,int& e)
{
   if(list == NULL)
	   return false;

   LNode* pre = findPosNode(list,pos-1);
   if(pre == NULL)
	   return false;
   
   LNode* delNode = pre->next;
   pre->next = pre->next->next;
   e = delNode->data;
   delete delNode;

   return true;
}
//打印 
void print(LinkList& list)
{
	if(list == NULL)
		return;
	LNode* p = list->next;
	while(p != NULL)
	{
        cout<<p->data<<" ";
		p = p->next;
	}
	cout<<endl;
}
//得到数据节点的个数
int getListLength(LinkList& list )
{
	 
	int cnt = 0; /* 初始化计数器 count */

	LNode* p = list->next;  /* p指向表的第一个数据结点 */
	while ( p != NULL) { 
		p = p->next; 
		cnt++; /* 当前p指向的是第cnt个结点*/
	}   
	return cnt;
}
//头插法 
void insertListByHead(LinkList& list, int x)
{
	
		LNode* newNode = new LNode();
		newNode->data = x;
		newNode->next = list->next;  
		list->next = newNode;	
}
//尾插法 
void insertListByTail(LinkList& list,int x)
{
	if(list == NULL)//判断单链表存不存在
		return;
    LNode* tail = list; 
	while(tail->next != NULL)//判断是不是尾节点
		tail = tail->next;  //往后面走一步
		LNode* newNode = new LNode(); //依次创建新节点
		newNode->data = x;
		newNode->next = NULL;
		tail->next = newNode; //把新节点链到尾节点后面
		tail = newNode;       //指定新的尾节点
}
//头删法 
int deleteListByHead(LinkList& list,int x)
{
	
		LNode* delNode = list->next;
		list->next=list->next->next;
        x=delNode->data;
		delete delNode;
		return x;
	
}
//迭代实现链表反转 
LNode* reverse(LinkList& list) {
      LNode *preNode = NULL, *curNode = list->next;
      while (curNode) {
         LNode *nextNode = curNode -> next;
          curNode -> next = preNode;
          preNode = curNode;
          curNode = nextNode;
      }
    list -> next = preNode;
    return list;
}

int main()
{
	LinkList  list = NULL;
	//初始化链表,带头节点的链表。 
	initList(list);
	cout<<"插入十个数据:"; 
	for(int i = 0; i < 10; i++)
		insertList(list,i,i);
	print(list);
	cout<<"反转后链表:"; 
	LNode *r=reverse(list);
	print(r);
	cout<<"头插一个10:";
	insertListByHead(list,10);
	print(list);
	cout<<"尾插一个0: ";
	insertListByTail(list,0);
	print(list);
	cout<<"pos位置后的链表:";
	LNode *t=findPosNode(list,5); 
	print(t);
	return 0;
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈chay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值