【数据结构】typedef struct 和 struct在链表中的应用

前端之家收集整理的这篇文章主要介绍了【数据结构】typedef struct 和 struct在链表中的应用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. (1) struct{ int x; int y; }test1;
  2. 定义了 结构 test1
  3. test1.x test1.y 可以在语句里用了。
  4.  
  5. (2) struct test {int x; int y; }test1;
  6. 定义了 结构 test1
  7. test1.x test1.y 可以在语句里用了。
  8. 1 比,1里面省写 test
  9.  
  10. (3)
  11. typedef struct test
  12. {int x; int y; }text1,text2;
  13. 只说了 这种结构 的(类型)别名 text1 或叫 text2
  14.  
  15. 真正在语句里用,还要写:
  16. text1 hwh;
  17. 然后好用 hwh.x,hwh.y
  18.  
  19. 或写 text2 hwh1;
  20. 然后好用 hwh.x,hwh.y
  21.  
  22. 4typedef struct {int x; int y; }test1;
  23. 3)一样,真正在语句里用,还要 写:
  24. test1 hwh;
  25. 才能用 hwh.x hwh.y
  1.  
  1. (5)超高端用法
  1. typedef struct list *list_p;//首先定义了一个结构体list,在这里要把struct list看成是一体了,继续分析
  1. //在这里,list_p是一个结构体指针,struct list *就等同于list_p,这样做是可以在下面互相替换
  1. //在定义结构list之前,定义了一个指向该结构的指针list_p,C语言允许定义指向不存在的类型的指针。
  1. typedef struct list{
  1. char a[3];
  1. list_p link;//结构体自引用,这句话等同于struct list * link;。link是一个指向结构体的的指针
  1. };
  1. //在定义了结构list之后,就可以建立一个新表,用list_p ptr = Null;完成。
  1. list_p ptr = Null;//存在一个名为ptr的新表ptr包含表的起始地址
  1. ptr = (list_p) malloc(sizeof(list)); //
  1. //e->name 等同与(*e).name//e是指针
  1. strcpy(ptr->a,"bat");
  1. ptr->link = NULL;
  1. -----
  1. 完整代码
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. main()
  5. {
  6. 	typedef struct list_node *list_pointer;
  7. 	typedef struct list_node
  8. 	{
  9. 		char data[4];
  10. 		list_pointer link;
  11. 	};
  12. 	list_pointer ptr = NULL;
  13. 	ptr = (list_pointer)malloc(sizeof(list_node));
  14. 	strcpy(ptr->data,"bat");
  15. 	ptr->link = NULL;
  16. 	printf("%s\n",ptr->data);
  17. }
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define IS_Empty(ptr) (!(ptr))
  4. #define IS_FULL(ptr) (!(ptr))
  5. typedef struct list_node *list_pointer;
  6. typedef struct list_node
  7. {
  8. 	int data;
  9. 	list_pointer link;
  10. };
  11. list_pointer ptr = NULL;
  12. list_pointer create()
  13. {
  14. 	list_pointer first = NULL,second = NULL;
  15. 	first = (list_pointer)malloc(sizeof(list_node));
  16. 	second = (list_pointer)malloc(sizeof(list_node));
  17. 	first->data = 10;
  18. 	first->link = second;
  19. 	second->data = 20;
  20. 	second->link = NULL;
  21. 	return first;
  22. }
  23. void insert(list_pointer *ptr,list_pointer node)
  24. {
  25. 	list_pointer temp;
  26. 	temp = (list_pointer) malloc (sizeof(list_node));
  27. 	if(IS_FULL(temp))
  28. 	{
  29. 		fprintf(stderr,"The memory is full.\n");
  30. 		exit(1);
  31. 	}
  32. 	temp->data = 50;
  33. 	if(*ptr)
  34. 	{
  35. 		temp->link = node->link;
  36. 		node->link =temp;
  37. 	}
  38. 	else
  39. 	{
  40. 		temp->link =NULL;
  41. 		*ptr = temp;
  42. 	}
  43. }
  44. void delete_node(list_pointer *ptr,list_pointer trail,list_pointer node)
  45. {
  46. 	if(trail)
  47. 	{
  48. 		trail->link = node->link;
  49. 	}
  50. 	else
  51. 	{
  52. 		*ptr = (*ptr)->link;
  53. 	}
  54. 	free(node);
  55. }
  56. void print_list(list_pointer ptr)
  57. {
  58. 	printf("Thw list contains:\n");
  59. 	for(; ptr; ptr=ptr->link)
  60. 	{
  61. 		printf("%4d",ptr->data);
  62. 	}
  63. 	printf("\n");
  64. }
  65. void main()
  66. {
  67. 	ptr = create();
  68. 	insert(&ptr,ptr->link);
  69. 	printf("After insert--------------------------\n");
  70. 	print_list(ptr);
  71. 	delete_node(&ptr,ptr,ptr->link);
  72. 	printf("After delete--------------------------\n");
  73. 	print_list(ptr);	
  74. }
  75.  
  76.  
  77.  

猜你在找的数据结构相关文章