C data link stack structure (create, stack, the stack, taking the top element, the element stack traversing the chain)

/ *
* Create link stack
* Create pointer represents a top head pointer
* Storage Structure using
* create using interpolation header list
* create a stack push operation to take the top element
structure created data field *
* create a data field name pointer
* function using random number data field assignment
* /

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define the OK. 1
#define ERROR 0
#define NAMESIZE the maximum length of the string 255 //
int count = 0; // number of the chain of data elements stack
typedef int idType; // data field data type
typedef char * NameType; // data type of data field names
typedef int statu; // function return value data type
typedef struct ElementType
{
idtype ID;
NameType name; // pointer to data field name
} the ElementType;
typedef struct linkstack
{
the ElementType * data; // pointer to data field
struct linkstack * next; // pointer to point to the next node


} Link_Stack; // link stack structure of
Statu create_linkstack (Link_Stack * top); // Create a link stack
void Init_linkstack (Link_Stack * top); // initialize the link stack
Statu push_linkstack (Link_Stack * top, ElementType key); / / stack
statu pop_linkstack (Link_Stack * top, ElementType * key); // popped
statu getdata_linkstack (Link_Stack * top, ElementType * data); // get the top element
statu empty_linkstack (Link_Stack * top); // determines whether the stack empty
void Display_linkstack (Link_Stack * top); // through the data stack
void main ()
{
Link_Stack * Top;
the ElementType data; // stack data
ElementType data1; // the element stack
ElementType key; // get top element
Top = (Link_Stack *) the malloc (the sizeof (Link_Stack));
// initialize to link stack
Init_linkstack (Top);
int Result = create_linkstack (Top);
IF (Result == ERROR)
{
the printf ( "creation failed link stack \ n-");
}
the else
{
the printf ( "successfully created link stack \ n-");
the printf ( "number of data elements in the link stack is:% d \ n ", count);

}
IF (empty_linkstack (Top))
the printf ( "link stack is empty \ n-");
the else
the printf ( "link stack is not empty \ n-");
data.name = (NameType) the malloc (the sizeof (char) * NAMESIZE) ;
data.id = RAND () 50%;
the printf ( "Please enter the name of the data stack:");
Scanf ( "% S", data.name);
int = RESULT1 push_linkstack (Top, data);
IF ( == ERROR result1)
printf ( "stack failed \ the n-");
the else
printf ( "stack success \ the n-");
data1.name = (NameType) malloc (sizeof (char) * NAMESIZE);
int result2 = pop_linkstack ( Top, & DATAl);
IF (result2 == ERROR)
{
the printf ( "pop failed \ n-");
}
the else
{
the printf ( "pop success \ n-");
the printf ( "the element stack is [% s, % d] \ n ", data1.name , data1.id);
}
= key.name (NameType) the malloc (the sizeof (char) * NAMESIZE);
int = result3 getdata_linkstack (Top, & Key);
IF (result3 == ERROR)
the printf ( "failed to take the top element \ n-");
the else
{
the printf ( "take the top element success \ n-");
the printf ( "stack elements: [% S,% D] \ n-", key.name, key.id);
}
the printf ( "traversal results in the data stack is \ n-");
Display_linkstack (Top);
}
void Init_linkstack (Link_Stack * Top) // initialize the link stack
{
IF (Top == NULL)
{
the printf (" initialization failed link stack \ n-");
return;
}
the else
{
TOP-> Next = NULL;
}
}
Create statu create_linkstack (Link_Stack * top) // link stack of
{
IF (Top == NULL)
return ERROR;
Link_Stack * S;
name char [NAMESIZE];
S = (Link_Stack *) the malloc (the sizeof (Link_Stack));
IF (S == NULL)
return NULL;
the printf ( "Please enter the name of the data field: \ n-");
the while (Scanf ( " S% "!, name) = the EOF)
{
IF (strcmp (name," ^ ") == 0)
BREAK;
the else IF (S == NULL)
BREAK;
the else
{

S-> Data = (the ElementType *) the malloc (the sizeof (the ElementType));
S-> DATA-> name = (NameType) the malloc (the sizeof (char) * NAMESIZE);
strcpy (S-> DATA-> name, name) ;
S-> DATA-> ID = RAND (30%); // data is a data area pointer pointing arrow so all using
S-> Next TOP- => Next;
TOP-> Next = S;
COUNT ++; // data elements ++
}
S = (Link_Stack *) the malloc (the sizeof (Link_Stack));
}
return the OK;
}
if statu empty_linkstack (Link_Stack * top) // stack is empty judgment
{
iF (TOP-> Next == NULL)
{
return the OK ;
}
the else
{
return ERROR;
}
}
statu push_linkstack (Link_Stack * Top, the ElementType Key) // stack
{
IF (Top == NULL)
return ERROR;
Link_Stack * S;
s=(Link_Stack*)malloc(sizeof(Link_Stack));
s->data=(ElementType*)malloc(sizeof(ElementType));
s->data->name=(NameType)malloc(sizeof(char)*NAMESIZE);
s->data->id=key.id;
strcpy(s->data->name,key.name);
s->next=top->next;
top->next=s;
return OK;

}
Statu pop_linkstack(Link_Stack *top,ElementType *key)//出栈
{
Link_Stack*node;
if(empty_linkstack(top))
{
return ERROR;
}
else
{
node=top->next;
key->id=node->data->id;
strcpy(key->name,node->data->name);
top->next=node->next;
free(node);//释放其内存
}
}
Statu getdata_linkstack(Link_Stack*top,ElementType *data)//取栈顶元素
{
//取栈顶元素
if(empty_linkstack(top))
return ERROR;
else
{
Link_Stack*node;
node=top->next;
data->id=node->data->id;
strcpy(data->name,node->data->name);
return OK;

}
}
Void Display_linkstack (Link_Stack * Top) // stack through the data
{
the ElementType Data; // data out of the stack

// determined whether the stack of empty
IF (empty_linkstack (Top))
return;
the else
{the while (empty_linkstack (Top) =. 1!)
{
Data.name = (NameType) the malloc (the sizeof (char) * NAMESIZE);
IF ( pop_linkstack (Top, & Data))
the printf ( "[% S,% D] \ T", data.name, data.id);
}

}
}

Guess you like

Origin www.cnblogs.com/ly570/p/11069914.html