当前位置:首页 » 编程语言 » 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;
}
}
}