dzungchaos

CTDL&TT: Danh sách móc đơn

May 31st, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef int ElementType;
  6. typedef struct Node{
  7.     ElementType inf; //luu tru phan tu
  8.     Node *next;      //tro den phan tu tiep theo
  9.  }NodeType;
  10.  
  11. // typedef struct Node NodeType;
  12.  
  13. NodeType *head=NULL; //khoi tao phan tu dau tien
  14.  
  15. NodeType *Insert_ToMiddle(NodeType *list, ElementType x){
  16.     NodeType *newNode;
  17.    
  18.     newNode = (NodeType*) malloc(sizeof(NodeType));
  19.     newNode->inf = x;
  20.     newNode->next = list->next;
  21.     list->next = newNode;
  22.    
  23.     return newNode;
  24. }
  25.  
  26.  
  27. ElementType DeleteMiddle(NodeType *list){
  28.     NodeType *oldNode;
  29.  
  30.     oldNode = list->next;
  31.     ElementType x = oldNode->inf;
  32.     list->next = oldNode->next;
  33.     free(oldNode);
  34.    
  35.     return x;
  36. }
  37.  
  38. NodeType *Insert_ToHead(NodeType *list,ElementType x){
  39.     NodeType *newNode;
  40.    
  41.     newNode=(NodeType*) malloc(sizeof(NodeType));
  42.     newNode->inf=x;
  43.     newNode->next=head;
  44.     head=newNode;
  45.    
  46.     return head;
  47. }
  48.  
  49. NodeType *DeleteHead(NodeType *list){
  50.     NodeType *oldNode;
  51.    
  52.     oldNode = list->next;  
  53.     free(list);
  54.    
  55.     return oldNode;
  56. }
  57.  
  58. NodeType *Insert_ToTail(NodeType *list,ElementType x){
  59.     NodeType *newNode,*cur;
  60.    
  61.     newNode=(NodeType*) malloc(sizeof(NodeType));
  62.     newNode->inf=x;
  63.     newNode->next=NULL;
  64.     cur=head;//tro cur vao dau danh sach
  65.     while(cur!=NULL&&cur->next!=NULL){
  66.         cur=cur->next;
  67.     }
  68.     if(cur==NULL){
  69.         newNode->next=head;
  70.         head=newNode;
  71.     }else{
  72.         cur->next=newNode;
  73.     }
  74.    
  75.     return newNode;
  76. }
  77.  
  78. NodeType *DeleteTail(NodeType *list){
  79.     NodeType *oldNode1, *oldNode2;
  80.     oldNode1 = list;
  81.     oldNode2 = list;
  82.    
  83.     while(oldNode1 != NULL){
  84.         oldNode2 = oldNode1;
  85.         oldNode1 = oldNode1->next;
  86.     }
  87.    
  88.     oldNode2->next = NULL;
  89.     free(oldNode1);
  90.     return list;
  91. }
  92.  
  93. NodeType* InsertWithSort(NodeType *list, ElementType x){
  94.     NodeType *newNode;
  95.     NodeType *preCurrent=NULL;
  96.     NodeType *Current = list;
  97.     newNode = (NodeType*)malloc(sizeof(NodeType));
  98.     while (Current!= NULL&&!(Current->inf>x)){
  99.         preCurrent = Current;
  100.         Current = Current->next;
  101.     }
  102.     newNode->inf=x;
  103.     newNode->next=Current;
  104.     if(preCurrent==NULL){
  105.         head=newNode;
  106.     }else{
  107.         preCurrent->next=newNode;
  108.     }
  109.     return head;
  110. }
  111.  
  112. NodeType *Search(NodeType *list, ElementType x){
  113.     while(list->inf != x)
  114.         list = list->next;
  115.     return list;
  116. }
  117.  
  118. int isEmpty(NodeType *list){
  119.     return !list;
  120. }
  121.  
  122. NodeType *MakeNull(NodeType *list){
  123.     while (!isEmpty(list)) list = DeleteHead(list);
  124.     return list;
  125. }
  126.  
  127. void Print_DS(NodeType *list){
  128.     NodeType *temp;
  129.    
  130.     temp=head;
  131.     printf("-------------\n"); 
  132.     while(temp){
  133.         printf("%d -- ", temp->inf);
  134.         temp=temp->next;
  135.     }
  136.     printf("\n-------------\n");
  137. }
  138.  
  139. int main(){
  140.     ElementType temp;
  141.     NodeType *ptemp;
  142.     for(int i=0;i<5;i++){
  143.         printf("Nhap vao 1 phan tu: ");scanf("%d",&temp);
  144.         ptemp=Insert_ToHead(head,temp);
  145.     }
  146.     Print_DS(head);
  147.     printf("Nhap vao 1 phan tu: ");scanf("%d",&temp);
  148.     ptemp = Insert_ToTail(head,temp);
  149.     printf("Nhap vao 1 phan tu: ");scanf("%d",&temp);
  150.     ptemp = Insert_ToMiddle(head, temp);
  151.     Print_DS(head);
  152.     DeleteMiddle(head);
  153.     Print_DS(head);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment