单链表遍历,建立,插入,删除

1.1链表的概念
概念:链表是一种物理存储结构上非连续、非顺序的存储结构,但链表在逻辑上是连续的,顺序的,而数据元素的逻辑顺序是通过链表中的指针连接次序实现的。

1.2 链表的结构
链表是由一个个结点组成的.

注意:链表中的最后一个结点的next指向空,next=NULL。

一个个结点串成了链表

 有人可能会有疑问,不是说链表只是在逻辑结构上是连续的,在物理存储结构上是不连续的,那为什么上图中一个个结点明明是挨在一起的,那么它在物理存储结构上肯定是连续的呀,其实不然,上图是为了方便大家理解,才用线条连接了结点,实际上在内存中,每个结点可能会隔得很远,仔细观察每个结点上面的红色文字,那就是这个结点的地址,而蓝色文字是下一个结点的地址,很明显能看到这两个结点并不是相邻的,因此也验证了顺序表在逻辑结构上确实是连续的,但在物理存储结构上确实是不连续的。

1.3 链表的遍历

while(q)
	{
		cout<<q->data<<endl;
		q=q->next;
	}

1.4 链表的建立

(1)头插法

#include<iostream>
#include<algorithm>
using namespace std;
typedef struct asd{
	int data;
	struct asd *next;
}lin,*node;
int main()
{
	node head=(node)malloc(sizeof(lin));
	head->next=NULL;
	for(int i=1;i<=5;i++)
	{
		int a;
		cin>>a;
		node p=(node)malloc(sizeof(lin));
		p->data=a;
		p->next=head->next;
		head->next=p;
	}
	node q=head->next;
	while(q)
	{
		cout<<q->data<<endl;
		q=q->next;
	}
	return 0;
}

(2)尾插法

#include<iostream>
#include<algorithm>
using namespace std;
typedef struct asd{
	int data;
	struct asd *next;
}lin,*node;
int main()
{
	node head=(node)malloc(sizeof(lin));
	node rear=head;
	for(int i=1;i<=5;i++)
	{
		int a;
		cin>>a;
		node p=(node)malloc(sizeof(lin));
		p->data=a;
		rear->next=p;
		rear=p;
    }
    rear->next=NULL;
    node q=head->next;
    while(q)
    {
    	cout<<q->data<<endl;
    	q=q->next;
	}
	return 0;
}

1.5 链表的插入

#include<iostream>
#include<algorithm>
using namespace std;
typedef struct asd{
	int data;
	struct asd *next;
}lin,*node;
void charu(node head,int a,int b)
{
	node p=head->next;
	while(p)
	{
		if(p->data==a)
		{
		  break;
		}
		p=p->next;
	}
	if(p==NULL)
	{
		
		cout<<"NO"<<endl;
		return ;
	}
	node q=(node)malloc(sizeof(lin));
	q->data=b;
	q->next=p->next;
	p->next=q;
	p=head->next;
	while(p)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
}
int main()
{
	node head=(node)malloc(sizeof(lin));
	node rear=head;
	for(int i=1;i<=5;i++)
	{
		int a;
		cin>>a;
		node p=(node)malloc(sizeof(lin));
		p->data=a;
		rear->next=p;
		rear=p;
    }
    rear->next=NULL;
    int b,c;
    cin>>b>>c;
    charu(head,b,c);
	return 0;
}

1.6 链表的删除

#include<iostream>
#include<algorithm>
using namespace std;
typedef struct asd{
	int data;
	struct asd *next;
}lin,*node;
void charu(node head,int a)
{
	node p=head->next;
	while(p)
	{
		if(p->data==a)
		{
		  break;
		}
		p=p->next;
	}
	if(p==NULL)
	{
		cout<<"NO"<<endl;
		return ;
	}
	node q=(node)malloc(sizeof(lin));
	q=p->next;
	q=q->next;
	p->next=q;
	p=head->next;
	while(p)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	free(q);
}
int main()
{
	node head=(node)malloc(sizeof(lin));
	node rear=head;
	for(int i=1;i<=5;i++)
	{
		int a;
		cin>>a;
		node p=(node)malloc(sizeof(lin));
		p->data=a;
		rear->next=p;
		rear=p;
    }
    rear->next=NULL;
    int b,c;
    cin>>b;
    charu(head,b);
	return 0;
}

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
单链表是由一系列节点组成的数据结构,每个节点包含两个部分:数据域和指针域。数据域用于存储数据,指针域用于指向下一个节点。 单链表建立可以通过不断地在链表尾部插入新节点来实现。具体步骤如下: 1. 创建头节点,头节点不存储数据,只是作为链表的入口。 2. 创建新节点,将数据存储在新节点的数据域中。 3. 将新节点的指针域指向空。 4. 将链表的最后一个节点的指针域指向新节点。 单链表插入操作分为两种情况:在链表头部插入和在链表中间插入。 在链表头部插入新节点的步骤如下: 1. 创建新节点,将数据存储在新节点的数据域中。 2. 将新节点的指针域指向链表的第一个节点。 3. 将头节点的指针域指向新节点。 在链表中间插入新节点的步骤如下: 1. 找到需要插入的位置的前一个节点。 2. 创建新节点,将数据存储在新节点的数据域中。 3. 将新节点的指针域指向原来前一个节点的下一个节点。 4. 将前一个节点的指针域指向新节点。 单链表删除操作也分为两种情况:删除链表头部节点和删除链表中间节点。 删除链表头部节点的步骤如下: 1. 将头节点的指针域指向下一个节点。 2. 释放原来的头节点。 删除链表中间节点的步骤如下: 1. 找到需要删除的节点的前一个节点。 2. 将前一个节点的指针域指向需要删除的节点的下一个节点。 3. 释放需要删除的节点。 总之,单链表的设计思想是通过指针域将一系列节点串联起来,实现数据的存储和操作。在插入删除节点时,需要注意指针的指向关系,确保链表的结构不会被破坏。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值