Quick actions

cmd+k|ctrl+k

Navigation

Languages

Enqueue

Snippet info

Language

C

Visibility

unlisted

Author

legacy-anonymous

Created

2019-09-14T15:31:49Z

Updated

2019-09-14T15:31:49Z

#include<stdio.h>
#include<stdlib.h>

typedef unsigned int bool;
#define false 0
#define true 1

typedef struct list_node{
        int value;
        struct list_node* Next_ptr;
}Node;

void Create_Queue(Node* head_ptr,Node* rear_ptr){
        head_ptr = (Node*)malloc(sizeof(Node));
        rear_ptr = (Node*)malloc(sizeof(Node));
        head_ptr->value = 0;
        rear_ptr->value = 0;
        head_ptr->Next_ptr = NULL;
        rear_ptr->Next_ptr = NULL;
}

bool isEmpty(Node* head_ptr){
        if(head_ptr->value == 0){
                return true;
        }
        else{
                return false;
        }
}

void Enqueue(int Data,Node* head_ptr,Node* rear_ptr){
        Node* New_Node;
        New_Node = (Node*)malloc(sizeof(Node));

        New_Node->value = Data;
        New_Node->Next_ptr = NULL;


        if(head_ptr->value == 0){
            head_ptr = New_Node;
                rear_ptr = New_Node;
        }

        else{
            rear_ptr->Next_ptr = New_Node; //segementation fault
                rear_ptr = New_Node;
        }

}

/*
void Print_Queue(Node* head_ptr){
        Node* Now_ptr = head_ptr;
        while(Now_ptr!=NULL){
                printf("%d ",Now_ptr->value);
                Now_ptr = Now_ptr->Next_ptr;
        }
}
*/


int main(){
        Node* Head_ptr;  //only declare no assign in global
        Node* Rear_ptr;

        int n;
        Create_Queue(Head_ptr,Rear_ptr);
        while(scanf("%d",&n) && n!=0){
                int i;
                Enqueue(1,Head_ptr,Rear_ptr);
                for(i=1;i<=n;i++){
                        Enqueue(i,Head_ptr,Rear_ptr);
                }
                //Print_Queue(Head_ptr);
                printf("\n");
        }

        return 0;
}
INFO