RenHao

DS_HW1

Nov 13th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  5. /*
  6. For a linked list of integers, create a user-friendly, men-driven program that performs the following
  7. operations.
  8. (a) Create a list of integers.              //checked
  9. (b) Insert an integer at the end of a list.
  10. (c) Search for an integer.                  //checked
  11. (d) Delete an integer.
  12. (e) Print out a list.                       //checked
  13. */
  14.  
  15. typedef struct list
  16. {
  17.     int Data ;
  18.     struct list *link ;
  19. }Node ;
  20.  
  21. Node *Create_list(Node *N,int num)
  22. {
  23.     Node *New_Node ;
  24.    
  25.     New_Node = malloc( sizeof( Node) );
  26.     New_Node->Data = num ;
  27.     New_Node->link = N ;
  28.    
  29.     return New_Node ;
  30.    
  31. }
  32.  
  33. void printList(Node *first)
  34. {
  35.     printf("The list contains: ");
  36.     for (; first; first = first->link)
  37.         printf("%4d", first->Data);
  38.     printf("\n");
  39. }
  40.  
  41. int search(Node *first,int n)
  42. {
  43.     Node *t;
  44.     t = malloc( sizeof( Node) );
  45.     t = first ;
  46.    
  47.     while(t->link != NULL)
  48.     {
  49.         if(t->Data == n)
  50.         {
  51.             //printf("integer %d is found\n",n);
  52.             return 1;
  53.             break;
  54.         }
  55.  
  56.         t = t->link ;
  57.     }
  58.     return -1; 
  59. }
  60.  
  61. void End_insert(Node **first,int num)
  62. {
  63.     Node *New_Node ;
  64.    
  65.     New_Node = malloc( sizeof( Node) );
  66.     New_Node->Data = num ;
  67.     New_Node->link = NULL ;
  68.     for(; *first != NULL ; *first = *first->link)
  69.     {
  70.         if(*first->link == NULL)
  71.             *first->link = New_Node ;
  72.     }
  73. }
  74. /*
  75. void delete(Node **first,int d)
  76. {
  77.     Node* trail , *x ;
  78.     trail = first ;
  79.     x = trail->link ;
  80.    
  81.     for(; trail != NULL ; trail = trail->link)
  82.     {
  83.         if(trail->Data == d)
  84.        
  85.     }
  86.     if(x->Data == d)
  87.         {
  88.             trail->link = x->link ;
  89.             free(x);
  90.         }
  91.     else if(first->Data == d)
  92.         {
  93.            
  94.         }
  95.    
  96. }
  97. */
  98. int main(int argc, char *argv[])
  99. {
  100.     Node *head , *ptr , *end;
  101.     int DataArray[5] = { 1, 42 , 37 ,98 ,65};
  102.     int i,j;
  103.     head = NULL ;
  104.     end = Create_list(end,5);
  105. //  head = malloc( sizeof(Node) );
  106.     for(i=4 ; i>=0 ; i--)
  107.         head = Create_list(head,DataArray[i]);
  108.    
  109.     scanf("%d",&j);
  110.     if( search(head,j) == 1)
  111.         printf("integer %d is found\n",j);
  112.     else
  113.         printf("integer is not found\n");
  114.     End_insert(&head,9);
  115.     ptr = head ;
  116.     printList(head);
  117.     while(ptr != NULL)
  118.     {
  119.         printf("data = %d \n",ptr->Data);
  120.         ptr = ptr->link ;
  121.     }
  122.    
  123.    
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment