Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int Q_size = 0; // dynamic size of queue
- struct Node{
- int value;
- struct Node *next;
- };
- typedef struct Node Node;
- struct Node *head = NULL;
- void Insertion(int ele){
- Node *temp = ( Node*) ( malloc(sizeof(Node) ) );
- if( temp == NULL){
- printf("\nmemory is not allocated");
- return;
- }
- temp->value = ele;
- if(Q_size==0){
- temp->next = head;
- head = temp;
- }
- else{
- struct Node *ptr = head;
- int point = Q_size-1;
- while(point--){
- ptr = ptr->next;
- }
- temp->next = ptr->next;
- ptr->next = temp;
- }
- Q_size++;
- }
- void Deletion(){
- if(head == NULL){
- printf("\nQueue is empty");
- return;
- }
- Node* dele = head;
- head = head->next;
- free(dele);
- Q_size--;
- }
- void Print(){
- Node *ptr = head;
- while(ptr != NULL){
- printf("%d ",ptr->value);
- ptr = ptr->next;
- }
- }
- int main()
- {
- int choice,ele,key;
- while(1){
- printf("\n1 for insertion\n2 for deletion\nany to key just print the queue\n");
- scanf("%d",&choice);
- if(choice == 1){
- printf("\nEnter the element ");
- scanf("%d",&ele);
- Insertion(ele);
- }
- else if (choice == 2){
- Deletion();
- }
- else Print();
- printf("\n1 to repeat and any key to exit ");
- scanf("%d",&key);
- if(key != 1) break;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment