Advertisement
Guest User

Minhdeptrai

a guest
Aug 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef int ElementType;
  5.  
  6. typedef struct Node* NodeType;
  7.  
  8. struct Node {
  9. ElementType element;
  10. NodeType next;
  11. };
  12.  
  13. typedef NodeType Position;
  14. typedef Position List;
  15.  
  16. void MakeNullList(List *Header) {
  17. (*Header)=(NodeType)malloc(sizeof(struct Node));
  18. (*Header)->next= NULL;
  19. }
  20.  
  21. int EmptyList(List L) {
  22. return (L->next==NULL);
  23. }
  24.  
  25. void InsertList(ElementType X,Position P, List *L) {
  26. Position T;
  27. T=(NodeType)malloc(sizeof(struct Node));
  28. T->element=X;
  29. T->next=P->next;
  30. P->next=T;
  31. }
  32.  
  33. void InsertList1(ElementType X, int P1, List *L) {
  34. Position P;
  35. P = *L;
  36. while((P->next != NULL) && (P1 > 1)) {
  37. P = P->next;
  38. P1--;
  39. }
  40. InsertList(X, P, L);
  41. }
  42.  
  43. ElementType Retrieve(Position P, List L) {
  44. if (P->next!=NULL)
  45. return P->next->element;
  46. }
  47.  
  48. Position First(List L) {
  49. return L;
  50. }
  51.  
  52. Position EndList(List L) {
  53. Position P;
  54. P=First(L);
  55. while (P->next!=NULL)
  56. P=P->next;
  57. return P;
  58. }
  59.  
  60. Position Next(Position P, List L) {
  61. return P->next;
  62. }
  63.  
  64. void PrintList(List L) {
  65. Position P;
  66. P = First(L);
  67. while (P != EndList(L)) {
  68. printf("%d ",Retrieve(P,L));
  69. P = Next(P, L);
  70. }
  71. printf("\n");
  72. }
  73.  
  74. void PrintRevList(List L) {
  75.  
  76. if(L->next != NULL) {
  77. Position P = L->next;
  78. if(P->next == NULL)
  79. printf("%d ",P->element);
  80. else {
  81. PrintRevList(L->next);
  82. printf("%d ",P->element);
  83. }
  84. }
  85. }
  86. void ReadList(List *L) {
  87. int i,N;
  88. ElementType X;
  89. MakeNullList(L);
  90. printf("So phan tu danh sach N= ");
  91. scanf("%d",&N);
  92. for(i=1;i<=N;i++) {
  93. printf("Phan tu thu %d: ",i);
  94. scanf("%d",&X);
  95. InsertList(X,EndList(*L),L);
  96. }
  97. }
  98.  
  99. int main() {
  100. List L;
  101. ElementType X;
  102. Position P;
  103. ReadList(&L);
  104. printf("Danh sach vua nhap: ");
  105. PrintList(L);
  106.  
  107. printf("Danh sach nghich dao: ");
  108. PrintRevList(L);
  109. printf("\n");
  110. return (0);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement