Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct nod
  5. {
  6. int inf;
  7. nod *next;
  8. };
  9. nod *prim;
  10. void creare (nod *&prim)
  11. {
  12. int n,i;
  13. nod *p,*q;
  14. cout<<"Dati numarul de elemente: ";
  15. cin>>n;
  16. prim=new(nod);
  17. cout<<"Informatia 1= ";
  18. cin>>prim->inf;
  19. prim->next=NULL;
  20. p=prim;
  21. for (i=2; i<=n; i++)
  22. {
  23. q=new(nod);
  24. cout<<"Informatia "<<i<<"= ";
  25. cin>>q->inf;
  26. q->next=NULL;
  27. p->next=q;
  28. p=q;
  29. }
  30. }
  31. void add(nod* &prim)
  32. {
  33. nod *p,*q;
  34. if (prim==NULL)
  35. {
  36. prim=new(nod);
  37. cin>>prim->inf;
  38. prim->next=NULL;
  39. }
  40. else
  41. {
  42. p=prim;
  43. q=new(nod);
  44. while (p->next)
  45. p=p->next;
  46. q=new(nod);
  47. cin>>q->inf;
  48. q->next=NULL;
  49. p->next=q;
  50.  
  51. }
  52. }
  53. void sterg(nod* &prim)
  54. {
  55. nod *q,*p;
  56. if (prim==NULL)
  57. cout<<"Nu ai ce sa stergi...";
  58. else
  59. {
  60. p=prim;
  61. prim=p->next;
  62. delete p;
  63. }
  64. }
  65. void afis(nod* &prim)
  66. {
  67. nod *p=prim;
  68. while (p)
  69. {
  70. cout<<p->inf<<" ";
  71. p=p->next;
  72. }
  73. cout<<endl;
  74. }
  75.  
  76. int main()
  77. {
  78. creare(prim);
  79. afis(prim);
  80. add(prim);
  81. afis(prim);
  82. sterg(prim);
  83. afis(prim);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement