當前位置:首頁 » 編程語言 » c語言實現循環鏈表
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言實現循環鏈表

發布時間: 2022-07-10 21:43:54

1. 數據結構雙向循環鏈表的c語言實現(插入,查詢,刪除),代碼如下:

#include<stdio.h>
#include<malloc.h>

typedefintElemtype;

typedefstructdNode{
Elemtypedata;/*數據域*/
structdNode*prior;/*指向前驅結點的指針域*/
structdNode*next;/*指向後繼結點的指針域*/
}*pDLink,*DLinkList;

DLinkListGetEmptyDLink(){//初始化
DLinkListhead=(pDLink)malloc(sizeof(structdNode));
head->data=0;
head->prior=head;
head->next=head;
returnhead;
}

voidDLink_create(DLinkListhead,intn){/*雙向循環鏈表建立函數*/
inti;
pDLinkp,r;
p=r=head;
for(i=0;i<n;i++){
p->next=(pDLink)malloc(sizeof(structdNode));/*為一個新結點分配空間*/
scanf("%d",&p->next->data);/*從鍵盤輸入值,並保存在新結點數據域中*/
p=p->next;//p指向新結點
p->prior=r;//新結點的prior指向上一個結點
r=r->next;//上一個結點前進到新結點
}
p->next=head;//指向頭結點
head->prior=p;//head的prior指向最後的結點
}

voidShow(DLinkListhead){//正向顯示鏈表數據
pDLinkp=head->next;
while(p!=head){
printf("%d",p->data);
p=p->next;
}
printf(" ");
}

voidShowR(DLinkListhead){//反向顯示數據
pDLinkp=head->prior;
while(p!=head){
printf("%d",p->data);
p=p->prior;
}
printf(" ");
}

intmain(){
DLinkListhead=GetEmptyDLink();
DLink_create(head,10);
printf("正向顯示: ");
Show(head);
printf("反向顯示: ");
ShowR(head);
return0;
}

2. C語言怎麼動態生成單循環鏈表

在雙向鏈表中,結點除含有數據域外,還有兩個鏈域,一個存儲直接後繼結點地址,一般稱之為右鏈域;一個存儲直接前驅結點地址,一般稱之為左鏈域。
鏈表的C語言實現之循環鏈表及雙向鏈表
一、循環鏈表
循環鏈表是與單鏈表一樣,是一種鏈式的存儲結構,所不同的是,循環鏈表的最後一個結點的指針是指向該循環鏈表的第一個結點或者表頭結點,從而構成一個環形的鏈。
循環鏈表的運算與單鏈表的運算基本一致。所不同的有以下幾點:
1、在建立一個循環鏈表時,必須使其最後一個結點的指針指向表頭結點,而不是象單鏈表那樣置為NULL.此種情況還使用於在最後一個結點後插入一個新的結點。
2、在判斷是否到表尾時,是判斷該結點鏈域的值是否是表頭結點,當鏈域值等於表頭指針時,說明已到表尾。而非象單鏈表那樣判斷鏈域值是否為NULL.
二、雙向鏈表
雙向鏈表其實是單鏈表的改進。
當我們對單鏈表進行操作時,有時你要對某個結點的直接前驅進行操作時,又必須從表頭開始查找。這是由單鏈表結點的結構所限制的。因為單鏈表每個結點只有一個存儲直接後繼結點地址的鏈域,那麼能不能定義一個既有存儲直接後繼結點地址的鏈域,又有存儲直接前驅結點地址的鏈域的這樣一個雙鏈域結點結構呢?這就是雙向鏈表。
在雙向鏈表中,結點除含有數據域外,還有兩個鏈域,一個存儲直接後繼結點地址,一般稱之為右鏈域;一個存儲直接前驅結點地址,一般稱之為左鏈域。

3. 帶有頭結點的的循環雙鏈表的實現和相關操作C語言

#include <stdio.h>
struct node
{
int a;
node *pre;//指向前節點的指針
node *pos;//指向後節點的指針
};
int main()
{
int i=0;
node *head,*tmp;
tmp=head=new node;
head->a=0;
for(i=1;i<20;i++)
{
tmp->pos=new node;
tmp=tmp->pos;
tmp->a=i;
}
tmp->pos=head;//閉環
tmp=tmp->pos;
while(tmp->pos!=head)
{
tmp->pos->pre=tmp;
tmp=tmp->pos;
}
tmp->pos->pre=tmp;

//輸出測試
tmp=head;
do
{
printf("%d ",tmp->a);
tmp=tmp->pos;
}while(tmp!=head);
printf("\n");
tmp=head->pre;
do
{
printf("%d ",tmp->a);
tmp=tmp->pre;
}while(tmp!=head->pre);
printf("\n");

//刪除鏈表

head->pre->pos=NULL;
while(head)
{
tmp=head;
head=head->pos;
delete tmp;
}
return 0;
}

