单循环链表表是怎么建立的

单循环链表 看我写的建立链表的代码怎么错了,或者怎么建立。那个链表结束的条件的怎么去确定。o(╯□╰)o_百度知道
单循环链表 看我写的建立链表的代码怎么错了,或者怎么建立。那个链表结束的条件的怎么去确定。o(╯□╰)o
struct node *next=NULL;
scanf(&quot,(*q);
NODE* creat()
NODE* head,*p;){
q=malloc(sizeof(NODE));%d&
head=p=malloc(sizeof(NODE));n&
p-&%d&输入结点的数值&
while(scanf(&)!=&.data);next=NULLtypedef struct node{
提问者采纳
输入结点的数值;%d
while(p;data)),&(q-&
creat(){ slnode
*next=q;next=NULL;#include &next=NULL;/
void main() {
&#47:&#92,(p-&malloc.h&
p=p-& } n& head=(( slnode*) malloc( sizeof(slnode)));
q-&data));输入0代表终止typedef struct node{
struct node *}slnode!=NULL)
printf(&;;
head=creat(); p-&&#47,*p,*q;if(&quot!=NULL)printf(&
p=p-& p= printf(&1;);%d&
while(true) {
q=( node*)malloc(sizeof(struct node)),*p;);
p=head-&gt#include &lt.h&stdio
谢谢了。看到了你写的,我感觉我很多东西都想起来了。不过你代码q=( node*)malloc(sizeof(struct node));结点类型标记错了,我突然明白了,编译器一直提示我的类型转换错误是怎么一回事了。用malloc 开辟的空间,返回的都是一个void * 的指针吧。
提问者评价
thanks a lot.
其他类似问题
按默认排序
其他2条回答
)这句必然导致死循环第二;%d&\n&quot, while(scanf(&%d&n&&#92, while(scanf(&quot!=&quot!=&);)第一;)这一句你到底想要表达什么意思
为什么呢?我学数据结构从来没有实际编过,这里我就是想既然构造一个链表,那么怎么去设定这个链表的结束条件呢?这里我想,如果输入一系列的数,如果输入回车后。输入结束,那么链表创建结束?可是怎么导致死循环了,我不明白,望指教。
while(scanf(&%c&)!='\n')
链表的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁判断一个链表是不是循环链表 - 拾贝壳的小男孩 - 博客园
算法思想:在建立循环链表时设置两个指针,头指针和尾指针,head和tail,使tail-&next=head,即让尾节点指向头节点,建立的时候采用尾插入法,每次让新节点先指向尾节点的下一个节点,
然后让头节点的下一个节点指向新节点,然后让新节点赋值给头节点和尾节点。
&&&&&&&&&&&&& 判断是否是循环链表时,也设置两个指针,慢指针和快指针,让快指针比慢指针每次移动快两次。如果快指针追赶上慢指针,则为循环链表,否则不是循环链表,如果快指针或者慢指针指向NULL,则不是循环链表。
include &iostream&
#include &cstdlib&
typedef struct list
struct list *
}loopNode,*lpN
void createList(lpNode &list,int arr[],int n)
//创建循环链表
lpNode head=NULL,tail=NULL;
head=tail=
tail-&next=
for(int i=0;i&n;i++)
node=(struct list*)malloc(sizeof(struct list));
node-&data=arr[i];
node-&next=tail-&
head-&next=
cout&&"create success"&&
int judgeIsLoop(lpNode list)
//判断是否是循环链表
if(list==NULL)
lpNode slow,
fast=list-&next-&
while(slow)
if(fast==NULL||fast-&next==NULL)
else if(fast==slow||fast-&next==slow)
slow=slow-&
fast=fast-&next-&
void display(lpNode list)
//输出循环链表
lpNode head,
head=tail=
while(head-&next!=tail)
cout&&head-&data&&"--";
head=head-&
cout&&head-&
int main()
lpNode list=NULL;
int arr[10]={1,2,3,4,5,6,7,8,9,0};
int flag=0;
list=(struct list*)malloc(sizeof(struct list));
list-&data=11;
list-&next=NULL;
createList(list,arr,10);
flag= judgeIsLoop(list);
if(flag==1)
//如果是循环链表,就输出循环链表
cout&&"the list is loop list."&&
cout&&"output the list:"&&
display(list);
cout&&"the list is not loop list."&&
&运行截图:
随笔 - 253数据结构中,怎样建一个循环链表,要代码_百度知道
数据结构中,怎样建一个循环链表,要代码
我有更好的答案
} /}void insert_node(struct dllist *next) {
printf(& for(lnode =#include &lt.Lock()!= NULL; struct dllist *next == NULL)
tail = lnode-&gt, lnode-& else
lnode-& i &/ lnode = lnode-&}.Unlock();
&#47, ** add some numbers to the double linked list *&#47, struct dllist *after) { lnode-& for(i = 0;next = NULL; } /}void append_node(struct dllist *lnode) { if(head == NULL) {
head =n&prev == NULL)
head = lnode-&next = if(lnode-&#include &number);
lnode-&prev =prev =struct dllist *prev-&gt!= NULL)
remove_node(head);number = i,进行数据处理
head = tail = NULL;void remove_node(struct dllist *lnode); lnode-&CCriticalSnext-& else
lnode-&gt, struct dllist *after);}void remove_node(struct dllist *lnode) { if(lnode-& struct dllist *prev = if(after-&gt!= NULL)
after-&临界区锁定; int i = 0;next.h&int main(void) {struct dllist *void insert_node(struct dllist */prev = NULL; lnode-& } else {
tail-& after-&gt.h&void initial_node(struct dllist *lnode);
lnode-&}void initial_node(struct dllist *lnode) { prev = lnode-&void append_node(struct dllist *lnode);; i++) {
lnode = (struct dllist *)malloc(sizeof(struct dllist));%d\next = after-&* destroy the dll list */next = lnode-&* print the dll list */next = / else
tail == 5;next-& } tail =
append_node(lnode);next.h&
lnode-& return 0;/ struct dllist {stdio#include & while(head
其他类似问题
您可能关注的推广
循环链表的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁单循环链表-创建、插入、删除、反转等操作 - hubi0952的专栏
- 博客频道 - CSDN.NET
2954人阅读
#ifndef _List_H
#define _List_H
#include &stdio.h&
#include &stdlib.h&
#define NotFound NULL;
typedef struct List
struct Node *
typedef struct List *pN
typedef pNode pL
typedef pNode P
pList creatList(int *array, int len);
Position findNode(pList head, int x);
//pList lastInsertList(pList head, int insertvalue);
pList InsertList(pList head, int val, int insertvalue);
pList deleteNode(pList head, int delValue);
pList reversalList(pList head);
pList freeList(pList head);
void printList(pList head);
/* _List_H */
#include &singleList.h&
pList creatList(int *array, int len)
pList head,ptr,
head = (pList)malloc(sizeof(Node));
printf(&分配内存失败!&);
return NULL;
head-&next = NULL;
head-&value = array[0];
for(i = 1; i & i++)
ptr = (pList)malloc(sizeof(Node));
printf(&分配内存失败!&);
return NULL;
ptr-&value = array[i];
ptr-&next = NULL;
tmp-&next =
tmp = tmp-&
ptr-&next =//单循环增加
Position findNode(pList head, int x)
pList ptr =
while(ptr)
if(ptr-&value == x)
ptr = ptr-&
if(ptr == head)//单循环增加
return NotF
//在值为val的节点前插入
pList InsertList(pList head, int val, int insertvalue)
pList temp =
pList ptr =
while(ptr)
if (ptr-&value == val)
while(temp-&next != ptr)
temp = temp-&
tmp = (pList)malloc(sizeof(Node));
tmp-&next =
tmp-&value =
temp-&next =
ptr = ptr-&
if (ptr == head)
printf(&no such value can be insert!\n&);
return NotF
pList deleteNode(pList head, int delValue)
pList ptr,
ptr = temp =
while(ptr)
if (ptr-&value == delValue)//是“==”注意
while(temp-&next != ptr)
temp = temp-&//找到ptr的上一个节点temp
temp-&next = ptr-&
free(ptr);
if (head == ptr)//说明删除的是头节点
return temp-&
//当删除头节点时就不行
ptr = ptr-&
if (ptr == head)//说明循环了一周
printf(&the delValue had not found!\n&);
return NotF
pList reversalList(pList head)
pList ptr,
temp = head-&
while(temp-&next)
temp = temp-&
if(temp-&next == head)//单循环增加
temp-&next =
head-&next =//NULL改成temp
pList freeList(pList head)
pList p =//单循环新增
while(head)
head = head-&
free(ptr);
if(head == p)//单循环新增
head = NULL;
//注意内存分配问题
void printList(pList head)
pList ptr =
printf(&the list is empty!\n&);
printf(&the list is : &);
while(ptr)
printf(&%d &,ptr-&value);
ptr = ptr-&
if(ptr == head)//单循环增加
printf(&\n&);
#include &singleList.h&
int main(void)
int array[] = {1,2,3,4,5,6,7,8,9,10};
int len = sizeof(array)/sizeof(*array);
int insertvalue = 100;
pList head = creatList(array,len);
printList(head-&next);
head = reversalList(head-&next);
printList(head);
p = findNode(head,5);
printf(&the position value is : %d\n&,p-&value);
//在元素为10前面插入100
head = InsertList(head,10,insertvalue);
printList(head);
//删除10000的节点
head = deleteNode(head,10);
printList(head);
//删除头节点
head = deleteNode(head,1);
printList(head);
//释放链表
head = freeList(head);
printList(head);
与单链表相比,在插入和删除操作都简化了。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:149816次
积分:2453
积分:2453
排名:第6993名
原创:61篇
转载:117篇
评论:22条
(1)(1)(1)(1)(3)(4)(12)(10)(13)(1)(14)(27)(33)(15)(11)(5)(2)(1)(4)(18)(1)(1)建立非循环单链表的过程_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
喜欢此文档的还喜欢
建立非循环单链表的过程
一​步​步​教​你​如​何​建​立​一​个​链​表​。​非​常​详​细​。​有​图​示​。​非​常​适​合​初​学​者
阅读已结束,如果下载本文需要使用
想免费下载本文?
把文档贴到Blog、BBS或个人站等:
普通尺寸(450*500pix)
较大尺寸(630*500pix)
你可能喜欢}

我要回帖

更多关于 单循环链表 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信