Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- struct Node
- {
- int data;
- struct Node *next;
- } *temp,*temp2, *node;
- struct Head
- {
- int count;
- struct Node *front;
- struct Node *rear;
- } *head;
- void Head_create()
- {
- head = (struct Head*) malloc(sizeof(struct Head));
- head->count = 0;
- head->front = NULL;
- head->rear = NULL;
- }
- void Enque(int value)
- {
- node=(struct Node*)malloc(sizeof(struct Node));
- node->data = value;
- node->next=NULL;
- if(head->count==0)
- {
- head->front=node;
- head->rear=node;
- (head->count)++;
- }
- else
- {
- head->rear->next=node;
- head->rear=node;
- (head->count)++;
- }
- }
- void swap_q()
- {
- int m,v;
- temp = head->front;
- printf("\t\tEnter value: ");
- scanf("%d",&m);
- while(temp!= NULL)
- {
- if(m==temp->data)
- {
- v=temp->next->data;
- temp->data=v;
- temp->next->data=m;
- break;
- }
- temp = temp->next;
- }
- }
- void Deque()
- {
- //temp=head->front;
- if(head->count == 0)
- printf("\t\tqueue is empty.\n");
- else if(head->count == 1)
- {
- temp=head->front;
- temp2=head->rear;
- head->rear=NULL;
- head->front=NULL;
- free(temp);
- free(temp2);
- (head->count)--;
- }
- else
- {
- temp=head->front;
- head->front=head->front->next;
- (head->count)--;
- free(temp);
- }
- }
- void Search_values(int value)
- {
- int flag = 0,i;
- for(temp = head->front,i=1; temp != NULL; temp = temp->next,i++)
- {
- if(temp->data == value)
- {
- printf("\t\t%d Found in the queue in %d th position.\n",temp->data,i);
- flag=1;
- break;
- }
- }
- if(flag == 0)
- {
- printf("\t\t%d not Found in the queue!\n",value);
- }
- }
- void Show_values()
- {
- temp = head->front;
- if(head->count==0)
- printf("\t\tNo values in the queue!!\n");
- else
- {
- printf("\t\tCurrent Values in the queue\n");
- while(temp!= NULL)
- {
- printf("\t\t-> %d",temp->data);
- temp = temp->next;
- }
- }
- }
- void Rear()
- {
- if(head->count!=0)
- {
- printf("\t\tRear is: %d",head->rear->data);
- }
- else
- printf("\t\tqueue is empty.\n");
- }
- void Front()
- {
- if(head->count!=0)
- {
- printf("\t\tFront is: %d",head->front->data);
- }
- else
- printf("\t\tqueue is empty.\n");
- }
- void Menu_function()
- {
- printf("\n\n\t\t-------------------------------------------------\n");
- printf("\t\t: Queue :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 1. Enqueue :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 2. Dequeue :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 3. Search a Data :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 4. Show All Data :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 5. Queue empty or not!! :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 6. Front :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 7. Rear :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 8. swap :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\t: 9. Exit program :\n");
- printf("\t\t-------------------------------------------------\n");
- printf("\t\tEnter choice :");
- }
- int Take_number()
- {
- int value;
- printf("\t\tEnter a value: ");
- scanf("%d", &value);
- return value;
- }
- int main()
- {
- int choice;
- Head_create();
- for( ; ; )
- {
- Menu_function();
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- Enque(Take_number());
- break;
- case 2:
- Deque();
- break;
- case 3:
- Search_values(Take_number());
- break;
- case 4:
- Show_values();
- break;
- case 5:
- if(head->count==0)
- printf("\t\tqueue is empty\n");
- else
- printf("\t\tqueue is not empty\n");
- break;
- case 6:
- Front();
- break;
- case 7:
- Rear();
- break;
- case 8:
- swap_q();
- break;
- case 9:
- free(temp);
- free(node);
- free(head);
- return EXIT_SUCCESS;
- break;
- default:
- printf("\t\tplease only press 1 to 8\n");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement