⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queue.c

📁 在C语言环境下编写的利用队列计算向量的+
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#define FALSE 0#define NULL 0struct listst {    int     dataitem;    struct listst *  next;} ;typedef struct listst listelement;void Menu (int *choice);listelement * AddItem (listelement * list, int data);listelement * RemoveItem (listelement * list);void PrintQueue (listelement * list);void ClearQueue (listelement * list);int main () {    listelement listmember, *list;    int     data, choice;    list = NULL;    do {	Menu (&choice);	switch (choice) {	    case 1: 		printf ("Enter data item value to add  ");		scanf ("%d", &data);		list = AddItem (list, data);		break;	    case 2: 		if (list == NULL)		    printf ("Queue empty!\n");		else		    list = RemoveItem (list);		break;	    case 3: 		PrintQueue (list);		break;	    case 'q': 			printf("Byebye");		break;	    default: 		printf ("Invalid menu choice - try again\n");		break;	}    } while (choice != 'q');    ClearQueue (list);}				/* main */void Menu (int *choice) {    char    local;    printf ("\nEnter\t1 to add item,\n\t2 to remove item\n\\t3 to print queue\n\t4 to quit\n");    do {   	local = getchar ();	if ( !( '0'< local &&  local < '5') && (local != '\n')) {	    printf ("\nyou must enter an integer.\n");	    printf ("Enter 1 to add, 2 to remove, 3 to print, q to quit\n");	}    } while (!('0'< local &&  local < '5'));    *choice = (int) local - '0';}listelement * AddItem (listelement * list, int data) {    listelement * lp = list;    if (list != NULL) {	while (list -> next != NULL)	    list = list -> next;	list -> next = (listelement  *) malloc (sizeof (listelement));	list = list -> next;	list -> next = NULL;	list -> dataitem = data;	return lp;    }    else {	list = (listelement  *) malloc (sizeof (listelement));	list -> next = NULL;	list -> dataitem = data;	return list;    }}listelement * RemoveItem (listelement * list) {    listelement * tempp;    printf ("Element removed is %d\n", list -> dataitem);    tempp = list -> next;    free (list);    return tempp;}void PrintQueue (listelement * list) {    if (list == NULL)	printf ("queue is empty!\n");    else	while (list != NULL) {	    printf ("%d\t", list -> dataitem);	    list = list -> next;	}    printf ("\n");}void ClearQueue (listelement * list) {    while (list != NULL) {	list = RemoveItem (list);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -