[数据结构]链栈的创建,入栈和出栈

栈是一种在栈顶压入和弹出的数据结构,所以只在一端进行操作.为了减小遍历开支,所以链栈一般在首元节点处进行插入(入栈).

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>


typedef struct Node {
    int data;
    struct Node* next;

}Node;
Node* pushStack(Node* , int); 
void print_Stack(Node* );
Node* popStack(Node* ptr,int* popvalue);
int main() 
{
    Node* ptr = NULL; 
    int value = 0,popvalue=0;

    /*数据进行入栈*/
    for (int i = 0; i < 10; i++) {
        value = 10 * i + 10;
        ptr=pushStack(ptr, value);
    }
    print_Stack(ptr);    
    ptr=popStack(ptr,&popvalue);
    printf("popvalue=%d\n",popvalue);
    print_Stack(ptr);
    
    return 0;
}
/*执行压栈操作*/
Node* pushStack(Node* ptr, int pushvalue) 
{
    if (ptr == NULL)
    {
        Node* newNode = (Node*)malloc(sizeof(Node));
        ptr = newNode; newNode->next = NULL; newNode->data = pushvalue;
    }
    else {
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->data = pushvalue;
        newNode->next = ptr;
        ptr = newNode;
    }
    return ptr;
}

void print_Stack(Node* ptr)
{
    Node* str=ptr;
    while (str->next!=NULL) 
    {
        printf("%d\n", str->data);
        str = str->next;
    }
    printf("%d\n", str->data);
}
/*执行出栈操作*/
Node* popStack(Node* ptr,int* popvalue)
{    
    Node* delete_ptr=NULL;
    *popvalue=ptr->data;
    delete_ptr=ptr;
    ptr=ptr->next;
    free(delete_ptr);
    delete_ptr=NULL;    
    return ptr;
}

出栈返回栈顶数据代码:

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>


typedef struct Node {
    int data;
    struct Node* next;
}Node;
Node* pushStack(Node* , int); 

void print_Stack(Node* );
int popStack(Node** ptr,int popvalue);

int main() 
{
    Node* ptr = NULL; 
    int value = 0,popvalue=0;
    /*数据进行入栈*/
    for (int i = 0; i < 10; i++) {
        value = 10 * i + 10;
        ptr=pushStack(ptr, value);
    }
    print_Stack(ptr);    
    popvalue=popStack(&ptr,popvalue);
    printf("popvalue=%d\n",popvalue);
    print_Stack(ptr);
    return 0;
}

/*执行压栈操作*/
Node* pushStack(Node* ptr, int pushvalue) 
{
    if (ptr == NULL)
    {
        Node* newNode = (Node*)malloc(sizeof(Node));
        ptr = newNode; 
        newNode->next = NULL;
        newNode->data = pushvalue;
    }
    else {
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->data = pushvalue;
        newNode->next = ptr;
        ptr = newNode;
    }
return ptr;
}

void print_Stack(Node* ptr)
{
    Node* str=ptr;
    while (str->next!=NULL) {
        printf("%d\n", str->data);
        str = str->next;
    }
    printf("%d\n", str->data);
}

/*进行出栈操作*/
int popStack(Node** ptr,int popvalue)
{   
    Node* delete_ptr=NULL;
    popvalue=(*ptr)->data;
    delete_ptr=*ptr;
    *ptr=((*ptr)->next);
    free(delete_ptr);
    delete_ptr=NULL;    
    return popvalue;
}

修改为具有头指针的形式

相关推荐
zander2581 小时前
138. 随机链表的复制
数据结构·算法·链表
过期动态1 小时前
【LeetCode 热题 100】找到字符串中所有字母异位词
java·数据结构·算法·leetcode·职场和发展·rabbitmq
来一碗刘肉面1 小时前
栈在递归中的应用
数据结构·算法
皓月斯语1 小时前
程序设计语言的特点
开发语言·数据结构·c++
邪修king2 小时前
C++ 进阶终章:异常机制与智能指针全解 —— 从错误处理到 RAII 资源管理,打通现代 C++ 的核心命脉
android·数据结构·c++
Adios79410 小时前
设置交集大小至少为2
数据结构·算法·leetcode
来一碗刘肉面16 小时前
栈的应用(表达式求值)
数据结构·算法
yuannl1019 小时前
图的深度优先遍历DFS
数据结构
青山木1 天前
Hot 100 --- 岛屿数量
java·数据结构·算法·leetcode·深度优先·广度优先
不会就选b1 天前
算法日常・每日刷题--<归并排序>1
数据结构·算法