首页 > 编程知识 正文

栈和队列的作用,计算机中的栈和队列

时间:2023-05-06 14:08:15 阅读:160344 作者:4759

1 .堆栈特殊的线形表(只在表的末尾进行插入和删除操作就进行数据插入的一端称为堆栈顶部删除操作的一端,称为堆栈底部,在1个堆栈中插入新元素(input,input, 或称为stack )从栈顶元素中移除元素(通过在栈顶元素上存储新元素使其成为新的栈顶元素)被称为外栈或外栈,其中去除栈顶元素并使邻近元素成为新的栈顶元素堆栈中的数据元素根据后进先出原则,先进入的数据被推入堆栈底部,最后一个数据在堆栈顶部读取数据时,从堆栈顶部弹出数据。

2 .堆栈实现堆栈的实现一般使用数组或链表,优于数组的结构实现。 因为数组在末尾插入数据的成本很小。

# include stdio.h # include assert.h # include stdlib.h # include stdbool.htypedefintstdatatype; typedefstructstack { ST datatype * a; int top; //个数int capacity; //最大值}ST; //初始化堆栈voidstackinit(ST*PS ) assert ) PS; ps-a NULL; ps-top ps-capacity 0; //voidstackpush(ST*PS,STDataType x ) assert ) PS ); PS-topps-capacity (if ) {int newCapacity ps-capacity 0? 4 : ps-capacity * 2; STdatatype*tmp(STdatatype* ) realloc ) PS-a,sizeof (ST datatype ) ) * newCapacity ); if(tmpnull ) printf ) realloc:n; exit(-1; (}ps-a tmp; ps-capacity newCapacity; }ps-a[ps-top] x; ps-top; //如果检查堆栈为空,则返回非零结果;否则返回0boolstackempty(ST*PS ) (assert ) PS ); return ps-top 0; //voidstackpop(ST*PS ) (assert ) PS ); assert (! sackempty(PS ); PS-top----; //获取堆栈顶部元素STdatatypestacktop(ST*PS ) assert (PS ) ); assert (! sackempty(PS ); return ps-a[ps-top - 1]; //获取堆栈中的有效元素数intstacksize(ST*PS ) assert (PS ) ); return ps-top; //堆栈voidstackdestroy(ST*PS ) assert (PS ) ); free(PS-a ); ps-a NULL; ps-top ps-capacity 0; }void TestStack () ) {ST lin; sackinit(Lin ); 堆栈推送(Lin,1 ); 堆栈推送(Lin,2 ); 堆栈推送(Lin,3 ); 堆栈推送(Lin,4; 堆栈推送(Lin,5; while (! sackempty(Lin ) (printf ) %d,stack top (Lin ) ); 堆栈pop (Lin ); }printf(n ); }int main () {TestStack; 返回0; } 3.在队列的特殊线性表中,在表的前端进行删除操作的表的后端进行插入操作的端被称为在队列的末尾进行删除操作的端。

4 .队列实现队列也通过数组和链表的结构实现链表的结构更好。 因为如果使用数组的结构进行队列输出,则在组的开头输出数据的效率会降低

# include stdio.h # include assert.h # include stdlib.h # include stdbool.htypedefintqdatatype; typedefstructqueuenode { struct queue node * next; q数据类型数据; }QNode; typedef struct Queue{QNode* head; QNode* tail; (}Queue; //初始化队列voidqueueinit(queue*q ) assert ); q-head q-tail NULL; (//如果检测队列是否为空,则返回非零结果,否则返回0intqueueempty ) queue*q ) {assert(q ) q}; 返回q-head null; () ) ) ) ) ) ) /入队到队列的voidqueuepush ) queue*q,QDataType x ) ) assert ) q; qode*newnode(qnode* ) malloc (sizeof ) qnode ); 新节点空(if )打印) malloc:(n; exit(-1; }newnode-data x; 新节点下一个空值; if(q-tailnull ) {q-head q-tail newnode; }else{q-tail-next newnode; q-tail newnode; () ) ) ) ) ) ) ) ) ) ) ) ) ) )在队伍的开头打出队列voidqueuepop ) queue*q ); assert (! queueempty(q ); if(q-head-nextnull ) free ) q-head; q-head q-tail NULL; }else{QNode* next q-head-next; 自由(q-head ); q-head next; }//获取队列报头元素qdatatypequeuefront(queue*q ) {assert(q ) q }; assert (! queueempty(q ); return q-head-data; //获得队列的最后一个元素qdatatypequeueback(queue*q ) (资产) q ); assert (! queueempty(q ); return q-tail-data; //获取队列中的有效元素数intqueuesize(queue*q ) (assert ) q ); int size 0; QNode* cur q-head; wile(cur ) {size; cur cur-next; }return size; //队列voidqueuedestroy(queue*q ) (资产) q ); QNode* cur q-head; wile(cur ) {QNode* next cur-next; free(cur; cur next; }q-head q-tail NULL; (}int main ) ) {Queue lin; 队列单元(Lin ); 队列推送(Lin,1; 队列推送(Lin,2 ); 队列推送(Lin,3 ); 队列推送(Lin,4; 队列推送(Lin,5; //打印末尾的printf (% d (n,queueback ) Lin ); 从头开始打印while (! queueempty(Lin ) ) printf ) %d,queuefront ) Lin ); 队列pop (Lin ); }printf(n ); 返回0; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。