C语言数据结构 如何建立单向循环链表并且输入值

Python025

C语言数据结构 如何建立单向循环链表并且输入值,第1张

#include <iostream>

using namespace std

typedef char ElemType

typedef int Status

#define OK 1

#define ERROR 0

typedef struct Lnode

{

ElemType data

struct Lnode *next

}Lnode,*LinkList

void Creat_List(LinkList L)//创建单链表并输入元素

{

LinkList p,q

q=L

char ch

cout<<"请输入链表元素,并且以输入#表示结束!"<<endl

while(cin>>ch&&ch!='#')

{

p=new Lnode[sizeof(Lnode)]

if(!p)

{

cout<<"获取内存失败"<<endl

exit(ERROR)

}

p->data=ch//尾插法

L->next=p

L=p

}

L->next=q

}

void output_List(LinkList L)//遍历单链表(输出单链表元素)

{

LinkList p

p=L->next

if(p==L)

{

cout<<"该链表是空链表!"<<endl

exit(ERROR)

}

while(p!=L)

{

cout<<p->data<<"   "

p=p->next

}

}

Status main()

{

LinkList H

H=(LinkList)malloc(sizeof(Lnode))

H->next=NULL//设置头结点为空

    Creat_List(H)

output_List(H)

return 0

}//头结点有和没有都是可以的,头结点只是为了让操作链表更方便,

#include<iostream>

using std::cin

using std::cout

using std::endl

template<class ElemType>

struct Node

{

ElemType data

Node<ElemType>* next

Node()

: next(0)

{}

}

//单链表的模板类

template<class ElemType>

class Linklist

{

private:

Node<ElemType>* head

int _length

public:

Linklist(){}

void Print(void)

void Sort(void)

void Delete(int)

void Creat(void)

void GameDelete(int)

void LinklistToCirclelist()

int Length() { return _length}

void CombineKeepSame(Linklist<ElemType>&)

void CombineKillSame(Linklist<ElemType>&)

void CopyTo(Linklist<ElemType>&)

~Linklist()

{ while(head->next!=0)

{

Node<ElemType>* tmpNode=head->next

for(int i=0i<_length-1i++)

tmpNode=tmpNode->next

delete tmpNode

}

delete head

}

}

//单链表建立后,进行逐一赋值

template<class ElemType>

void Linklist<ElemType>::Creat()

{

int len

cout<<"该的单链表长度:"

cin>>len

head=new Node<ElemType>

_length=0

Node<ElemType>* p

p=head

for(int i=0i<leni++)

{

p->next=new Node<ElemType>

cout<<"第"<<i+1<<"个链表元素:"

cin>>p->next->data

p=p->next

_length++

}

if(_length != len )

cout<<"输入的个数不对:len = "<<len<<" _length = "<<_length<<endl

}

//拷贝函数,将一个对象的数据域拷贝到另一个对象

template<class ElemType>

void Linklist<ElemType>::CopyTo(Linklist<ElemType>&tmpLink)

{

tmpLink._length=_length

tmpLink.head=new Node<ElemType>

Node<ElemType>*tmpNode=tmpLink.head

Node<ElemType>*curListNode=head

for(int i=0i<_lengthi++)

{

tmpNode->next=new Node<ElemType>

tmpNode->next->data = curListNode->next->data

tmpNode = tmpNode->next

curListNode = curListNode->next

}

}

//打印函数,对象打印

template<class ElemType>

void Linklist<ElemType>::Print()

{

cout<<"print list : "

Node<ElemType>* tmpNode=head->next

for(int i=0i<_lengthi++)

{

cout<<tmpNode->data<<" "

tmpNode=tmpNode->next

}

cout<<endl

}

//排序函数,对象排序

template<class ElemType>

void Linklist<ElemType>::Sort()

{

ElemType temp

Node<ElemType>* tmpNode=(*head).next

for(int i=0i<_lengthi++)

{

for(int j=0j<_lengthj++)

{

if((*tmpNode).next==0)tmpNode=(*head).next

if((*tmpNode).data>(*tmpNode).next->data)

{

temp=(*tmpNode).data

(*tmpNode).data=((*tmpNode).next)->data

((*tmpNode).next)->data=temp

}

else

tmpNode=(*tmpNode).next

}

}

}

template<class ElemType>

void Linklist<ElemType>::LinklistToCirclelist()//把单链表转化为单循环链表

{

Node<ElemType>*tmpNode=head->next

while(0!=tmpNode->next)

tmpNode=tmpNode->next

head=head->next

tmpNode->next=head

}

template<class ElemType>

void Linklist<ElemType>::Delete(int position)

{

Node<ElemType>*tmpNode=head

Node<ElemType>*delNode

for(int i=1i<position-1&&_length>1i++)

{

tmpNode=tmpNode->next

}

if(_length>1)

{

delNode=tmpNode->next

tmpNode->next=delNode->next

head=delNode->next

delete delNode

_length--

}

}

template<class ElemType>

void Linklist<ElemType>::GameDelete(int position)

{

while(_length!=1)

{

Delete(position)

Print()

}

}

int main()

{

Linklist<char>LA

cout<<"请输入一个单链表(字母):"<<endl

LA.Creat()

LA.Print()

LA.LinklistToCirclelist()//你可以在这设置个断点看看

int x

cin>>x

return 0

}//这个是我上次试验写的,字数限制删除了两个函数…………

如果存在空头结点:

p->next

head

->next

如果不存在空头结点:

p->next

head

判断空链表的条件是:

head==head->next;

rear==rear->next;

扩展资料:

用尾指针rear表示的单循环链表对开始结点a1和终端结点an查找时间都是O(1)。而表的操作常常是在表的首尾位置上进行,因此,实用中多采用尾指针表示单循环链表。

在单链表中,从一已知结点出发,只能访问到该结点及其后续结点,无法找到该结点之前的其它结点。而在单循环链表中,从任一结点出发都可访问到表中所有结点,这一优点使某些运算在单循环链表上易于实现。