用头插、尾插、按顺序插入创建一个不带头节点的链表,栈的基本操作

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <time.h>  
  4.   
  5. #define N 10  
  6.   
  7. typedef struct Node  
  8. {  
  9.     int data;  
  10.     struct Node *next;  
  11. }Node, *pNode;  
  12.   
  13. /*尾插法*/  
  14. void create_list_rear(pNode *h)  
  15. {  
  16.     srand(time(NULL));  
  17.     pNode p, q;  
  18.     p = q = *h = (pNode)calloc(1,sizeof(Node));  
  19.     p->next = NULL;  
  20.     int count = 0;  
  21.     while (count!=N){  
  22.         ++count;  
  23.         if (count == 1){  
  24.             p->data = rand()%100;  
  25.             printf("%d ", p->data);  
  26.         }  
  27.         else{  
  28.             p = (pNode)calloc(1, sizeof(Node));  
  29.             p->data = rand() % 100;  
  30.             printf("%d ",p->data);  
  31.             p->next = NULL;  
  32.             q->next = p;  
  33.             q = p;  
  34.         }  
  35.     }  
  36.     printf("\n");  
  37. }  
  38.   
  39. /*头插法*/  
  40. void create_list_front(pNode *h)  
  41. {  
  42.     pNode p;  
  43.     p = *h = (pNode)calloc(1,sizeof(Node));  
  44.     p->next = NULL;  
  45.     int count = 0;  
  46.     while (count != N){  
  47.         ++count;  
  48.         if (count == 1){  
  49.             p->data = rand() % 100;  
  50.             printf("%d ", p->data);  
  51.         }  
  52.         else {  
  53.             p = (pNode)calloc(1,sizeof(Node));  
  54.             p->data = rand() % 100;  
  55.             printf("%d ", p->data);  
  56.             p->next = *h;  
  57.             *h = p;  
  58.         }  
  59.     }  
  60.     printf("\n");  
  61. }  
  62. /*顺序插入法*/  
  63. void create_list_sequence(pNode *h)  
  64. {  
  65.     pNode p, q, r=NULL;  
  66.     p = q = *h = (pNode)calloc(1,sizeof(Node));  
  67.     p->next = NULL;  
  68.     int count = 0;  
  69.     while (count != N){  
  70.         ++count;  
  71.         if (count == 1){  
  72.             p->data = rand()%100;  
  73.             printf("%d ", p->data);  
  74.         }  
  75.         else{             
  76.             r = (pNode)calloc(1,sizeof(Node));  
  77.             r->data = rand() % 100;  
  78.             printf("%d ", r->data);  
  79.             p = q = *h;  
  80.             while (p ->next != NULL && p->data < r->data ){  
  81.                 q = p;  
  82.                 p = p->next;  
  83.             }  
  84.             if (p->data >= r->data){  
  85.                 if (p == q){  
  86.                     r->next = *h;  
  87.                     *h = r;  
  88.                 }  
  89.                 else {  
  90.                     r->next = p;  
  91.                     q->next = r;  
  92.                 }  
  93.             }  
  94.             else{  
  95.                 p->next = r;  
  96.                 r->next = NULL;  
  97.             }  
  98.         }  
  99.     }  
  100.     printf("\n");  
  101. }  
  102.   
  103. void printList(pNode h)  
  104. {  
  105.     while (h != NULL){  
  106.         printf("%d ",h->data);  
  107.         h = h->next;  
  108.     }  
  109.     printf("\n");  
  110. }  
  111.   
  112. int main()  
  113. {  
  114.     pNode List = NULL;  
  115.     /*尾插法*/  
  116.     printf("尾插法:原数列的顺序为:\n");  
  117.     create_list_rear(&List);  
  118.     printf("链表的顺序为:\n");  
  119.     printList(List);  
  120.   
  121.     /*头插法*/  
  122.     printf("头插法:原数列的顺序为:\n");  
  123.     create_list_front(&List);  
  124.     printf("链表的顺序为:\n");  
  125.     printList(List);  
  126.   
  127.     /*顺序插法*/  
  128.     printf("顺序插法:原数列的顺序为:\n");  
  129.     create_list_sequence(&List);  
  130.     printf("链表的顺序为:\n");  
  131.     printList(List);  
  132.     return 0;  
  133. }  

栈的存储结构有两种:一种是线性栈,一种是链式栈。下面分别是这两种存储结构的实现。

线性栈:

 

