queue insertions

This is a discussion on queue insertions within the C Programming forums, part of the General Programming Boards category; I have a queue of 10 elements. I want to insert only new values that are not in the list, ...

  1. #1
    Unregistered
    Guest

    queue insertions

    I have a queue of 10 elements. I want to insert only new values that are not in the list, then delete head to allow for new value. Does anyone have an effective method to test all values of a queue to see if new vaslue exists or not. Any help is gratly appreciated. e

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If you could search through it to pick and choose then it wouldn't really be a queue would it? But since you asked you can simply traverse like a linked list, though that will defeat the entire purpose so why not just use a binary tree which restricts multiple nodes with the same value?

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    I am working on a school assignment and am having difficulty. The assignment calls for you to use a queue, so I have no choice. If you could help me through this one segment I can complete this project. This is my code: I need a way to compare each new element to the entire list of 10 elements. If it already exists I will increase array counter and test next value.

    /* queue.c */
    /* Demo of dynamic data structures in
    C */

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

    #define FALSE 0
    #define NULL 0

    typedef struct {
    int dataitem;
    struct listelement *link;
    } listelement;

    listelement * AddItem (listelement * listpointer, int
    data);
    listelement * RemoveItem (listelement * listpointer);
    void PrintQueue (listelement * listpointer);
    void ClearQueue (listelement * listpointer);

    main () {
    listelement listmember, *listpointer;
    int data, ct;

    int d, r, x, y, count, framect, z;
    int i, a, pages[39],frames[9];
    unsigned seed;

    printf("\nThis is the complete ref string\n");
    printf("Enter seed: ");

    scanf("%u", &seed);
    srand(seed);

    for( y=0;y<40;y++){
    a = (1 + rand() % 20);
    printf("%10d", a);

    pages[y]=a;}



    listpointer = NULL;
    y = 0;
    for(ct=0;ct<10;ct++)
    { data=pages[y];
    listpointer = AddItem (listpointer, data);
    y++;}

    PrintQueue(listpointer);
    scanf("%d", &data);
    return 0;
    }
    /* main */

    listelement * AddItem (listelement * listpointer, int
    data) {

    listelement * lp = listpointer;

    if (listpointer != NULL) {
    while (listpointer -> link != NULL)
    listpointer = listpointer -> link;
    listpointer -> link = (struct listelement *) malloc
    (sizeof (listelement));
    listpointer = listpointer -> link;
    listpointer -> link = NULL;
    listpointer -> dataitem = data;
    return lp;
    }
    else {
    listpointer = (struct listelement *) malloc (sizeof
    (listelement));
    listpointer -> link = NULL;
    listpointer -> dataitem = data;
    return listpointer;
    }
    }

    listelement * RemoveItem (listelement * listpointer) {

    listelement * tempp;
    printf ("Element removed is %d\n", listpointer ->
    dataitem);
    tempp = listpointer -> link;
    free (listpointer);
    return tempp;
    }

    void PrintQueue (listelement * listpointer) {

    if (listpointer == NULL)
    printf ("queue is empty!\n");
    else
    while (listpointer != NULL) {
    printf ("%d\t", listpointer -> dataitem);
    listpointer = listpointer -> link;
    }
    printf ("\n");
    }

    void ClearQueue (listelement * listpointer) {

    while (listpointer != NULL) {
    listpointer = RemoveItem (listpointer);
    }
    }

  4. #4
    Nosepicker DrakkenKorin's Avatar
    Join Date
    Nov 2001
    Posts
    407
    Don't they teach anything about meaningful data names anymore?

    Well if the specs for your assignment don't say anything about creating new prototypes, then you can create a new one that will take in the next element to be added, traverse the list and if it is NOT already in the queue, then go ahead and add it into the queue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 08:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21