1. 程式人生 > >帶有頭結點的迴圈單鏈表的相關操作

帶有頭結點的迴圈單鏈表的相關操作

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;
}