Advertisement
Adytzu04

l3p1 SDA

Mar 25th, 2013
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. //h
  2.  
  3. #ifndef                 L3P1_H
  4. #define                 L3P1_H
  5. #pragma once
  6.  
  7. #include<iostream>
  8. #include<assert.h>
  9.  
  10. using namespace std;
  11.  
  12. //int *p,*q;
  13.  
  14.  
  15. typedef int Atom;
  16. typedef struct Nod{
  17.     Atom data;
  18.     Nod*succ,*pred;
  19. }*PNod;
  20.  
  21. typedef PNod ListaD;
  22. void Eliberare(ListaD &l);
  23. int Meniu(void);
  24. int CitireInt(char*msg);
  25. void Sterge(ListaD &l,int k); //Atom a
  26. void InsPoz(ListaD &l,Atom,int k);
  27. void Afisare(ListaD l);
  28. void InsFata(ListaD &l,Atom a);
  29.  
  30.  
  31.  
  32. #endif
  33. //l3p1.c
  34.  
  35. #include "l3p1.h"
  36.  
  37. void InsFata(ListaD &l,Atom a)
  38. {
  39.     PNod p=new Nod;
  40.     assert (p);
  41.     p->data=a;
  42.     p->pred=l;
  43.     p->succ=0;
  44.     if(l)
  45.         l->pred=p;
  46.     l=p;
  47. }
  48. void Afisare(ListaD l)
  49. {
  50.     while(l)
  51.     {
  52.         cout<<l->data<<" ";
  53.         l=l->succ;
  54.     }
  55.     cout<<endl;
  56. }
  57.  
  58. void InsPoz(ListaD &l,Atom a,int k) //atom a
  59. {
  60.  
  61.     if(k<0)
  62.         return;
  63.     if(!k)
  64.         InsFata(l,a);
  65.     else
  66.     {
  67.         PNod p=l;
  68.         while(p&&(k>1))
  69.         {
  70.             p=p->succ;
  71.             k--;
  72.         }
  73.         if(p)
  74.         {
  75.             PNod q=new Nod;
  76.             assert(q);
  77.             q->data=a;
  78.             q->pred=p;
  79.             q->succ=p->succ;
  80.             p->succ=q;
  81.             if(q->succ)q->succ->pred=q;
  82.         }
  83.     }
  84. }
  85.  
  86. void Sterge(ListaD &l,int k) //Atom a
  87. {
  88.     PNod p,q;
  89.  
  90.     if((k<0)||(!l))
  91.         return;
  92.     if(!k)
  93.     {
  94.         PNod q=l;
  95.         l=l->succ;
  96.         if(l)
  97.             l->pred=0;
  98.         delete(q);
  99.     }
  100.     else
  101.     {
  102.         //PNod p=1;
  103.     while((l)||(k>1))
  104.         p=p->succ;
  105.     k-1;
  106.     }
  107.     if(p)
  108.     {
  109.         q=p->succ;
  110.         if(q)
  111.         {
  112.                 p->succ=q->succ;
  113.                 if(q->succ)
  114.                 {
  115.                     q->succ->pred=p;
  116.                     delete q;
  117.                 }
  118.         }
  119.     }
  120. }
  121.  
  122. int CitireInt(char*msg)
  123. {
  124.     int rez,ok;
  125.     do
  126.     {
  127.         cout<<msg<<endl;
  128.         cin>>rez;
  129.         if(cin.fail())
  130.         {
  131.             ok=0;
  132.             cin.clear();//sterge bitul de eroare
  133.             cin.ignore(80,'\n');
  134.         }
  135.         else ok=1;
  136.  
  137.     }while(!ok);
  138.  
  139.     return rez;
  140. }
  141.  
  142.  
  143. int Meniu(void)
  144. {
  145.     int opt;
  146.     do
  147.     {
  148.         cout<<"0 - Iesire\n1 - Inserare Fata\n2 - Inserare pe Pozitie\4 - Stergere\n";
  149.         opt=CitireInt("Alegeti 0-4");
  150.        
  151.  
  152.     }while((opt<0)||(opt>4));
  153.  
  154.     return opt;
  155. }  
  156.  
  157. void Eliberare(ListaD &l)
  158. {
  159.     PNod p;//
  160.     while(l)
  161.     {
  162.         PNod p=l;
  163.         l=l->succ;
  164.         delete p;
  165.     }
  166. }
  167.    
  168. //main
  169.  
  170. #include"l3p1.h"
  171.  
  172. int main()
  173. {
  174.    
  175.     ListaD l=0;
  176.     int opt,k;
  177.     Atom a;
  178.  
  179.     do
  180.     {
  181.         opt=Meniu();
  182.         switch(opt)
  183.         {
  184.         case 0:
  185.                 Eliberare(l);
  186.                 break;
  187.         case 1:
  188.                 a=(Atom)
  189.                 CitireInt("val = ");
  190.                 InsFata(l,a);
  191.                 break;
  192.         case 2:
  193.                 Afisare(l);
  194.         case 3:
  195.                 a=CitireInt("val=");
  196.                 InsPoz(l,a,k);
  197.                 break;
  198.         case 4:
  199.                 k=CitireInt("Poz");
  200.                 Sterge(l,k-1);
  201.                 break;
  202.         }  
  203.         //'k=citirePoz()
  204.     }while(opt);
  205.  
  206.     return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement