IHYPRESS PROGRAMMING
Tutorials and C programs with code and output for beginners
c programming
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
CAdvanced Programming : Stack Data Structure
<12.08>
#include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node *next; } node; node *top; void initialize () { top = NULL; } void push(int value) { node *tmp; tmp = (node *) calloc (1, sizeof(node)); tmp -> data = value; tmp -> next = top; top = tmp; } int pop (void) { node *tmp; int n; tmp = top; n = tmp->data; top = top->next; free (tmp); return (n); } int topvalue (void) { return (top->data); } int isempty (void) { return (top==NULL); } void display (node *head) { if (head == NULL) { printf ("NULL\n"); } else { printf ("[%d] ", head -> data); display (head->next); } } int main (void) { initialize(); push(22); push(33); push(44); printf ("The top is %d\n", topvalue()); pop(); printf("The top after pop is %d\n", topvalue()); display(top); return (0); }
Hergestellt in Deutschland / Made in Germany
The top is 44 The top after pop is 33 [33] [22] NULL
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland