Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Node
  5. {
  6.     int data;
  7.     Node* next;
  8. };
  9.  
  10. struct List
  11. {
  12.     Node* head;
  13.     unsigned int length;
  14. };
  15.  
  16. void addElemToList(List* list, unsigned int number)
  17. {
  18.     Node*temp;
  19.     temp=(Node*)malloc(sizeof (Node));
  20.     temp->next=NULL;
  21.     temp->data=number;
  22.     if(list->head == NULL)
  23.     {
  24.         list->head=temp;
  25.     }else{
  26.         temp->next=list->head;
  27.         list->head=temp;
  28.     }
  29.     list->length++;
  30. }
  31.  
  32. void delEleInList(List* list, unsigned int number)
  33. {
  34.     Node* current = list->head;
  35.     if(number==1)
  36.     {
  37.         list->head=list->head->next;
  38.         free(current);
  39.     }else
  40.     {
  41.         for(unsigned int i=0;i<number-2;++i)
  42.             current=current->next;
  43.         Node*temp=current->next;
  44.         current->next=temp->next;
  45.         free(temp);
  46.     }
  47.     list->length--;
  48. }
  49.  
  50.  
  51. void reverse(List* list)
  52. {
  53.     Node* current = list->head;
  54.     Node* prev= NULL, *next=NULL;
  55.     while (current!=NULL)
  56.     {
  57.         next = current->next;
  58.         current->next=prev;
  59.         prev = current;
  60.         current=next;
  61.     }
  62.     list->head=prev;
  63. }
  64.  
  65. void print(List* list)
  66. {
  67.     Node*current=list->head;
  68.     for(unsigned int i=0; i<list->length; ++i)
  69.     {
  70.         printf("%d ",current->data);
  71.         current=current->next;
  72.     }
  73. }
  74.  
  75. int getItem(List* list, unsigned int index)  
  76. {  
  77.      
  78.     Node* current=list->head;
  79.     unsigned int count = 0;
  80.     while(current != NULL)  
  81.     {  
  82.         if(count == index)
  83.             return(current->data);  
  84.         count++;  
  85.         current = current->next;  
  86.     }
  87. }
  88.  
  89.  
  90. void setValueItem(List* list, unsigned int index, int newData)  
  91. {  
  92.      
  93.     Node* current=list->head;
  94.     unsigned int count = 0;
  95.     while(current != NULL)  
  96.     {  
  97.         if(count == index)
  98.             current->data=newData;  
  99.         count++;  
  100.         current = current->next;  
  101.     }
  102. }
  103.  
  104.  
  105. int main()
  106. {
  107.     List list;
  108.     list.length=0;
  109.     list.head=NULL;
  110.     for(int i=1;i<=5;++i)
  111.         addElemToList(&list,i);
  112.    
  113.     print(&list); printf("%s","\n");
  114.     delEleInList(&list,4);
  115.     print(&list); printf("%s","\n");
  116.     int temp=getItem(&list,1);
  117.     printf("\n%d\n\n",temp);
  118.     reverse(&list);
  119.     print(&list); printf("%s","\n");
  120.    
  121.     setValueItem(&list,2,123);
  122.     print(&list); printf("%s","\n");
  123.    
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement