带有头结点的循环单链表的相关操作

link.h

//带有头结点的循环链表的建立
#include<iostream>
using namespace std;

struct listnode;
typedef struct listnode *link;
struct listnode
{
	int element;
	link next;
};

link Init_Link();//初始化链表
int IsEmpty_Link(link );//判断链表是否为空
void MakeEmpty_Link(link);//链表置空,释放内存
void InsertNode_link(link ,int );//插入结点
void DeleteNode_link(link,int );//删除结点
int FindNode_Link(link,int );//查找结点
void Print_link(link);//打印链表

link.cpp

#include "link.h"
#include<stdlib.h>
link Init_Link()//初始化带有头结点的链表
{
	link head=(link)malloc(sizeof(struct listnode));
	if(head==NULL)
	{
		cout<<"insufficient memory!"<<endl;
		exit(0);
	}
	head->next=head;
	return head;
}
int IsEmpty_Link(link head )
{
	return head->next==head;
}
void MakeEmpty_Link(link head)
{
    cout<<"链表置空中..."<<endl;
	link p=head->next;
	while(p!=head)//在结点P的指针域的指针指向头结点时,循环结束
	{
		link tmp;
		tmp=p;
		p=p->next;//必须先指定后继结点,在对该结点进行释放内存的工作
		free(tmp);
	}
	head->next=head;
}
void InsertNode_link(link head ,int x )//保证按序插入
{
	link newnode=(link)malloc(sizeof(struct listnode));
	newnode->element =x;
   
	if(head->next==head)//若开始时,链表中只有一个头结点
	{
		head->next=newnode;
		newnode->next=head;
	}
	else
	{
		link p;//pre指向的结点为p所指向的结点的前驱结点
		link pre=head;//为了避免插入的第二个实结点比第一个实结点小,导致for循环只运行一次便break,从而使pre没有被赋值的情况,在这里先为pre赋值
	    for(p=head->next;p!=head;pre=p,p=p->next)
	    {
		   if(p->element > newnode->element)
			  break;
	    }
	    pre->next=newnode;
	    //if(p==head)  //这里实际上不用用条件判断,直接newnode->next=p即可
		   //newnode->next=head;
	    //else
		   //newnode->next=p;
		newnode->next=p;
	}
}
void DeleteNode_link(link head,int x)
{
	link p,pre;
	link tmp;
	if(IsEmpty_Link(head))
	{
		cout<<"链表已为空"<<endl;
		exit(0);
	}
	if(!FindNode_Link(head,x))
	{
		cout<<"元素为x的节点没有找到,不能进行删除操作!"<<endl;
		exit(0);
	}
	for(p=head->next;p!=head;pre=p ,p=p->next)
	{
		if(p->element==x)
			break;
	}
	tmp=p;
	pre->next=p->next;
	free(tmp);
}
int FindNode_Link(link head,int x )
{
	link p;
	for(p=head->next;p!=head;p=p->next)
	{
		if(p->element==x)
			return 1;

	}
	return 0;
}
void Print_link(link head)
{
	link p;
	for(p=head->next;p!=head;p=p->next)
	{
		cout<<p->element<<" ";
	}
	cout<<endl;
}

main.cpp

/*******************************************************************************************
 *name:jae chia                                                                            *                                      
 *date:2014.6.19                                                                           *
 *purpose:带有头结点的循环单链表的建立,插入,删除等操作                                    *
 *version:1.0                                                                              *
 *******************************************************************************************/

#include"link.h"
int main(void)
{
	link head;
	head=Init_Link();
	if(IsEmpty_Link(head))
	{
		cout<<"now,the link is empty!"<<endl;
	}
	cout<<"then insert the element into the link..."<<endl;
	InsertNode_link(head,3);
	InsertNode_link(head,2);
	InsertNode_link(head,1);
	//Print_link(head);
	//MakeEmpty_Link( head);
	InsertNode_link(head,5);
	InsertNode_link(head,4);
	cout<<"print the link:"<<endl;
	Print_link(head);
	cout<<endl<<"now ,delete the node which element is 3"<<endl;
	DeleteNode_link(head,3);
	cout<<endl<<"print the link "<<endl;
	Print_link(head);
	
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值