Hi,
I'm new here, so I'm sory if this theme already exsist.

I'm trying to do a database of students in c, and I want to use queue for realizing that.
For example:
Name: char
surname: char
id number: char or int
subjects: queue

User will enter id number and get all information about that student.
I don't know hot to put this two thing together, so I'm asking you for some help.

Recommended Answers

All 8 Replies

In your shoes my struct definitions would probably look like this:

struct Subject
{
    char name[MAX_SUBJECT];
};

struct Node
{
    struct Subject data;
    struct Node* prev;
    struct Node* next;
};

struct Queue
{
    struct Node* head;
    struct Node* tail;
    int size;
}

struct Student
{
    int id;
    char first_name[MAX_FIRST];
    char last_name[MAX_LAST];
    struct Queue* subjects;
};

I prefer a linked list based queue because it's more extensible. However, in this case the nubmer of subjects may have a hard upper limit, in which case a rotating array implementation would work fine. But without knowing for sure if that's the case, I'll have to suggest a linked list as the go-do implementation. ;)

So from here your task would be to implement the queue.

Yes, I have something like that in my mind. I agree, that rotating array is better, but I decided to do it with queue.
Now, I want to write a program wher the user type students data and when he's finished he could print the whole list. Can you help me with that to? It's been a very long time since I was working with C, so I'm aksking for some help :)

I agree, that rotating array is better, but I decided to do it with queue.

A queue is an abstract data type, the rotating array is one option for implementing a queue. Just FYI.

Can you help me with that to?

If you have any specific problems with your code, feel free to post them on this forum and someone will help you.

Here is my code:

struct lnkdList list;

Node firstLL(void) {
    return list.first;
}

Node lastLL(void) {
    return list.last;
}


Node nextLL(Node pos) {
    return pos->next;
}

void insertDefPosLL(int elem, Node pos) {

    Node tmp = (Node) malloc(sizeof(struct lnkdListNode));

    tmp->data = elem;
    tmp->next = nextLL(pos);

    pos->next = tmp;

    // vstavljamo prvi element
    if(lastLL() == NULL)
        list.last = firstLL();
    // vstavljamo na koncu
    else if(nextLL(lastLL()) == pos)
        list.last = pos;
}


void insertStudent (char n, char s, int i, char su) {    
    if(lastLL() == NULL)
        insertDefPosLL(elem, firstLL());
    else
        insertDefPosLL(elem, nextLL(lastLL()));
}
     }

void init_queue(queue *q) {     
    q->first = 0;
    q->last  = QUEUE_SIZE - 1;
    q->count = 0;
}

void enqueue(queue *q,int x)   
{
     q->last = (q->last + 1) % QUEUE_SIZE;
     q->q[ q->last ] = x;
     q->count = q->count + 1;
}

int dequeue(queue *q)  
{ 
    int x = q->q[ q->first ];
    q->first = (q->first + 1) % QUEUE_SIZE;
    q->count = q->count - 1;
    return x;
}


// main function
void linkedList(void) {

     int choice; // uporabnikova izbira
     queue q;
     char  p;
     char name;
     char surname;
     char subj;
     int idN;

    do {
        choice = menuLL();

        switch (choice) {
            // vstavljanje elementa na poljuben polozaj v seznamu

            case 1:

               init_queue (&q);

               printf("\tstudent's name: ");
               scanf("%d",&name);

               printf("\tstudents surname: ");
               scanf("%d",&surname);

               printf("\tinsert id: ");
               scanf("%d",&idN);

               printf("\tinsert subject: ");
               scanf("%d",&subj);

              insertStudent(&name, &surname, &idN, &subj);

        //       enqueue(&q,p);
                break;

            case 2:
             //    printf(dequeue(&p));
              insertStudent(&name, &surname, &idN, &subj);

                 break;
            case 10:
                break;
        }
    } while(choice != 10);
}


int menuLL(void) {
    int choice;

    // brisanje zaslona (za Cygwin)
    system("cmd /c cls");
    //system("cls");

    printf("\n\t************************************************************\n");
    printf("\t*** Student database ***\n");
    printf("\t************************************************************\n\n");
    printf("\t 1.  insert student \n\t 2.   print list   \n\t10.  close\n\n\tenter your choice : ");
    scanf("%d", &choice);
    printf("\n\n");
    return choice;
}

