码迷,mamicode.com
首页 > 其他好文 > 详细

第十二章 使用结构指针

时间:2018-02-02 14:29:30      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:blog   一个   put   pre   name   类型   返回   you   集合   

这张这章没有认真读,涉及到数据结构。

1.链表是一些包含数据的独立数据结构(通常称为节点)的集合。每个节点之间通过链或指针连接在一起。

 

2.Node:通常在数据结构中用作节点的类型名。

struct Node{

...

}

之后可以

Node x;相当于struct Node x;

 

 

 1 void main ()//主函数
 2 { 
 3  struct LNode *head;
 4  head = creat();//调用creat函数,创建一个单链表,并将单链表的头指针地址存放在指针 head 中
 5 }
 6 struct LNode *creat()//定义指针函数 creat(地址)及创建一个单链表 ,其返回值是一个单链表的首地址
 7 {
 8  struct LNode *head,*p,*rear;//定义指针变量
 9  int x;
10  char a;
11  head=(struct LNode *)malloc(sizeof(struct LNode));/*申请 struct list 类型字节长的存储空间,第一个括号中内
12  容表示将申请空间的首地址经过强制类型转换为struct list *型
13  并把转换后的首地址存储在指针head中;*/
14  rear=head;//用一个需要移动的指针 rear 来存放头结点的地址
15  puts("input name of the list :\n");
16  scanf("%c",&a);//输入的链表名称只能用一个字母表示 ;若用两个则应为 : scanf("%c%c",&a,&b);
17  puts("input the list end with ‘-1‘:\n");//输入单链表的元素以 -1 结束 
18  scanf("%d",&x);
19  while(x+1)//输入的数据为非零数 判断为真,当等于 -1 时为假,则跳出循环
20  {
21  p=(struct LNode *)malloc(sizeof(struct LNode));//同上
22  p->data=x;//将输入的值取出一个值并存储在结点 p 的数据域中
23  rear->next=p;//将结点 p 的首地址存放在 p 的前驱结点的指针域中
24  rear=p;//rear指针后移到下一个结点首地址处
25  scanf("%d",&x);//继续从输入的值中取出另一个值
26  }
27  rear->next=NULL;//指针 rear 移至最后一个结点处,将此结点的指针域 制空(NULL)
28  puts("the list you input is :");
29  print(head->next,a);//调用 print 函数
30  return head->next;//函数结束时返回创建的单链表的头指针(此头指针指向的结点为输入的第一个数值所存放的结点)
31 }
32 void print (struct LNode *head,char a)/*定义一个 print 函数,及打印函数,将单链表head中元素输出,
33  a为要输出单链表的名称*/
34 {
35  struct LNode *p;//定义一个需要移动的指针 p
36  p=head;//将单链表的首地址赋予指针 p
37  printf("%c = ( ",a);
38  while( p )//p存放的是地址,当 p 指向链表最后一个结点时 p=p->next(=NULL)为空,while判断为假,跳出循环
39  {
40  printf("%d ",p->data);//输出 p 指针指向的结点的数据域中存放的数值
41  p=p->next;//p指针后移,指向下一个结点首地址
42  }
43  puts(")");
44 }

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct LNode
 4 {
 5  int data;
 6  struct LNode *next;
 7 };
 8 struct LNode *creat();
 9 void print (struct LNode *head,char a);
10 void main ()
11 { 
12  struct LNode *head;
13  head = creat();
14 }
15 struct LNode *creat()
16 {
17  struct LNode *head,*p,*rear;
18  int x;
19  char a;
20  head=(struct LNode *)malloc(sizeof(struct LNode));
21  rear=head;
22  puts("input name of the list :\n");
23  scanf("%c",&a);
24  puts("input the list end with ‘-1‘:\n");
25  scanf("%d",&x);
26  while(x+1)
27  {
28  p=(struct LNode *)malloc(sizeof(struct LNode));
29  p->data=x;
30  rear->next=p;
31  rear=p;
32  scanf("%d",&x);
33  }
34  rear->next=NULL;
35  puts("the list you input is :");
36  print(head->next,a);
37  return head->next;
38 }
39 void print (struct LNode *head,char a)
40 {
41  struct LNode *p;
42  p=head;
43  printf("%c = ( ",a);
44  while( p )
45  {
46  printf("%d ",p->data);
47  p=p->next;
48  }
49  puts(")");
50 }

 

待续... ...

第十二章 使用结构指针

标签:blog   一个   put   pre   name   类型   返回   you   集合   

原文地址:https://www.cnblogs.com/Mayfly-nymph/p/8404261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!