首页 > 其他 > 详细

数据结构——删除递增链表区间节点

时间:2019-03-17 12:02:45      阅读:169      评论:0      收藏:0      [点我收藏+]
#if 1
#include<stdio.h>
#include<stdlib.h>
#include<iostream>

using namespace std;

struct Node
{
	int data;
	Node *next;
};

//初始化
Node *init()
{
	Node *head=new Node;
	head->next=NULL;
	return head;
}

//头插法创建节点
void insetList(Node *head,int i)
{
	Node *cur=new Node;

	cur->data=i;

	cur->next=head->next;
	head->next=cur;
}

//链表A生成
void CreateList(Node *head_A)
{
	for(int i=20;i>0;i--)
	{

		insetList(head_A,--i);
	}
}

//打印链表
void print(Node *head)
{
	Node *temp=head->next;	//防止头指针移动
	while(temp)
	{
		cout<<temp->data<<"  ";
		temp=temp->next;

	}
}


void  DeleteElement(Node *head_A)
{
	Node *pa=head_A->next;	//pa指向链表A的首元节点
	
	int mink=5;int maxk=15;
			
	Node *pc=head_A;			//pc为pa前驱

	
	while(pa)		
	{
		if(pa->data>mink&&pa->data<maxk)
		{
			pc->next=pa->next;   //pc指向pa的后继节点
			delete pa;
		}
		else
		{
			pc->next=pa;		//pc指向小于mink的节点
			pc=pc->next;		//pc指针移动
		}
		pa=pc->next;			//pa
	}

}
void main()
{
	Node *head_A=init();	//链表A初始化头节点

	//创建链表A
   CreateList(head_A);

   //打印链表A
   cout<<"链表A为:"<<endl;
   print(head_A);
   cout<<endl;

   //调用函数排除元素
   DeleteElement(head_A);

    //打印链表C
   cout<<endl<<"删除后链表为:"<<endl;
   print(head_A);
   cout<<endl;

   system("pause");

}
#endif

/*
总结:
删除顺序链表区间元素,需要一个前驱节点,其中两个临时的移动结构体指针
*/

  

数据结构——删除递增链表区间节点

原文:https://www.cnblogs.com/huxiaobai/p/10546096.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!