2016-03-25 16 views
-1

Ich habe diesen Code gefunden, der die Operation einer kreisförmigen Warteschlange zeigt. Ich experimentiere damit, um mehr darüber zu erfahren, wie Warteschlangen funktionieren. In diesem Fall möchte ich die folgende Implementierung ändern, die anstelle des Druckens von Zahlen Sätze drucken wird.Datenstruktur mit zirkularer Warteschlange in c

Ich habe versucht, den Datentyp zu ändern, aber es weiterhin Zahlen drucken.

Irgendwelche Ideen?

#include <stdio.h> 
#include <conio.h> 

#define MAX 10 

void addq (int *, int, int *, int *) ; 
int delq (int *, int *, int *) ; 
void display (int *) ; 

void main() 
{ 
    int arr[MAX] ; 
    int i, front, rear ; 

    system("cls") ; 

    /* initialise data member */ 

    front = rear = -1 ; 
    for (i = 0 ; i < MAX ; i++) 
     arr[i] = 0 ; 

    addq (arr, 14, &front, &rear) ; 
    addq (arr, 22, &front, &rear) ; 
    addq (arr, 13, &front, &rear) ; 
    addq (arr, -6, &front, &rear) ; 
    addq (arr, 25, &front, &rear) ; 

    printf ("\nElements in the circular queue: ") ; 
    display (arr) ; 

    i = delq (arr, &front, &rear) ; 
    printf ("Item deleted: %d", i) ; 

    i = delq (arr, &front, &rear) ; 
    printf ("\nItem deleted: %d", i) ; 

    printf ("\nElements in the circular queue after deletion: ") ; 
    display (arr) ; 

    addq (arr, 21, &front, &rear) ; 
    addq (arr, 17, &front, &rear) ; 
    addq (arr, 18, &front, &rear) ; 
    addq (arr, 9, &front, &rear) ; 
    addq (arr, 20, &front, &rear) ; 

    printf ("Elements in the circular queue after addition: ") ; 
    display (arr) ; 

    addq (arr, 32, &front, &rear) ; 

    printf ("Elements in the circular queue after addition: ") ; 
    display (arr) ; 

    getch() ; 
} 

/* adds an element to the queue */ 
void addq (int *arr, int item, int *pfront, int *prear) 
{ 
    if ((*prear == MAX - 1 && *pfront == 0) || ( *prear + 1 == *pfront)) 
    { 
     printf ("\nQueue is full.") ; 
     return ; 
    } 

    if (*prear == MAX - 1) 
     *prear = 0 ; 
    else 
     (*prear)++ ; 

    arr[*prear] = item ; 

    if (*pfront == -1) 
     *pfront = 0 ; 
} 

/* removes an element from the queue */ 
int delq (int *arr, int *pfront, int *prear) 
{ 
    int data ; 

    if (*pfront == -1) 
    { 
     printf ("\nQueue is empty.") ; 
     return NULL ; 
    } 

    data = arr[*pfront] ; 
    arr[*pfront] = 0 ; 

    if (*pfront == *prear) 
    { 
     *pfront = -1 ; 
     *prear = -1 ; 
    } 
    else 
    { 
     if (*pfront == MAX - 1) 
      *pfront = 0 ; 
     else 
      (*pfront)++ ; 
    } 
    return data ; 
} 

/* displays element in a queue */ 
void display (int * arr) 
{ 
    int i ; 
    printf ("\n") ; 
    for (i = 0 ; i < MAX ; i++) 
     printf ("%d\t", arr[i]) ; 
    printf ("\n") ; 
} 

Antwort

0

Damit Sie Kreis Warteschlangen verwenden Sätze drucken statt Zahlen alles, was Sie tun müssen, ist für die Warteschlange eine 2D-Zeichenmatrix zu sein. Sie können die Implementierung im folgenden Code sehen:

#include <stdio.h> 
#include <string.h> 
#define MAX 10 

/* adds an element to the queue */ 
void addq (char **arr, char *item, int *pfront, int *prear) 
{ 
    if ((*prear == MAX - 1 && *pfront == 0) || ( *prear + 1 == *pfront)) 
    { 
     printf ("\nQueue is full.") ; 
     return ; 
    } 

    if (*prear == MAX - 1) 
     *prear = 0 ; 
    else 
     (*prear)++ ; 

    arr[*prear] = item ; 

    if (*pfront == -1) 
     *pfront = 0 ; 
} 

/* removes an element from the queue */ 
char * delq (char **arr, int *pfront, int *prear) 
{ 
    char *data ; 
    char *c; 
    if (*pfront == -1) 
    { 
     printf ("\nQueue is empty.") ; 
     return NULL ; 
    } 

    c = (char *)malloc(50*sizeof(char)); 
    c = "dummy content"; 
    data = arr[*pfront] ; 
    arr[*pfront] = c ; 

    if (*pfront == *prear) 
    { 
     *pfront = -1 ; 
     *prear = -1 ; 
    } 
    else 
    { 
     if (*pfront == MAX - 1) 
      *pfront = 0 ; 
     else 
      (*pfront)++ ; 
    } 
    return data ; 
} 

/* displays element in a queue */ 
void display (char ** arr) 
{ 
    int i ; 
    printf ("\n") ; 
    for (i = 0 ; i < MAX ; i++) 
     printf ("%s\n", arr[i]) ; 
    printf ("\nDone here\n") ; 
} 
void main() 
{ 
    char **arr = (char **)malloc(MAX * sizeof(char *)); 
    int i, front, rear ; 
    char *ch; 
    /* initialise data member */ 

    front = rear = -1 ; 
    for (i = 0 ; i < MAX ; i++) { 
     arr[i] = (char *)malloc(50 * sizeof(char)); 
     arr[i] = "dummy content" ; 
    } 
    addq (arr, "hello data 1", &front, &rear) ; 
    addq (arr, "hello data 2", &front, &rear) ; 
    addq (arr, "hello data 3", &front, &rear) ; 
    addq (arr, "hello data 4", &front, &rear) ; 
    addq (arr, "hello data 5", &front, &rear) ; 

    printf ("\nElements in the circular queue: ") ; 
    display (arr) ; 

    ch = delq (arr, &front, &rear) ; 
    printf ("Item deleted: %s", ch) ; 

    ch = delq (arr, &front, &rear) ; 
    printf ("\nItem deleted: %s", ch) ; 

    printf ("\nElements in the circular queue after deletion: ") ; 
    display (arr) ; 

    addq (arr, "hello data 6", &front, &rear) ; 
    addq (arr, "hello data 7", &front, &rear) ; 
    addq (arr, "hello data 8", &front, &rear) ; 
    addq (arr, "hello data 9", &front, &rear) ; 
    addq (arr, "hello data 10", &front, &rear) ; 

    printf ("Elements in the circular queue after addition: ") ; 
    display (arr) ; 

    addq (arr, "hello data 11", &front, &rear) ; 

    printf ("Elements in the circular queue after addition: ") ; 
    display (arr) ; 

}