[cpp]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #define STACK_INIT_SIZE 10  
  2. #define STACK_REALLOCATION 2  
  3.   
  4. typedef int ElemType;  
  5.   
  6. typedef struct SqStack  
  7. {  
  8.     ElemType *base;  
  9.     int top;  
  10.     int stacklength;  
  11. }SqStack;  
  12.   
  13. void InitSqStack(SqStack *S)  
  14. {  
  15.     S->base = (ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));  
  16.     if (!S->base)  
  17.         exit(1);  
  18.     S->top = 0;  
  19.     S->stacklength = STACK_INIT_SIZE;  
  20. }  
  21.   
  22. void Push_SqStack(SqStack *S,ElemType e)  
  23. {  
  24.     if (S->top == S->stacklength){  
  25.         S->base = (ElemType*)realloc(S->base,(S->stacklength  
  26.   
  27. +STACK_REALLOCATION)*sizeof(ElemType));  
  28.         if (!S->base)  
  29.             exit(1);  
  30.         S->stacklength += STACK_REALLOCATION;  
  31.     }  
  32.     S->base[S->top] = e;  
  33.     ++S->top;  
  34. }  
  35.   
  36. int Pop_SqStack(SqStack *S, ElemType *e)  
  37. {  
  38.     if (S->top == 0)  
  39.         return 0;  
  40.     --S->top;  
  41.     *e = S->base[S->top];  
  42.     return 1;  
  43. }  
  44.   
  45. int GetTopElem_SqStack(SqStack *S, ElemType *e)  
  46. {  
  47.     if (S->top == 0)  
  48.         return 0;  
  49.     *e = S->base[S->top-1];  
  50.     return 1;  
  51. }  
  52.   
  53. int isSqStackEmpty(SqStack S)  
  54. {  
  55.     if (S.top == 0)  
  56.         return 1;  
  57.     else  
  58.         return 0;  
  59. }  
  60.   
  61. void SqStack_test()  
  62. {  
  63.     SqStack S;  
  64.     InitSqStack(&S);  
  65.     ElemType e;  
  66.     printf("Please input some numbers(Ctrl + Z to end).\n");  
  67.     while (scanf("%d", &e) != EOF)  
  68.         Push_SqStack(&S,e);  
  69.     printf("The out stack sequence is:\n");  
  70.     while (!isSqStackEmpty(S)){  
  71.         Pop_SqStack(&S,&e);  
  72.         printf("%d ",e);  
  73.     }  
  74.     printf("\n");  
  75. }  
  76.   
  77. int main()  
  78. {  
  79.     SqStack_test();  
  80.     return 0;  
  81. }  



 


链式栈:


 

[cpp]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. #define STACK_INIT_SIZE 10  
  5. #define STACK_REALLOCATION 2  
  6.   
  7. typedef int ElemType;  
  8.   
  9. typedef struct LinkStack  
  10. {  
  11.     ElemType data;  
  12.     struct LinkStack *next;  
  13. }StackNode,*StackPNode;  
  14.   
  15. StackPNode InitLinkStack()  
  16. {  
  17.     StackPNode head = (StackPNode)malloc(sizeof(StackNode));  
  18.     if (!head)  
  19.         exit(1);  
  20.     head->next = NULL;  
  21.     head->data = 0;  
  22.     return head;  
  23. }  
  24.   
  25. void Push_LinkStack(StackPNode head, ElemType e)  
  26. {  
  27.     StackPNode node = (StackPNode)malloc(sizeof(StackNode));  
  28.     if (!node)  
  29.         exit(1);  
  30.     node->data = e;  
  31.     node->next = NULL;  
  32.     node->next = head->next;  
  33.     head->next = node;  
  34. }  
  35.   
  36. int Pop_LinkStack(StackPNode head, ElemType *e)  
  37. {  
  38.     StackPNode p;  
  39.     if (!head->next)  
  40.         return 0;  
  41.     //StackPNode p;  
  42.     //StackPNode p;  
  43.     //struct LinkStack *p;  
  44.     p= head->next;  
  45.       
  46.     *e=p->data;  
  47.     head->next = p->next;  
  48.     free(p);  
  49.     return 1;  
  50. }  
  51.   
  52. int isLinkStackEmpty(StackPNode head)  
  53. {  
  54.     if (head->next)  
  55.         return 0;  
  56.     else  
  57.         return 1;  
  58. }  
  59.   
  60. int getTopElem_LinkStack(StackPNode head,ElemType *e)  
  61. {  
  62.     if (head->next)  
  63.         return 0;  
  64.     *e = head->next->data;  
  65.     return 1;  
  66. }  
  67.   
  68. void LinkStack_test()  
  69. {  
  70.     StackPNode head = InitLinkStack();  
  71.     ElemType e;  
  72.     printf("Please input some numbers(Ctrl + Z to end).\n");  
  73.     while (scanf("%d", &e) != EOF)  
  74.         Push_LinkStack(head,e);  
  75.     printf("The out stack sequence is:\n");  
  76.     while (!isLinkStackEmpty(head)){  
  77.         Pop_LinkStack(head, &e);  
  78.         printf("%d ", e);  
  79.     }  
  80.     printf("\n");  
  81. }  
  82.   
  83. int main()  
  84. {  
  85.     LinkQueueTest();  
  86.     return 0;  
  87. }  


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值