dsdeep

Data Struct O-7(2)

Oct 16th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Database{
  4. int d;
  5. struct Database *n;
  6. }node;
  7. node *h;
  8. int main(){
  9. createList();
  10. printList();
  11. deleteData();
  12. printList();
  13. }
  14. void deleteData(){
  15.   int a;
  16. printf("Enter The Desired Data : ");
  17. scanf("%d",&a);
  18. node *list=h;
  19. if(list->n->d==a){
  20.     node *tmp=h;
  21.     h=h->n;
  22.     free(tmp);
  23.     return;
  24. }
  25. while(list->n->n){
  26.     if(list->n->n->d==a){
  27.         node *tmp=list->n;
  28.         list->n=list->n->n;
  29.         free(tmp);
  30.         return;
  31.     }
  32.     list=list->n;
  33. }
  34. printf("Data Not Found In Any Node\n");
  35. }
  36.  
  37. void createList(){
  38. node *list=(node*)malloc(sizeof(node));
  39. int a,b;
  40. printf("Enter Amount Of Node : ");
  41. scanf("%d",&a);
  42.  
  43. printf("Enter Node 1 : ");
  44. scanf("%d",&b);
  45. list->d=b;
  46. list->n=NULL;
  47. h=list;
  48. loop(a-1);
  49. }
  50. void loop(int n){
  51. for(int i=1;i<=n;i++){
  52.     printf("Enter Node %d : ",i+1);
  53.      int a;
  54.     scanf("%d",&a);
  55.     addLast(a);
  56. }
  57. }
  58. void addLast(int a){
  59.    node *list=h;
  60. while(list->n){
  61.     list=list->n;
  62. }
  63. node *temp=(node*)malloc(sizeof(node));
  64. temp->d=a;
  65. temp->n=NULL;
  66. list->n=temp;
  67. }
  68. void printList(){
  69. node *list=h;
  70. while(list){
  71.     printf("%d ",list->d);
  72.     list=list->n;
  73. }
  74. printf("\n");
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment