ramytamer

haybat

May 10th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*Name: Hossam El-Din Haybat Mohamed */
  5. /*ID:2208*/
  6.  
  7. typedef struct Node{
  8.     int Value;
  9.     struct Node *Next;
  10.     struct Node *Previous;
  11. }Node;
  12.  
  13. Node *Head=NULL;
  14. Node *Tail=NULL;
  15. Node *Current_Value;
  16.  
  17. void Test () {
  18.     Node *Display;
  19.     Display = Head;
  20.     printf("\nThe printed full queue:\n");
  21.     printf("=======================\n\n");
  22.     while(Display) {
  23.         printf("[%d]->",Display->Value);
  24.         Display=Display->Next;
  25.     }
  26.     printf("[NULL]\n\n");
  27.  }
  28.  
  29. void Insert_First(int X);
  30. void Count ();
  31. void Insert_Last(int X);
  32. void Remove_First ();
  33. void Remove_Last ();
  34. void Last();
  35. void First();
  36. void Is_Empty();
  37.  
  38. int main(){
  39.     int X;
  40.     int Character;
  41.     printf("\nChoose selection:\n");
  42.     printf("=================\n\n");
  43.     printf("[1] Insert first.\n");
  44.     printf("[2] Insert last.\n");
  45.     printf("[3] Remove first.\n");
  46.     printf("[4] Remove last.\n");
  47.     printf("[5] First.\n");
  48.     printf("[6] Last.\n");
  49.     printf("[7] Is empty?\n");
  50.     printf("[8] Size?\n");
  51.     scanf("%d",&Character);
  52.     if(Character==1) {
  53.         printf("Enter variable:");
  54.         scanf("%d",&X);
  55.         Insert_First(X);
  56.     }
  57.     if(Character==2) {
  58.         printf("Enter variable:");
  59.         scanf("%d",&X);
  60.         Insert_Last(X);
  61.     }
  62.     if(Character==3) {
  63.         Remove_First();
  64.     }
  65.     if(Character==4) {
  66.         Remove_Last();
  67.     }
  68.     if(Character==5) {
  69.         First();
  70.     }
  71.     if(Character==6) {
  72.         Last();
  73.     }
  74.     if(Character==7) {
  75.         Is_Empty();
  76.     }
  77.     if(Character==8) {
  78.         Count();
  79.     }
  80.     else {
  81.         system("cls");
  82.         main();
  83.     }
  84.     return 0;
  85. }
  86. void Insert_First(int X) {
  87.     system("cls");
  88.     Current_Value = (Node*)malloc(sizeof(Node));
  89.     if(Tail==NULL && Head==NULL) {/*Queue is empty*/
  90.         Tail=Current_Value;
  91.         Head=Current_Value;
  92.         Current_Value->Value=X;
  93.         Current_Value->Next=NULL;
  94.         Current_Value->Previous=NULL;
  95.         Test();
  96.         main();
  97.     }
  98.     Current_Value->Value=X;
  99.     Current_Value->Next=Head;
  100.     Head->Previous=Current_Value;
  101.     Head=Current_Value;
  102.     Head->Previous=NULL;
  103.     Test();
  104.     main();
  105. }
  106.  
  107.  
  108. void Insert_Last(int X) {
  109.     system("cls");
  110.     Current_Value = (Node*)malloc(sizeof(Node));
  111.     if(Tail==NULL && Head==NULL) {/*Queue is empty*/
  112.         Tail=Current_Value;
  113.         Head=Current_Value;
  114.         Current_Value->Value=X;
  115.         Current_Value->Next=NULL;
  116.         Current_Value->Previous=NULL;
  117.         Test();
  118.         main();
  119.     }
  120.     Current_Value->Value=X;
  121.     Current_Value->Previous=Tail;
  122.     Tail->Next=Current_Value;
  123.     Tail=Current_Value;
  124.     Tail->Next=NULL;
  125.     Test();
  126.     main();
  127. }
  128.  
  129. void Remove_First () {
  130.     system("cls");
  131.     if(Head==NULL) {/*Queue is empty*/
  132.         printf("\nERROR! Queue is empty!\n\n");
  133.         main();
  134.     }
  135.     Node *Dequeue;
  136.     Dequeue=Head;
  137.     Head=Head->Next;
  138.     if(Head==NULL) {
  139.         Tail=NULL;
  140.         Dequeue->Next=NULL;
  141.         printf("Dequeued Number:[%d]\n\n",Dequeue->Value);
  142.         free(Dequeue);
  143.         Test();
  144.         main();
  145.     }
  146.     Head->Previous=NULL;
  147.     Dequeue->Next=NULL;
  148.     printf("Dequed Number:[%d]\n\n",Dequeue->Value);
  149.     free(Dequeue);
  150.     Test();
  151.     main();
  152. }
  153.  
  154. void Remove_Last () {
  155.     system("cls");
  156.     if(Tail==NULL) {/*Queue is empty*/
  157.         printf("\nERROR! Queue is empty!\n\n");
  158.         main();
  159.     }
  160.     Node *Dequeue;
  161.     Dequeue=Tail;
  162.     Tail=Tail->Previous;
  163.     if(Tail==NULL) {
  164.         Head=NULL;
  165.         Dequeue->Previous=NULL;
  166.         printf("Dequeued Number:[%d]\n\n",Dequeue->Value);
  167.         free(Dequeue);
  168.         Test();
  169.         main();
  170.     }
  171.     Tail->Next=NULL;
  172.     Dequeue->Previous=NULL;
  173.     printf("Dequeued Number:[%d]\n",Dequeue->Value);
  174.     free(Dequeue);
  175.     Test();
  176.     main();
  177. }
  178.  
  179. void First () {
  180.     system("cls");
  181.     printf("First element is:[%d].",Head->Value);
  182.     main();
  183. }
  184.  
  185. void Last() {
  186.     system("cls");
  187.     printf("Last element is:[%d].",Tail->Value);
  188.     main();
  189. }
  190.  
  191. void Is_Empty () {
  192.     system("cls");
  193.     if(Head==NULL) {
  194.         printf("Queue is empty.");
  195.     } else {
  196.         printf("Queue is not empty.");
  197.     }
  198.     main();
  199. }
  200.  
  201. void Count () {
  202.     system("cls");
  203.     Node *Display;
  204.     int Counter=0;
  205.     Display=Head;
  206.     while(Display) {
  207.         Counter++;
  208.         Display=Display->Next;
  209.     }
  210.     printf("Number of elements: [%d].",Counter);
  211.     main();
  212. }
Advertisement
Add Comment
Please, Sign In to add comment