当前位置: 首页 > news >正文

东莞资深网站建设国外域名注册公司

东莞资深网站建设,国外域名注册公司,手机网站模板下载免费,wordpress it在 C 编程语言中#xff0c;可以使用数组或链表实现堆栈。这两种实现都有其优点和注意事项#xff0c;因此让我们探讨这两种方法。 1. 使用数组的堆栈实现#xff1a; 在此实现中#xff0c;我们使用数组来表示堆栈。数组将具有固定大小#xff0c;变量将跟踪堆栈的顶…在 C 编程语言中可以使用数组或链表实现堆栈。这两种实现都有其优点和注意事项因此让我们探讨这两种方法。 1. 使用数组的堆栈实现    在此实现中我们使用数组来表示堆栈。数组将具有固定大小变量将跟踪堆栈的顶部元素。 c     #include stdio.h #define MAX_SIZE 100 int stack[MAX_SIZE]; int top -1; // Function to check if the stack is empty int isEmpty() { return (top -1); } // Function to check if the stack is full int isFull() { return (top MAX_SIZE - 1); } // Function to push an element onto the stack void push(int item) { if (isFull()) { printf(Stack Overflow\n); return; } stack[top] item; } // Function to pop an element from the stack int pop() { if (isEmpty()) { printf(Stack Underflow\n); return -1; // Return an invalid value or handle error appropriately } return stack[top--]; } // Function to get the top element of the stack int peek() { if (isEmpty()) { printf(Stack is empty\n); return -1; // Return an invalid value or handle error appropriately } return stack[top]; } // Function to display the elements of the stack void display() { if (isEmpty()) { printf(Stack is empty\n); return; } printf(Stack elements: ); for (int i top; i 0; i--) { printf(%d , stack[i]); } printf(\n); } // Example usage int main() { push(10); push(20); push(30); display(); // Output: Stack elements: 30 20 10 printf(%d\n, pop()); // Output: 30 printf(%d\n, peek()); // Output: 20 display(); // Output: Stack elements: 20 10 return 0; } 2. 使用链表的堆栈实现    在此实现中链表用于表示堆栈。链表的每个节点都包含数据和指向下一个节点的指针。 c #include stdio.h #include stdlib.h struct Node { int data; struct Node* next; }; struct Node* top NULL; // Function to check if the stack is empty int isEmpty() { return (top NULL); } // Function to push an element onto the stack void push(int item) { struct Node* newNode (struct Node*)malloc(sizeof(struct Node)); if (newNode NULL) { printf(Memory allocation failed\n); return; } newNode-data item; newNode-next top; top newNode; } // Function to pop an element from the stack int pop() { if (isEmpty()) { printf(Stack Underflow\n); return -1; // Return an invalid value or handle error appropriately } struct Node* temp top; int item temp-data; top top-next; free(temp); return item; } // Function to get the top element of the stack int peek() { if (isEmpty()) { printf(Stack is empty\n); return -1; // Return an invalid value or handle error appropriately } return top-data; } // Function to display the elements of the stack void display() { if (isEmpty()) { printf(Stack is empty\n); return; } printf(Stack elements: ); struct Node* current top; while (current ! NULL) { printf(%d , current-data); current current-next; } printf(\n); } // Example usage int main() { push(10); push(20); push(30); display(); // Output: Stack elements: 30 20 10 printf(%d\n, pop()); // Output: 30 printf(%d\n, peek()); // Output: 20 display(); // Output: Stack elements: 20 10 return 0; } 这两种实现都提供了堆栈的基本操作“push”用于添加元素“pop”用于删除顶部元素“peek”用于检索顶部元素而不删除它“display”用于打印堆栈的元素。 在基于数组的实现中我们使用固定大小的数组并使用“top”变量跟踪顶部元素。函数“isEmpty”和“isFull”分别检查堆栈是空的还是满的以处理潜在的错误。“push”函数通过递增“top”并将值分配给数组中的相应索引来向堆栈添加元素。pop 函数通过递减 top 并从数组中返回值来删除 top 元素。“peek”函数在不修改堆栈的情况下返回顶部元素的值。“display”函数以相反的顺序遍历元素并打印它们。 在链表实现中我们为链表的节点定义了一个结构其中包含数据和指向下一个节点的指针。“top”指针指向堆栈中的第一个节点。“isEmpty”函数检查“top”指针是否为“NULL”以确定堆栈是否为空。“push”函数创建一个新节点分配数据并更新“next”指针以指向上一个顶部节点。“pop”函数删除顶部节点更新“top”指针并返回数据。“peek”函数返回顶部节点的数据而不修改堆栈。“display”函数遍历链表并打印每个节点的数据。 请记住根据特定要求适当地处理错误情况例如堆栈溢出或下溢。
http://www.zqtcl.cn/news/669278/

相关文章:

  • 达内培训网站开发金融公司网站 html
  • 珠海网站制作推荐微信营销和微博营销的区别
  • 电影网站如何做5网站建设公司
  • 河南网站优化公司哪家好南山网站设计线
  • 网站构建代码模板番禺网站建设
  • 拟一份饰品网站建设合同网站开发应注意哪些问题
  • 芜湖建站公司做网站的人多吗
  • 网站怎么加二级域名微信授权登录网站退出怎么做
  • 如何把旅行社网站做的好看网站创建方案怎么写
  • 织梦网站图标更换宠物网页设计图片
  • 如何查找网站竞争对手的宣传方式北京网站搭建公司电话
  • 北京正规制作网站公司wordpress 获取图片地址
  • 大学路网站建设推广图片素材
  • wordpress 创建网站搜索引擎优化代理
  • 设计网站用什么软件盈江城乡建设局网站
  • 网站建设模式有哪些内容seo品牌
  • 衡水做网站服务商济南如何挑选网站建设公司
  • 全屏的网站制作企业网站欢迎界面素材
  • 视频网站切片怎么做网站建设可自学吗
  • 本地推广平台网站seo优化如何做
  • 网站建设费算费用还是固定资产百度秒收录
  • 企业建站系统营销吧tt团队韩国企业网站设计
  • 上海嘉定网站建设公司有没有知道网址的
  • 电商网站的银行支付接入该怎么做杭州微信小程序外包
  • 余姚网站推广策划案门户网站做等保需要备案哪些
  • 网站关键字优化公司wordpress制作百度地图xml
  • 网站建设进度总结网站文件权限设置
  • 织梦网站如何做地区分站厦门网站代理
  • 模板做网站优缺点网络营销推广公司获客
  • 如何做网站充值用flash做网站超链接