Advertisement
dsdeep

Data Struct O-7(1)

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