分享:基本链栈操作
											看到论坛有个进制转换的说链栈做,就学了下。具体实现很简单,跟链表没多大区别。 就多了一个栈顶指针,实际上还是操作尾差法的链表。
程序代码:
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
    int data;
    struct node *next;
}List;
typedef struct stack
{
    int count;
    List *top;
}List_stack;
List_stack *Creat_stack()
{
    List_stack *p;
    p = (List_stack *)malloc(sizeof(p));
    if (p == NULL)
        return p;
    p->count = 0;
    p->top = NULL;
    return p;
}
List_stack *Push_stack(List_stack *p, int elem)
{
    List *temp = NULL;
    temp = (List *)malloc(sizeof(List));
    if (p == NULL || temp == NULL)
        return NULL;
    temp->next = p->top;
    p->top = temp;
    temp->data = elem;
    p->count++;
    return p;
}
void Show_stack(List_stack *p)
{
    List *temp = p->top;
    if (p == NULL)
        printf("The static is empty!\n");
    else
        while (temp != NULL)
        {
            printf("%d ", temp->data);
            temp = temp->next;
        }
}
List_stack *Pop_stack(List_stack *p)
{
    if (p == NULL)
    {
        printf("The static is empty!\n");
        return p;
    }
        
    List *temp = p->top;
    p->top = p->top->next;
    free(temp);
    p->count--;
    
    return p;
}
int main()
{
    List_stack *p;
    int i = 1, n, elem;
    p = Creat_stack();
    printf("How many elements add to stack\n");
    scanf("%d", &n);
    printf("Plese inuput %d elements\n", n);
    for (i = 1; i <= n; i++)
    {
        scanf("%d", &elem);
        p = Push_stack(p, elem);
    }
    printf("Static %d elements :\n", n);
    Show_stack(p);
    printf("\nStack count : %d\n", p->count);
    printf("\nHow many stack elements to delete\n");
    scanf("%d", &n);
    printf("Delete %d elements back:", n);
    while (n-- > 0)
        p = Pop_stack(p);
    Show_stack(p);
    printf("\nStack count : %d\n", p->count);
    printf("\nHow many elements add to stack\n");
    scanf("%d", &n);
    printf("Plese inuput %d elements\n", n);
    for (i = 1; i <= n; i++)
    {
        scanf("%d", &elem);
        p = Push_stack(p, elem);
    }
    printf("Add %d elements back\n", n);
    Show_stack(p);
    printf("\nStack count : %d\n", p->count);
    return 0;
}										
					
	


											


	    

	
写得规范很规范哦~~
