Advertisement
dzieciol

FIFO

Apr 25th, 2016
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3. typedef struct stos{
  4.         int wart;
  5.         struct stos *poprzedni;
  6.         struct stos *nastepny;
  7.         }stos;
  8.  
  9. void push (stos **head, stos **tail,int liczba)
  10. {
  11.     stos *nowy;
  12.     nowy = (stos *)malloc(sizeof(stos));
  13.     nowy->wart = liczba;
  14.     nowy->poprzedni=NULL;
  15.     nowy->nastepny = NULL;
  16.     if(*head == NULL){
  17.     *head = nowy;
  18.     *tail = nowy;
  19.     //printf("!");
  20.     }
  21.     else {
  22.     nowy->poprzedni=*tail;
  23.     (*tail)->nastepny = nowy;
  24.     (*tail)=nowy;
  25.          }
  26. }
  27. int pop(stos **head)
  28. {
  29.      stos *tmp=(*head);
  30.      int liczba=0;
  31.      if (*head ==NULL)
  32.      printf("Stos pusty");
  33.      else if ((*head)->nastepny==NULL) {
  34.  
  35.      *head=NULL;
  36.      liczba = tmp->wart;
  37.      }
  38.      else{
  39.  
  40.      (*head) =(*head)->nastepny;
  41.      liczba =tmp->wart;
  42.      free(tmp);
  43.      }
  44.      return liczba;
  45. }
  46.  
  47. void show(stos *head)
  48. {
  49.      while (head!=NULL){
  50.      printf("%d\n",
  51.      head->wart);
  52.      head=head->nastepny; }
  53.  
  54. }
  55.  
  56. int main ()
  57. {
  58.     int liczba, option=0;
  59.     stos *head = NULL, *tmp = NULL;
  60.     stos *tail = NULL;
  61.     while(option!=4)
  62.     {
  63.     printf("\n1.Push\n2.Show\n3.Pop\n4.END\n");
  64.             scanf("%d",&option);
  65.             switch(option)
  66.             {
  67.                case 1: printf("Podaj wartosc: ");
  68.                        scanf("%d", &liczba);
  69.                        push(&head,&tail,liczba);break;
  70.                case 2: printf("Wyswetlam stos od gory: \n");
  71.                        show(head); break;
  72.                case 3: printf("Usuwanie ze stosu: ");
  73.                        printf("%d", pop(&head));break;
  74.                case 4: option = 4;break;
  75.                default: break;
  76.             }
  77.     }
  78.     system("pause");
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement