简体   繁体   English

它是C中的通用堆栈数据结构链接列表实现吗?

[英]Is It A Generic Stack Data Structure Linked List Implementation in C?

My college professor taught us that a generic stack looks something like this (I basically copy-pasted this from the course support files): 我的大学教授告诉我们,通用堆栈看起来像这样(我基本上是从课程支持文件中粘贴出来的):

typedef struct
{ size_t maxe, dime;  
  char *b, *sv, *vf;  
} TStiva, *ASt;

#define DIME(a) (((ASt)(a))->dime)
#define BS(a) (((ASt)(a))->b)
#define SV(a) (((ASt)(a))->sv)
#define VF(a) (((ASt)(a))->vf)
#define DIMDIF(s,d) (DIME(s) != DIME(d))
#define VIDA(a)  (VF(a) == BS(a))
#define PLINA(a) (VF(a) == SV(a))

// Function Declarations
void* InitS(size_t d,...);
int Push(void* a, void* ae);
int Pop (void* a, void* ae);
int Top (void* a, void* ae);

void *InitS(size_t d,...) 
{ ASt a = (ASt)malloc(sizeof (TStiva));
  va_list ap;
  if (!a) return NULL;
  va_start(ap,d);
  a->maxe = va_arg(ap,size_t);  
  va_end(ap);
  a->dime = d;
  a->b = (char*)calloc(a->maxe, d);         
  if (!a->b) { free(a); return NULL; }     
  a->vf = a->b;  
  a->sv = a->b + d * a->maxe; 
  return (void *)a;
}

int Push(void *a, void *ae)  
{ if( PLINA(a)) return 0;
  memcpy (VF(a), ae, DIME(a)); 
  VF(a) += DIME(a);           
  return 1;
}

int Pop(void *a, void *ae)    
{ if(VIDA(a)) return 0;
  VF(a) -= DIME(a);        
  memcpy (ae, VF(a), DIME(a)); 
  return 1;
}

int Top(void *a, void *ae) 
{ if(VIDA(a)) return 0;
  memcpy (ae, VF(a)-DIME(a), DIME(a));
  return 1;
}

Anyway, what this wants to be is a generic stack implementation with vectors , from which I don't understand why in the Top , Push and Pop functions need to refer to the stack data structure as a void * . 无论如何,这是一个带有向量的通用堆栈实现,我不理解为什么在TopPushPop函数中需要将堆栈数据结构称为void *

By generic, doesn't it want to mean that the value the data structure wants to hold is generic ? 用泛型表示,不是要表示数据结构要保留值是泛型吗? This meaning that if you refer to your generic data structure as the typedef instead of void * it doesn't certainly mean that it's not generic. 这意味着,如果您将通用数据结构称为typedef而不是void * ,则不一定意味着它不是通用的。

I am asking this because I am about to create a Generic Stack implemented with Linked Lists and I am a bit confused. 我之所以这样问是因为我要创建一个使用链表实现的通用堆栈,我有点困惑。

This is my generic linked list data structure: 这是我的通用链表数据结构:

typedef struct Element {
    struct Element *next;
    void *value;
} TElement, *TList, **AList;

And for the Stack: 对于堆栈:

typedef struct Stack {
    size_t size;
    TList top;
} TStack, *AStack;

/* Function Definitions */
TStack InitStack(size_t);
void DeleteStack(AStack);
int Push(TStack, void*);
int Pop(TStack, void*);
int Top(TStack, void*);

Does anything seem not generic in my implementation? 在我的实现中似乎没有通用的东西吗?

Generic means that it can hold ANY data type ( char* , int* , etc..), or contain any data type. 通用意味着它可以保存任何数据类型( char*int*等),或包含任何数据类型。 Void pointers void * in C allow you to cast items as such and get those items out(having to re-cast them on retrieval. C空指针void *允许您按原样投射项目并取出这些项目(必须在检索时重新投射它们)。

So, it allows the program to be ignorant of the data types that you have in your custom data structure. 因此,它允许程序忽略自定义数据结构中的数据类型。

Referring to the structure itself(as long as you are not specifying the data that is held in said structure), does not break generalities. 引用结构本身(只要您不指定保存在该结构中的数据),就不会破坏一般性。 So, you can specifically mention your TStack in your functions as long as the data that is manipulated inside of that stack is general( id est void * ). 所以,你可以特别提到你的TStack只要在你的功能是在这个堆栈的内部操纵的数据是一般(ID EST void * )。

void* is for generic purposes. void*是出于一般目的。 Imagine it as a pointer to the memory, where of course the memory can hold anything. 想象一下它是指向内存的指针,当然内存可以容纳任何东西。 By void* you mean that you do not know what you point to, but you know that you point to something. void*是指您不知道所指的是什么,但是您知道您指向的是某物。

Yes a void* can correctly implement a generic stack, but that creates a problem that you have no idea about the type of data you are storing in the Stack. 是的, void*可以正确实现通用堆栈,但是这会产生一个问题,使您对堆栈中存储的数据类型一无所知。 The concept of void* is that it is pointing to some valid block of memory, but there is absolutely no clue as to the type of the memory. void*的概念是它指向某个有效的内存块,但是绝对没有关于内存类型的线索。 So, the code that is using this generic stack has to do type conversion explicitly. 因此,使用此通用堆栈的代码必须显式进行类型转换。 void* are used only to store data, manipulation with them are disallowed. void*仅用于存储数据,不允许对其进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM