c - C 中的通用结构

标签 c linked-list

我在使用 void* 返回值 int/double 时遇到问题。

例如:

#include "stdio.h"
#include "stdlib.h"

typedef struct list {

  void *info;
  struct list *prox;

} List;

typedef struct queue {

  List* begin;
  List* end;

} Queue;


Queue* create_queue (void) {

 Queue* f = (Queue*) malloc(sizeof(Queue));
 f->begin = f->end = NULL;
 return f;

}


 Queue* insert_queue_end (List* end, void* v) {

 List* p = (List*) malloc(sizeof(List));
  p->info=v;
  p->prox=NULL;
  if(end!=NULL) {
    end->prox=p;
  }
  return p;
 }

double queue_empty_double(Queue* f) {
   return (f->begin==NULL);
}


double queue_remove_double(Queue* f) {

   List* t;
   double v;


  if(queue_empty_double(f)) {
    exit(1);
  }

   t=f->begin;
   v=*((double*)(t->info));
   f->begin=t->prox;
   if (f->begin==NULL) {
     f->end = NULL;
   }
    free(t);
    printf("%.3lf\n",v);


  }


  void insert_queue(Queue* f, void* v) {

    f->end = insert_queue_end(f->end,v);
    if(f->begin==NULL) {
    f->begin=f->end;
  }

 }

 void print_queue_double(Queue* f) {

  List* i;
  for(i=f->begin;i!=NULL;i=i->prox)
  printf("%.3lf\n",*((double*)i->info));

  }



 int main () {
   Queue* f;
   f = create_queue();
   char ent1[100];
   double n1;
   scanf("%s",ent1);
   while(ent1[0]!='X')
   {
     if(ent1[0]=='E') {
        scanf("%lf",&n1);
        insert_queue(f,&n1);
        scanf("%s",ent1);
    }
    else if (ent1[0]=='D') {
        queue_remove_double(f);
        scanf("%s",ent1);
    }
  }


}

但是该函数不适用于 double 值,只能使用 int

另一个新代码,现在代码可以打印 double ,但在函数queue_remove_double中存在问题,她应该从队列中删除第一个元素并打印第一个元素。我相信这个问题来自通用结构,因为该函数删除了第一个并将其打印在普通结构中。

Input:
E 1.2
E 2.1
D
X

Output:
1.200

The wrong output:
2.100

最佳答案

它适用于代码块IDE。指定 *info 的类型以了解如何强制转换指针。

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

typedef enum DataTypes {
  INTEGER,
  FLOAT,
  DOUBLE,
  CHAR
} DataType;

typedef struct list {
   void *info;
   struct list *next;
   DataType type;
} List;


List* create_list(void *firstInfo, DataType firstType)
{
   List *f=(List*)malloc(sizeof(List));
   f->info=firstInfo;
   f->type=firstType;
   f->next=NULL;
   return f;
}

List* insertion(List *end, void *data, DataType type)
{
    List *p=(List*)malloc(sizeof(List));
    p->info=data;
    p->next=NULL;
    p->type=type;
    if(end != NULL)
    {
        end->next=p;
    }
    return p;
}

void showTheList(List **theBase)
{
    List *run=*theBase;

    while(run != NULL)
    {
        switch(run->type)
        {
            case INTEGER:
                printf("Showing the value: %d \n",*((int*)run->info));
                break;
            case FLOAT:
                printf("Showing the value: %f \n",*((float*)run->info));
                break;
            case DOUBLE:
                printf("Showing the value: %lf \n",*((double*)run->info));
                break;
            default:
                printf("Showing the value: %c \n",*((char*)run->info));
        }
        run=run->next;
    }
}

List* getEnd(List **theBase)
{
    List *run=*theBase;
    while(run->next != NULL)
        run=run->next;
    return run;
}

void clearList(List **theBase)
{
    List *run=(*theBase)->next;
    free(*theBase);
    while(run != NULL)
    {
        *theBase=run;
        run=run->next;
        free(*theBase);
    }
    *theBase=NULL;
}

int main(void)
{
   List *theList=NULL;
   int valA=10;
   float valB=1.25;
   double valC=23.45;
   char valD='C';

   theList=create_list(&valA,INTEGER);

   insertion(getEnd(&theList),&valB,FLOAT);
   insertion(getEnd(&theList),&valC,DOUBLE);
   insertion(getEnd(&theList),&valD,CHAR);

   showTheList(&theList);

   clearList(&theList);
   if(theList == NULL)
     printf("Ok, all operations realized !");

   return 0;
}

关于c - C 中的通用结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46869384/

相关文章:

c - 访问返回的二叉树的内容时出现问题

c - 在另一个字符串中查找字符串

c - TAILQ_INSERT_TAIL 宏 <sys/queue.h>

c - CUDD 的 bool 表达式解析器

使用函数更改指针包含的地址

c - 如何让链表的最后一个节点参与冒泡排序?

c++ - 2 个指向相同值的指针并仅更改其中一个 - 双链表 C++

c++ - 为什么我不能打印多个字符串?

c - 如何根据字符串删除链表中的节点

linked-list - 二叉树遍历Inorder输出错误为什么?