Chain queue implementation

Source: Internet
Author: User

Three files: Main. C student. C student. h

Student. h file:

# Ifndef student_h _ # define student_h _ # include <stdio. h> # include <string. h> # include <malloc. h> # include <stdlib. h> # define cmd_add_node '1' # define cmd_del_node '2' # define cmd_show_node '3' # define cmd_quit_node '4' # define max 10 typedef struct {char name [20]; int grade;} item; typedef struct node {item; struct node * Next;} node; typedef struct {node * front;/* head pointer */node * rear; /* team end pointer */INT items;} queue; static inline void print (const item) {printf ("% s, % d \ n", item. name, item. grade); return;} int queueadd (queue * queue, item); int queuedel (queue * queue, item * item); int queueshow (const queue * Queue); # endif

 

The main. c file is as follows:

# Include "student. H "int main (INT argc, char ** argv) {char cmd = 0; queue = {null, null, 0}; item; // initialize (& Queue); While (1) {printf ("The menun is: \ n"); printf ("1. add \ n "); printf (" 2. delete \ n "); printf (" 3. show \ n "); printf (" 4. quit \ n "); printf (" Enter your choice (1 ~ 4): "); cmd = getchar (); While (getchar ())! = '\ N') continue; // skip the extra part of the input line while (CMD! = Pai_add_node & cmd! = Pai_del_node & cmd! = Cmd_show_node & cmd! = Pai_quit_node) {printf ("Please input 1 ~ 4: "); cmd = getchar (); While (getchar ())! = '\ N') continue; // skip unnecessary parts of the input line} switch (CMD) {case when _add_node: printf ("Enter name and grade :"); scanf ("% S % d", item. name, & (item. grade); While (getchar ())! = '\ N') continue; // skip the excess queueadd (& queue, item); break; case when _del_node: queuedel (& queue, & item ); printf ("delete:"); print (item); break; case when _show_node: queueshow (& Queue); break; case when _quit_node: exit (0); break; default: break;} printf ("\ n");} return 0 ;}

 

The student. c file is as follows:

# Include "student. H "static int queueisempty (const queue * Queue) {return (queue-> items = 0);} static int queueisfull (const queue * Queue) {return (queue-> items = max);}/* plug in to the end of the Team */INT queueadd (queue * queue, item) {node * node; if (queueisfull (Queue) {fprintf (stderr, "the queue is full !!! \ N "); Return-1;} node = (node *) malloc (sizeof (node); If (node = NULL) {fprintf (stderr, "Cannot creat a node !!! \ N "); Return-1;} node-> item = item; node-> next = NULL; If (queueisempty (Queue) {queue-> front = node ;} else {queue-> rear-> next = node;} queue-> rear = node; queue-> items ++; return 0 ;} /* Delete */INT queuedel (queue * queue, item * Item) {node * node; If (queueisempty (Queue) {fprintf (stderr, "The queue is empty !!! \ N "); Return-1;} * Item = queue-> front-> item; node = queue-> front; queue-> front = node-> next; queue-> items --; free (node); Return 0;} int queueshow (const queue * Queue) {int I; node * node = queue-> front; if (queueisempty (Queue) {fprintf (stderr, "the queue is empty !!! \ N "); Return-1;} printf (" all of the Student: \ n "); for (I = 0; I <(queue-> items ); I ++) {print (node-> item); node = node-> next;} printf ("\ n"); Return 0 ;}

 

Chain queue implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.