• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

一个简单的C语言链表实现

OC/C/C++ 水墨上仙 2591次浏览

一个简单的C语言链表实现

/*
    Simple singly linked lists example usage; it is only one of 
    many possible implementations - try your own enchancements.
*/
 
#include <stdio.h>
#include <stdlib.h>
 
typedef int stack_data; //stack data type - set for integers, modifiable
typedef struct stack Node; //short name for the stack type
struct stack //stack structure format
{
    stack_data data;
    Node *next;
};
 
int stack_len(Node *node_head); //stack length
void stack_push(Node **node_head, stack_data d); //pushes a value d onto the stack
stack_data stack_pop(Node **node_head); //removes the head from the stack & returns its value
void stack_print(Node **node_head); //prints all the stack data
void stack_clear(Node **node_head); //clears the stack of all elements
void stack_snoc(Node **node_head, stack_data d); //appends a node
int stack_elem(Node **node_head, stack_data d); //checks for an element
 
int main(void)
{
    Node *node_head = NULL; //pointer to stack head
 
    //example usage:
    stack_push(&node_head, 7); //push value 7 onto stack
    printf("%d\n", node_head -> data); //show stack head value
    stack_push(&node_head, 21); //push value 21 onto stack
    stack_print(&node_head); //print the stack
    if(stack_elem(&node_head, 7)) puts("found 7"); //does 7 belong to the stack?
    stack_snoc(&node_head, 0); //append 0 to the end of the stack
    stack_print(&node_head); //print the stack
    printf("%d\n", stack_pop(&node_head)); //pop the stack's head
    stack_print(&node_head); //print the stack
    stack_clear(&node_head); //clear the stack
    stack_print(&node_head); //print the stack
     
    getchar();
    return 0;
}
 
int stack_len(Node *node_head)
{
    Node *curr = node_head;
    int len = 0;
     
    while(curr)
    {
        ++len;
        curr = curr -> next;
    }
    return len;
}
 
void stack_push(Node **node_head, stack_data d)
{
    Node *node_new = malloc(sizeof(Node));
     
    node_new -> data = d;
    node_new -> next = *node_head;
    *node_head = node_new;
}
 
stack_data stack_pop(Node **node_head)
{
    Node *node_togo = *node_head;
    stack_data d;
     
    if(node_head)
    {
        d = node_togo -> data;
        *node_head = node_togo -> next;
        free(node_togo);
    }
    return d;
}
 
void stack_print(Node **node_head)
{
    Node *node_curr = *node_head;
     
    if(!node_curr)
        puts("the stack is empty");
    else
    {
        while(node_curr)
        {
            printf("%d ", node_curr -> data); //set for integers, modifiable
            node_curr = node_curr -> next;
        }
        putchar('\n');
    }
}
 
void stack_clear(Node **node_head)
{
    while(*node_head)
        stack_pop(node_head);
}
 
void stack_snoc(Node **node_head, stack_data d)
{
    Node *node_curr = *node_head;
     
    if(!node_curr)
        stack_push(node_head, d);
    else
    {
        //find the last node
        while(node_curr -> next)
            node_curr = node_curr -> next;
        //build the node after it
        stack_push(&(node_curr -> next), d);
    }
}
 
int stack_elem(Node **node_head, stack_data d)
{
    Node *node_curr = *node_head;
     
    while(node_curr)
    {
        if(node_curr -> data == d) //set for numbers, modifiable
            return 1;
        else
            node_curr = node_curr -> next;
    }
    return 0;
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明一个简单的C语言链表实现
喜欢 (0)
加载中……