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

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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值