【420】链表实现Quack
quack.h
1 2 3 4 5 6 7 8 9 10 11 12 13 | // quack.h: an interface definition for a queue/stack #include <stdio.h> #include <stdlib.h> typedef struct node *Quack; Quack createQuack( void ); // create and return Quack void push( int , Quack); // put the given integer onto the top of the quack void qush( int , Quack); // put the given integer onto the bottom of the quack int pop(Quack); // pop and return the top element on the quack int isEmptyQuack(Quack); // return 1 is Quack is empty, else 0 void makeEmptyQuack(Quack); // remove all the elements on Quack void showQuack(Quack); // print the contents of Quack, from the top down |
quackLL.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | // quackLL.c: a linked-list-based implementation of a quack #include "quack.h" #include <limits.h> // INT_MAX means largest int // if using this value, you need to include <limits.h> #define HEAD_DATA INT_MAX // dummy data struct node { int data; struct node *next; }; // create a null head Quack createQuack( void ) { // Actually this head is NULL, it doesn't store // meaningful data, only store the address. Quack head; head = malloc ( sizeof ( struct node)); if (head == NULL) { fprintf (stderr, "Out of memory\n" ); exit (1); } head->data = HEAD_DATA; head->next = NULL; // return the address of head return head; } // push a new node between head and 1st node void push( int data, Quack qs) { Quack newnode; // determine whether this quack is NULL or not if (qs == NULL) { fprintf (stderr, "push: quack not initialised\n" ); } else { newnode = malloc ( sizeof ( struct node)); if (newnode == NULL) { fprintf (stderr, "push: no memory, aborting\n" ); exit (1); } newnode->data = data; newnode->next = qs->next; qs->next = newnode; } return ; } // pop the 1st node int pop(Quack qs) { // store data in variable retval int retval = 0; if (qs == NULL) { fprintf (stderr, "pop: quack not initialised\n" ); } else { // if qs is empty, you can't pop any nodes if (isEmptyQuack(qs)) { fprintf (stderr, "pop: quack underflow\n" ); } else { Quack pop_node = qs->next; retval = pop_node->data; // link head with 2nd node qs->next = qs->next->next; free (pop_node); pop_node = NULL; } } return retval; } // pop all nodes in qs void makeEmptyQuack(Quack qs) { if (qs == NULL) { fprintf (stderr, "makeEmptyQuack: quack not initialised\n" ); } else { while (!isEmptyQuack) { pop(qs); } } return ; } // only head, head link to NULL int isEmptyQuack(Quack qs) { if (qs == NULL) { fprintf (stderr, "makeEmptyQuack: quack not initialised\n" ); } else { if (qs->next == NULL) { return 1; } } return 0; } // print all nodes void showQuack(Quack qs) { if (qs == NULL) { fprintf (stderr, "makeEmptyQuack: quack not initialised\n" ); } else { // ??? maybe head is corrupted by opertions if (qs->data != HEAD_DATA) { fprintf (stderr, "showQuack: linked list head corrupted\n" ); } else { printf ( "Quack: " ); if (qs->next == NULL) { printf ( "<< >>\n" ); } else { printf ( "<<" ); Quack node = qs->next; while (node->next != NULL) { printf ( "%d " , node->data); node = node->next; } printf ( "%d " , node->data); printf ( ">>\n" ); } } } return ; } |
clientQuack.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include "quack.h" int main() { Quack qs = createQuack(); push(1, qs); showQuack(qs); push(2, qs); showQuack(qs); push(3, qs); showQuack(qs); push(4, qs); showQuack(qs); pop(qs); showQuack(qs); pop(qs); showQuack(qs); pop(qs); showQuack(qs); pop(qs); showQuack(qs); } |
运行下面命令实现编译
1 2 3 4 5 6 7 8 9 | alex@ubuntu:Day01$ gcc quackLL.c clientQuack.c && . /a .out Quack: <<1 >> Quack: <<2 1 >> Quack: <<3 2 1 >> Quack: <<4 3 2 1 >> Quack: <<3 2 1 >> Quack: <<2 1 >> Quack: <<1 >> Quack: << >> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)