Thread: Queue implimentation

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64

    Queue implimentation

    hello everyone i am trying to create a code to represent a queue using a linked list but i get the disturbing "Segmentation fault coredumped" after compilation any help plese thist are the three parts of the code

    Code:
    #ifndef QUEUE_H
    #define QUEUE_H
    
    #include <stdbool.h>
    
    typedef int Item;
    typedef struct queue_type *Queue;
    typedef struct node *return_node;
    
    Queue create(void);
    void insert(Queue fxn, Item x);
    Item remove_q(Queue fxn);
    return_node return_first(Queue fxn);
    return_node return_last(Queue fxn);
    bool  is_empty(Queue fxn);
    void terminate(char *message);
    
    #endif
    This is the function definations

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include "queue.h"
    
    struct node{
        Item *p;
        struct node *next;
        };
        
    struct queue_type{
        struct node *first;
        struct node *last;
        };
    
    Queue create(void)
    {
        Queue fxn = malloc (sizeof(struct queue_type));
        if(fxn==NULL){
                terminate("\nError in create: ");
                exit(EXIT_FAILURE);
            }
        fxn->first=NULL;
        fxn->last=NULL;
        return fxn;
    }
            
    
    void insert (Queue fxn, Item x)
    {
        struct   node *new_node = malloc(sizeof(struct node));
        if(new_node==NULL){
            terminate("Error in insert:");
            exit(EXIT_FAILURE);
        }
        
        new_node->p = malloc(sizeof(x));
        if(new_node->==NULL){
            terminate("Error in allocating size for new node pointer");
            exit(EXIT_FAILURE);
        }
        
        new_node->p = &x;
        new_node->next = NULL;
        if(fxn->first==NULL){
            fxn->first = new_node;
        }
        else{
            struct  node *old_last;
            old_last=fxn->last;
            old_last->next=new_node;
            fxn->last=new_node;
        }
    }
    
    Item remove_q (Queue fxn)
    {
        struct node *old_first;
        Item i;
        old_first=fxn->first;
        i = *(old_first->p);
        fxn->first=old_first->next;
        free(old_first);
        
        return i;
    }
    
    return_node  return_first(Queue fxn)
    {
        return fxn->first;
    }
    
    return_node return_last(Queue fxn)
    {
        return fxn->last;
    }
    
    bool is_empty(Queue fxn)
    {
        return fxn->last == NULL;
    }        
    
    void terminate(char *message)
    {
        printf("\n%s",message);
        exit(EXIT_FAILURE);
    }
    This is a sample to test the code
    Code:
    #include<stdio.h>
    #include "queue.h"
    
    int main(void)
    {
        printf("Create succeded");
        Queue s1,s2;
        int n;
        printf("Create succeded");
        s1=create();
        s2=create();
        printf("Create succeded");
        
        insert (s1,1);
        insert (s1,2);
        insert (s1,3);
        insert (s1,4);
        insert (s2,5);
        
        int i;
        for(i=0;i<3;i++){
            printf("Popped %d from s1\n",remove_q(s1));
        }
        printf("Poooed %d from s2\n",remove_q(s2));
        if(is_empty(s1)){
            printf("s1 is empty\n");
        }
        if(is_empty(s2)){
            printf("s2 is empty\n");
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    In the same way that you extract an item using this
    i = *(old_first->p);

    > new_node->p = &x;
    You need to do the same thing when inserting into the list, say
    *(new_node->p) = x;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by Salem View Post
    In the same way that you extract an item using this
    i = *(old_first->p);

    > new_node->p = &x;
    You need to do the same thing when inserting into the list, say
    *(new_node->p) = x;
    Thanks man! that was of gr8 help i have debuged it already i just wish to ask if a queue is very applicable?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Graph implimentation
    By ravimehta in forum Tech Board
    Replies: 5
    Last Post: 08-28-2011, 10:36 AM
  2. (queue*)this)->queue::qput’ does not have class type
    By brack in forum C++ Programming
    Replies: 13
    Last Post: 11-11-2010, 03:41 PM
  3. FMOD implimentation, how'd you do it?
    By Jeremy G in forum Game Programming
    Replies: 10
    Last Post: 06-12-2004, 12:04 AM
  4. Implimentation of viewing system probs
    By gazsux in forum Game Programming
    Replies: 0
    Last Post: 03-13-2003, 04:25 PM
  5. Queue and Priority Queue
    By Pamela in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2001, 11:09 PM