C language uses void pointer to implement stack

Source: Internet
Author: User

The textbook of data structure Basic course uses C to realize the data structure, the last semester to see other books with C + + implementation when there is no feeling, a contrast found that C + + classes and templates used more convenient than C.

In writing arithmetic calculator, int write once, Char also have to write again feel very painful, Baidu a bit there is no other solution, found the following this article.

How to use stacks and inverse polish expressions for math arithmetic: http://www.xuebuyuan.com/1602553.html

The key is to understand that the void pointer differs from the generic pointer in that it only holds the first address of the data, and does not know or care how many bytes the data has.

Thus, memcpy can be used to assign values regardless of the data type, which allows us to create multiple types of stacks.

Because the memcpy parameter is a void pointer, both push and pop accept the address as an argument and apply the change directly to the corresponding memory unit and do not return a value.

In addition, the top pointer implemented here is also different from the general stack definition, where the top pointer always points to the next available memory unit.

Modeled after the implementation of a stack is as follows:

1 //stack.h2 //This stack was implemented by void pointer, so it was GENERIC.3 //4 //How the void pointer allows assignments of different data types:5 //First , a normal pointer is binded to a data type, so it knows the number of bytes of the6 //data it points to. Second, it had an address, and which is the start-address of the data.7 //So they can tell the compiler where to start and where to stop accessing the data.8 //9 //But void pointer are binded to no data type. It's only knows the starting-address.Ten //so it can is forcely assigned to any pointers, thus allowing assignments of all data types. One //So, we do is:when we create the stack, we MALLOC a block of free space, and we take a A //Integer, the typesize, which specifies the length of the data. - //We Use the function MEMCPY, which directly access the Addresses,to push the data in the stack - //and pop data out of the stack. the // - //The difference from the definiton: - //The stack pointer here are different from the definition. - //When the stack is empty, Top = = base. + //When a element is pushed in, Top points to the address after the last byte of the element. - //So the Top is points to the next available space. +  A#include <stdio.h> at#include <stdlib.h> -  - structRecord - { -     void*Top; -     void*Base; in     intstacksize; -size_ttypesize; to }; +  -typedefstructRecord *Stack; the  *Stack Createstack (intstacksize, size_t typesize); $ voidPush (Stack S,void*data);Panax Notoginseng voidPop (Stack S,void*data); - intIsEmpty (Stack S); the voidClear (Stack S); + voidDestroy (Stack S);
//stack.c#include"stack.h"#include<string.h>Stack Createstack (intstacksize, size_t typesize) {Stack S= (Stack) malloc (sizeof(structRecord)); S-Base= malloc (stacksize*typesize); if(! S->Base) {printf ("Out of space.\n"); Exit (1); } S->top = s->Base; S->stacksize =stacksize; S->typesize =typesize; returnS;}voidPush (Stack S,void*data) {    if( (int) (s->top-s->Base) + s->typesize > s->stacksize) {printf ("Out of space to push.\n"); Exit (1); } memcpy (S->top, data, s->typesize); S->top = (void*)( (int) S->top + s->typesize);} //GNU defines the arithmetic of void* equals to char (which is defined as byte). //ANSI says we cannot do arithmetic on void* because we don ' t know the type. //As we update the TOP pointer, first regard it as an int, and convert it//Back when we is finished.voidPop (Stack S,void*data) {    if(S->top = = s->Base) {printf ("error:popping an empty stack.\n"); Exit (1); } S->top = (void*)( (int) s->top-s->typesize); memcpy (data, S->top, s->typesize);}voidClear (Stack s) {s->top = s->Base;}intIsEmpty (Stack S) {returnS->top = = s->Base;}voidDestroy (Stack s) {free (s-Base);}

C language uses void pointer to implement stack

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.