4. 關於C語言版數據結構中的單循環鏈表

void showlist(linklist head){//遍歷輸出鏈表
listnode *p;
p=head;
if(p->next=head){//這行里的p->next=head應寫成p->next==head
printf("list is empty!\n");
return;
}
else{
while((p=p->next)!=head){
printf("%d ",p->data);
}
}
}

5. C語言數據結構 如何建立單向循環鏈表並且輸入值

#include<iostream>
usingnamespacestd;
typedefcharElemType;
typedefintStatus;
#defineOK1
#defineERROR0
typedefstructLnode
{
ElemTypedata;
structLnode*next;
}Lnode,*LinkList;
voidCreat_List(LinkListL)//創建單鏈表並輸入元素
{
LinkListp,q;
q=L;
charch;
cout<<"請輸入鏈表元素,並且以輸入#表示結束!"<<endl;
while(cin>>ch&&ch!='#')
{
p=newLnode[sizeof(Lnode)];
if(!p)
{
cout<<"獲取內存失敗"<<endl;
exit(ERROR);
}
p->data=ch;//尾插法
L->next=p;
L=p;
}
L->next=q;
}
voidoutput_List(LinkListL)//遍歷單鏈表(輸出單鏈表元素)
{
LinkListp;
p=L->next;
if(p==L)
{
cout<<"該鏈表是空鏈表!"<<endl;
exit(ERROR);
}
while(p!=L)
{
cout<<p->data<<"";
p=p->next;
}
}
Statusmain()
{
LinkListH;
H=(LinkList)malloc(sizeof(Lnode));
H->next=NULL;//設置頭結點為空
Creat_List(H);
output_List(H);
return0;
}//頭結點有和沒有都是可以的,頭結點只是為了讓操作鏈表更方便,

6. 如何用C語言編寫一個鏈表

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"

struct Node
{
int data;//數據域
struct Node * next;//指針域
};

/*************************************************************************************
*函數名稱:Create
*函數功能:創建鏈表.
*輸入:各節點的data
*返回值:指針head
*************************************************************************************/
struct Node * Create()
{
struct Node *head,*p1,*p2;
head = NULL;
p1 = p2 = (struct Node *)malloc(sizeof(struct Node));
printf("Input the linklist (Input 0 to stop):\n");
scanf("%d",&p1->data);
while(p1->data!=0)
{
if(head == NULL){
head = p1;
}else{
p2->next = p1;
p2 =p1;
}
p1 = (struct Node *)malloc(sizeof(struct Node));
scanf("%d",&p1->data);
}
p2->next = NULL;
return head;
}
/*************************************************************************************
*函數名稱:insert
*函數功能:在鏈表中插入元素.
*輸入:head 鏈表頭指針,p新元素插入位置,x 新元素中的數據域內容
*返回值:無
*************************************************************************************/
void insert(struct Node * head,int p,int x)
{
struct Node * tmp = head;
struct Node * tmp2 ;
int i ;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return ;
if(i<p-1)
tmp = tmp->next;
}
tmp2 = (struct Node *)malloc(sizeof(struct Node));
tmp2->data = x;
tmp2->next = tmp->next;
tmp->next = tmp2;
}
/**************************************************************************************
*函數名稱:del
*函數功能:刪除鏈表中的元素
*輸入:head 鏈表頭指針,p 被刪除元素位置
*返回值:被刪除元素中的數據域.如果刪除失敗返回-1
**************************************************************************************/
int del(struct Node * head,int p)
{
struct Node * tmp = head;
int ret , i;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return -1;
if(i<p-1)
tmp = tmp->next;
}
ret = tmp->next->data;
tmp->next = tmp->next->next;
return ret;
}
/**************************************************************************************
*函數名稱:print
*函數功能:列印鏈表中的元素
*輸入:head 鏈表頭指針
*返回值:無
**************************************************************************************/
void print(struct Node *head)
{
struct Node *tmp;
for(tmp = head; tmp!=NULL; tmp = tmp->next)
printf("%d ",tmp->data);
printf("\n");
}
/**************************************************************************************
*函數名稱:main
*函數功能:主函數創建鏈表並列印鏈表。
**************************************************************************************/
int main(){
struct Node * head = Create();
print(head);
return 0;
}

7. C語言建立循環鏈表

#include <stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct list
{
int num;
struct list *next;
}List;
int n=0;
List *creat()
{
List *head,*p1,*p2;
int i;
if((head=(List *)malloc(sizeof(List)))==NULL)
{
printf("Error");
exit(0);
}
p1=p2=head;
printf("輸入創建鏈表的長度:");
scanf("%d",&head->num);//創建列表,帶頭結點,頭結點數據域表示輸入的個數
if(head->num==0)
{
head->next=NULL;
printf("已創建帶頭結點的空鏈表");
}
else
{
printf("輸入數據:\n");
for(i=0;i<head->num;i++)
{
if((p1=(List *)malloc(sizeof(List)))==NULL)
{
printf("Error");
exit(0);
}
scanf("%d",&p1->num);
p2->next=p1;
p2=p1;
}
p1->next=head;
}
return(head);
}
void print(List *head)
{
List *p;
p=head->next;
for(;p!=head;)
{
printf("%d ",p->num);
p=p->next;
}
printf("\n");
}
void main()
{
List *head;
head=creat();
print(head);
}

