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

c語言循環鏈建設

發布時間: 2022-06-06 01:39:21

c語言循環單鏈表的創建.有句程序不明白高人幫忙指導一下,十分感謝

if(head->next==head){ /*能否把if的的功能詳細的解讀一下*/
這句很簡單啊,就是判斷head->next是否等於head,因為你在程序最初定義了head->next=head; ,所以程序第一次循環的時候會判斷為真。然後head->next=p;所以head->next不再等於head。

後邊的語句是控制循環的,為了防止無限循環,保證所有節點只被遍歷一次。

PS:我不知道你的程序是否能夠完美運行,我只是從語句猜測你的意思。

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

<p>建立單向循環鏈表的代碼:</p>
#include <stdio.h>
#include <stdlib.h>
typedef struct _A{

int data;

struct _A *next;
}A;
typedef A* IA;
void createDoubleLink(IA *header) {

int data;

IA p;

printf("input data end with -1:");

scanf("%d", &data);

while(data != -1) {

p = (IA)malloc(sizeof(A));

p->data = data;

if(*header == NULL) {

*header = p;

(*header)->next = *header;

}

else{

IA q = *header;

while(q->next != *header) {

q = q->next;

}

q->next = p;

q->next->next = *header;

}

scanf("%d", &data);

}
}
void print(IA header) {

IA p = header;

if(header == NULL) return;

while(1){

printf("%d\t", p->data);

if(p->next == header) break;

p = p->next;

}

printf("\n");
}
int main()
{

IA header = NULL;

createDoubleLink(&header);

print(header);

return 0;
}<p> 頭結點的意思是鏈表的第一個節點,但這個節點不保存數據。
</p>

❸ 如何創建c語言的雙向循環鏈表

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

int n;

typedef struct node
{
int data;
struct node *prior;
struct node *next;
}node,*DoubleLinkList;

void Creat_List(DoubleLinkList L)
{
DoubleLinkList r,s;
cout<<"請輸入n的值"<<endl;
cin>>n;
s = L;
s->prior = s;
s->data = n;
for(int i=0;i<n;i++)
{
r = (DoubleLinkList)malloc(sizeof(node));
L->next = r;
r->next = s->next;
r->prior = L;
s->next->prior = r;
r->data = i;
L = r;
}
}

void Desplay_List(DoubleLinkList L)
{
DoubleLinkList p;
p = L->next;
for(int i=0;i<n;i++)
{
cout<<"輸出:"<<p->data<<endl;
p = p->next;
}
}

void Destroy_List(DoubleLinkList L)
{
DoubleLinkList p;
p = L;
node *temp;

for(int k=0;k<n;k++)
{
p = L->next;
temp = p->next;
temp->prior->next = temp->next;
temp->next->prior = temp->prior;
free(temp);
}
cout<<"空間釋放成功"<<endl;
}

void main()
{
node a;
DoubleLinkList p;
p = &a;
Creat_List(p);
Desplay_List(p);
Destroy_List(p);
}
由於我也不熟悉,我自己保存的創建例子,希望對你有幫助~

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

❺ 如何創建一個空的c語言雙向循環鏈表

只是雙向給你參考... 加個循環對你應該問題不大吧...

❻ c語言建立鏈表的問題,請講循環語句中的幾句詳細解釋下。

while(x!
=
-1) {
//
只要數據值不是-1就繼續。

s
=
(struct
node
*)malloc(sizeof(struct
node));
//為結構數據s申請存儲空間。

s->data
=
x;
//存入數據。

r->next
=
s;
//前一個鏈表的指針指向本結構。

r
=
s;
//
作為臨時的s正式加入鏈錶行列。

scanf("%d",&x);//讀入新數據。
}
r->next=NULL;//
鏈表的最後成員的指針指向NULL,表明鏈表的末尾。

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

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

❽ 用C語言編寫一個程序,建立雙向循環鏈表,並實現它的插入操作、刪除操作

望笑納~
//CreateList_L.cpp
//To create a LinkList
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2

typedef struct DuLNode
{ int data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode,*DuLinkList;
// 初始條件:L已存在。操作結果:返回L中數據元素個數
int ListLength(DuLinkList L)
{
int i=0;
DuLinkList p=L->next; // p指向第一個結點
while(p!=L) // p沒到表頭
{
i++;
p=p->next;
}
return i;
}

// 由雙鏈循環線性表L的頭結點出發,正序輸出每個數據元素
void ListTraverse(DuLinkList L)
{
DuLinkList p=L->next;
while(p!=L)
{
cout<<p->data<<"\t";
p=p->next;
}
cout<<endl;
}
// 由雙鏈循環線性表L的頭結點出發,逆序輸出每個數據元素
void ListTraverseBack(DuLinkList L)
{
DuLinkList p=L->prior;
while(p!=L)
{
cout<<p->data<<"\t";
p=p->prior;
}
cout<<endl;
}

//To Creatre a DuLinkList L with HeadNode
void CreateList_DuL(DuLinkList &L)
{
int n;
int i;
DuLNode *p;
L=(DuLinkList)malloc(sizeof(DuLNode));
L->next=L->prior=L;
cout<<"CreateList_L"<<endl<<"================"<<endl;
cout<<"Please input the Init DuLinkNode Number: <eg. 5> ";
cin>>n;

cout<<"Please input the data for DuLinkList Nodes: <eg. 34,67,3,-9,45,...>"<<endl;
for(i=n;i>0;--i)
{
p=(DuLinkList)malloc(sizeof(DuLNode));
cin>>p->data; //Reverse order inputing for Creating a LinkList
p->prior=L;
p->next=L->next;
L->next->prior=p;
L->next=p;
}
if(n)
{
cout<<endl;
cout<<"Success to Create a DuLinkList !"<<endl;
ListTraverse(L);
cout<<endl;
}
else cout<<"A NULL DuLinkList have been created !"<<endl;
}

//ListInsert_Dul()
int ListInsert_DuL(DuLinkList &L)
{
DuLNode *p=L,*s;
int j=0;
int i;
int e;
cout<<"======"<<"before insert:"<<"======"<<endl;
ListTraverse(L);
cout<<"input the location you want to insert:";
cin>>i;
while (i<1||i>ListLength(L)+1)
{
//i值不合法
cout<<"illeagle location,please input the correct location:";
cin>>i;
}

cout<<"input the number you want to insert:";
cin>>e;
while(j<i)
{ p=p->next;
++j;
}
if(!(s=(DuLinkList)malloc(sizeof(DuLNode))))
{
cout<<endl<<"Allocate space failure ! " ;
return (ERROR);
}
s->data=e;
s->prior=p->prior;
s->next=p;
if(i==1)
L->next=s;
p->prior->next=s;
p->prior=s;
cout<<"has insert:"<<e<<endl;
ListTraverse(L);
cout<<"======"<<"after insert:"<<"======"<<endl<<endl;
return (OK);
}

// 刪除帶頭結點的雙鏈循環線性表L的第i個元素,i的合法值為1≤i≤表長
int ListDelete(DuLinkList L)
{
DuLinkList p;
int j=1; // j為計數器
int e;
int i;

cout<<"input the location of the number you want to delete"<<endl;
cin>>i;
while(i<0||i>ListLength(L))
{
//i值不合法
cout<<"illeagle location,please input the correct location:";
cin>>i;
}

p=L->next; // p指向第一個結點
while(p!=L&&j<i) // 順指針向後查找,直到p指向第i個元素或p指向頭結點
{
p=p->next;
j++;
}
if(p==L||j>i) // 第i個元素不存在
{
cout<<"第i個元素不存在"<<endl;
return ERROR;
}
else
{
cout<<"======"<<"before delete:"<<"======"<<endl;
ListTraverse(L);
e=p->data; // 取第i個元素
if(!p) // p=NULL,即第i個元素不存在
return ERROR;
e=p->data;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
cout<<"has delete:"<<e<<endl;
ListTraverse(L);
cout<<"======"<<"after delete:"<<"======"<<endl<<endl;
return OK;
}

}
void menu(int c)
{
cout<<"================================================="<<endl;
cout<<"\t\t1----建立"<<endl;
cout<<"\t\t2----插入"<<endl;
cout<<"\t\t3----刪除"<<endl;
cout<<"\t\t4----逆置"<<endl;
//cout<<"\t\t5----菜單"<<endl;
cout<<"\t\t0----退出"<<endl;
cout<<"================================================="<<endl;
cout <<endl<<"input you choice number:";
}
void main()
{
DuLinkList L;
int c=1;

while(c!=0)
{
switch(c)
{
case 0:
break;
case 1:
CreateList_DuL(L);
break;
case 2:
ListInsert_DuL(L);
break;
case 3:
ListDelete(L);
break;
case 4:
ListTraverseBack(L);
break;
default:
cout<<"infeasible choice,input again:"<<endl;
menu(c);
cin>>c;
}
if(c!=0)
{
menu(c);
cin>>c;
}
}
}