Here is my code:

You didn't ask a question. What do you want help with?

I want to make a student database. User will write name, surname, id and list of subjects for some students.
Subjects should be saved in queue.
When I run the program I can't write characters, I can only write numbers.

Then why do you use %d for your input if you want to enter strings? Look up and study the printf() formatting characters again.

Well, I write code, where user write students name and id. I change code completly, because my old code has too mush errors. Now I want to add structure, where we can also write subjects for each student (I decided to realize that with queue). I need some help here... Here is my code:

#include <stdio.h>
#include <stdlib.h>
//#include <ctype.h>
#include <string.h>

/* function prototypes */
struct node * initnode( char *name, char *priimek, int id );
void printnode( struct node * );
void printlist( struct node * );
void add( struct node * );
void insertnode( struct node * );


/* struktura o študentu*/
struct node {
   char name[20];
   char priimek[20];
   int  id;
   struct node *next;
   struct queue *subjects;
};

struct queue {
       struct node *head;
       struct node *tail;
       int size;
       };



/* head points to first node in list, end points to last node in list */
/* initialise both to NULL, meaning no nodes in list yet */
struct node *head = (struct node *) NULL;
struct node *end = (struct node *) NULL;

/* this initialises a node, allocates memory for the node, and returns   */
/* a pointer to the new node. Must pass it the node details, name and id */
struct node * initnode( char *name, char *priimek, int id )
{
   struct node *ptr;
   ptr = (struct node *) calloc( 1, sizeof(struct node ) );
   if( ptr == NULL )                      //če je seznam prazen 
       return (struct node *) NULL;       //vrne NULL 
   else {                                  

       strcpy( ptr->name, name );          //zapolni imena 
       strcpy (ptr->priimek, priimek);
       ptr->id = id;                      //kopira id 
       return ptr;                       //kazalec premakne na novega študenta  
   }
}


//izpiše posameznega študenta 
void printnode( struct node *ptr )        
{
   printf("name: %s\n", ptr->name );
   printf("last name: %s\n", ptr->priimek);
   printf("id: %d\n", ptr->id );
   printf("\n");
}

/* this prints all nodes from the current address passed to it. If you    */
/* pass it 'head', then it prints out the entire list, by cycling through */
/* each node and calling 'printnode' to print each node found             */
void printlist( struct node *ptr )
{
   while( ptr != NULL )           /* continue whilst there are nodes left */
   {
      printnode( ptr );           /* print out the current node           */
      ptr = ptr->next;            /* goto the next node in the list       */
   }
}

/* this adds a node to the end of the list. You must allocate a node and  */
/* then pass its address to this function                                 */
void add( struct node *new )  /* adding to end of list */
{
   if( head == NULL )      /* if there are no nodes in list, then         */
   {
       head = new;         /* set head to this new node                   */
       end = new; 
   }
   else
   {
       end->next = new;        /* link in the new node to the end of the list */
   new->next = NULL;       /* set next field to signify the end of list   */
   end = new;              /* adjust end to point to the last node        */
   }

}



/* this is the main routine where all the glue logic fits                 */
main()
{
   char name[20];
   char priimek[20];
   int id, ch = 1;
   struct node *ptr;

   while( ch != 0 ) {
          printf("\n");
          printf("************ Student list***********\n");
      printf("1 insert student \n");
      printf("2 prit list of students \n");
      printf("0 Izhod\n");
      printf("****************************************\n");
      printf("\n");
      scanf("%d", &ch );

      switch( ch )
      {
          case 1:  /* adding students */
                   printf("name: ");
                   scanf("%s", name );

                   printf("last name: ");
                   scanf("%s", priimek );

                   printf("id: ");
                   scanf("%d", &id );

                   printf("\n");

                   ptr = initnode( name, priimek, id );
                   add( ptr );
                   break;

          case 2:  /*list of all students */
                   printlist( head );
                   break;


      }
   }
  // deletelist( head );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.