用C语言编程实现单链表的基本操作
第二个显示为什么?为什么什么东西都没有、
#includestdio.h
typedef
struct
sample
{
char
ch;
struct
sample
*next;
}LNode;
LNode
*Createlist(LNode
*head)
//创建单链表,头插入法
{
LNode
*p;
if((head=(LNode
*)malloc(sizeof(LNode)))==NULL)
printf(“aply
error\n”);
head-next=NULL;
head-ch
=
‘\0’;
int
i=
0;
printf(“请一次输入A-Z:\n”);
for(i
=
;
i
26;
++i)
{
p=(LNode
*)malloc(sizeof(LNode));
if(!p)
printf(“aply
error\n”);
scanf(“%c”,p-ch);
p-next
=
head-next;
head-next=p;
}
return
head;
}
LNode
*EncryptList(LNode*
head)
{
LNode
*p=
head,*q
=
head-next,*r
=
head-next;
while(p-next)
{
p
=
p-next;
}
int
i
=
;
for(i
=
;
i
3;
i++)
{
p-next
=
r;
p
=
p-next;
q
=
q-next;
r
=
q;
}
p-next
=NULL;
head-next
=
q;
return
head;
}
void
ListPrint(LNode
*head)
{
LNode
*p
=
head-next;
while(p-next!=NULL)
{
printf(“%c\t”,p-ch);
p=p-next;
}
printf(“%c\n”,p-ch);
}
int
main(void)
{
LNode
*head;
head
=
Createlist(head);//链表初始化
ListPrint(head);
//打印单链表数据
head
=
EncryptList(head);
ListPrint(head);
return
0;
}
看看。
如何利用c语言实现集合的运算
这种集合问题,一般是使用链表来处理。
比如说有两个链表list1,list2,它们分别用来存储集合A,B
如果要对A,B进行并运算,
那么可以首先定义一个新的链表list3,然后让list1和list2的值进行循环的比较,如果有相同的值,那么就将这个值放入list3中,直到没有相同的值为止。
大致的算法就是这样了。
c foreach怎么实现list的嵌套循环
c:forEach items=”${rows }” var=”item”
c:forEach items=”${item.columns}” var=”it”
${it.属性 }
/c:forEach
/c:forEach
这样应该就行
C语言中creatlist的用法
1、createlist不是库函数,一般是数据结构中实现新建一个链表的自定义函数。因此没有什么用法好说的,关键是看自己怎么定义。
2、例程:
NODE *creatlist(int a[])
{ NODE *h,*p,*q;int i;
h=(NODE *)malloc(sizeof(NODE));
h-next=NULL;
for(i=0;iN;i++)
{q=(NODE *)malloc(sizeof(NODE));
q-data=a[i];
q-next=NULL;
if(h-next==NULL) h-next=p=q;
else {p-next=q;p=q;} }
return h;
}
C语言实现一个单向链表linked List
#include stdio.h
#include stdlib.h
#include time.h
struct node
{
int data;
struct node* next;
};
int main()
{
struct node *h=NULL, *p;
int i;
int s1, s2, s3;
srand(time(NULL));
for(i = 0 ;i 5; i ++)
{
if(h==NULL)
h=p=(struct node *)malloc(sizeof(struct node));
else
p=p-next=(struct node *)malloc(sizeof(struct node));
p-data=rand()%10+1;
}
p-next=NULL;
s1=s2=s3=0;
for(p=h; p; p=p-next)
{
if(p-data%2==1) s1+=p-data;
else s2+=p-data;
s3+=p-data;
}
printf(“all=%d odd=%d even=%d\n”, s3, s1, s2);
while(h)
{
p=h-next;
free(h);
h=p;
}
return 0;
}