8. 用C語言實現鏈表的演算法

這個是我們數據結構上機實驗的鏈表問題,
#include<stdio.h>
#include<malloc.h>
#define
LEN
sizeof(LinkNode)
typedef
int
Datatype;
typedef
int
Status;
typedef
struct
LinkNode{
Datatype
data;
struct
LinkNode
*next;
}
LinkNode,*LinkList;
typedef
struct
OrderedList
{
LinkNode
*head,*tail;
int
Listsize;
}
OrderedList;//有序循環鏈表的頭節點head,尾接接節點
tail及長度Listsize
Status
InitList(OrderedList
*List)//生成循環鏈表頭節點
{
List->tail=List->head=(LinkList)malloc(LEN);
if(List->head==NULL)
return
0;
else
{
List->head->next=List->tail;
List->tail->next=List->head;
List->Listsize=0;
return
1;
}
}
void
OrderedInsert(OrderedList
*List,Datatype
data)//每調用一次有序插入data形成有序的(從小到大)的鏈表
{
LinkNode
*p
,*q;
if(List->head==List->tail->next)
{
p=(LinkNode*)malloc(LEN);
p->data
=
data;
List->head->next=p;
p->next=List->tail;
List->Listsize++;
}
else
{
p=List->head->next;
q
=
List->head;
while(p->data<data&&p!=List->tail)
{
q
=
p;
p=p->next;
}
if(p->data==data)
{printf("YOu
have
input
the
same
datas
%d\n\t
YOu
should
input
another
data
\n",data);
scanf("%d",&data);
OrderedInsert(List,data);
}
else
{
p=(LinkNode*)malloc(LEN);
p->data
=
data;
p->next
=
q->next;
q->next
=
p;
List->Listsize++;
}
}
}
void
Creatset(OrderedList
*List)//多次調用OrderedInsert()生成有序鏈表即集合List
{
Datatype
data;
int
setsize
,
i=0;
printf("Please
input
the
setsize
you
want
to
creat:\n");
scanf("%d",&setsize);
InitList(List);
if(setsize==0)
printf("You
needen't
input
any
data\n");
else
if(setsize==1)
printf("Please
input
a
single
data\n");
else
printf("Please
input
%d
different
datas;\n",setsize);
while(i<setsize||setsize>List->Listsize)//當循環次數i小於setsize或者集合內實際元素數List.Listsize小於setsize時一直循環下去
{
scanf("%d",&data);
OrderedInsert(List,data);
i++;
}
}
void
Append(OrderedList
*List,Datatype
data)//在循環鏈表的最後面追加
一個data
{
LinkNode
*p;
p=(LinkNode*)malloc(LEN);
p->data=data;
List->tail=List->tail->next=p;
List->tail->next=List->head;
List->Listsize+=1;
}
void
MergeList(OrderedList
La,OrderedList
Lb,OrderedList
*Lc)//有序循環鏈表ListLa,ListLb求並集生成ListLc
{
LinkList
Pa,Pb;
Pa=La.head->next;Pb=Lb.head->next;
while(Pa!=La.tail&&Pb!=Lb.tail)
{
if(Pa->data<=Pb->data)
{
Append(Lc,Pa->data);
Pa=Pa->next;
}
else
{
Append(Lc,Pb->data);Pb=Pb->next;
}
}
while(Pa!=La.tail)
{
Append(
Lc,Pa->data);Pa=Pa->next;}
while(Pb!=Lb.tail)
{
Append(Lc,Pb->data);Pb=Pb->next;}
}
void
Print(OrderedList
List)
{
LinkNode
*p;
p=List.head->next;
if(p->next==List.head)
printf("No
Elem\n");
while(p!=List.head)
{
printf("%5d",p->data);p=p->next;
}
printf("\n");
}
void
main()
{
OrderedList
ListLa,ListLb,ListLc;
Creatset(&ListLa);
Creatset(&ListLb);
InitList(&ListLc);
MergeList(ListLa,ListLb,&ListLc);
printf("The
orgnial
list
ListLa,ListLb:\n");
Print(ListLa);
Print(ListLb);
printf("The
Merge
list
ListLc;\n");
Print(ListLc);
}

9. 循環鏈表怎麼建立。c語言

你好,循環鏈表就是普通鏈表的最後一個節點的Next指針指向第一個節點就好啦