栈,队列,链表

 

一:解密QQ号——队列

#include<stdio.h>

int main(void)
{
    int n, i, a[1001], head, tail;

    n = 9;

    for(i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }

    head = 0;
    tail = 9;


    for(i = 1; i < n; i++)
    {
        printf("%d ", a[head]);
        head++;

        a[tail] = a[head];

        head++;
        tail++;

    }//注意此时最后一个数还没有输出来

    printf("%d\n", a[head - 1]);

    return 0;
}
 

/*

二:又见回文

代码:

#include<stdio.h>
#include<string.h>

int main(void)
{
   int top, len, mid, i, flag = 1;
   char a[101];

   gets(a);
   len = strlen(a);
   mid = len / 2 - 1;
   top = 0;

   if(len % 2 == 0)
   {
       for(i = len - 1; i > mid; i--)
       {
           if(a[i] != a[top])
           {
               flag = 0;
               break;
           }

           top++;
       }
   }

   else
   {
       for(i = len - 1; i > mid + 1; i--)
       {
           if(a[i] != a[top])
           {
               flag = 0;
               break;
           }

           top++;
       }
   }

   if(flag)
   {
       printf("YES\n");
   }

   else
   {
       printf("NO\n");
   }

   return 0;

}
 

*/

 

/*

三:顺序建立链表

代码:

/*顺序创立链表*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;

}*head, *tail;

struct node *creat(int n)
{
    int i;
    struct node *p;

    head = (struct node *)malloc(sizeof(struct node));
    head-> next = NULL;
    tail = head;

    for(i = 0; i < n; i++)
    {
        p = (struct node *)malloc(sizeof(struct node));
        p-> next = NULL;
        scanf("%d", &p-> data);
        tail-> next = p;;
        tail = p;
    }

    return head;
}

void print(struct node *head)
{
    struct node *p;

    p = head;

    while(p-> next != NULL)
    {
        p = p-> next;
        printf("%d ", p-> data);
    }

}

int main(void)
{
    int n;
    struct node *h;

    scanf("%d", &n);

    h = creat(n);
    print(h);

    return 0;
}
 

*/

 

/*

四:利用链表来插入元素

代码:

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;

}*head, *tail;

struct node *creat(int n)
{
    int i;
    struct node *p;

    head = (struct node *)malloc(sizeof(struct node));
    head-> next = NULL;
    tail = head;

    for(i = 0; i < n; i++)
    {
        p = (struct node *)malloc(sizeof(struct node));
        p-> next = NULL;
        scanf("%d", &p-> data);
        tail-> next = p;;
        tail = p;
    }

    return head;
}

struct node *sert(struct node *head, int a)
{
    struct node *p, *q, *x;
    p = head;
    q = p;

    x = (struct node *)malloc(sizeof(struct node));
    x-> data = a;
    x-> next = NULL;
    while(p-> next != NULL)
    {
        if(p-> next-> data > a)
        {
             x-> next = q-> next;
             q-> next = x;
             break;
        }

        p = p-> next;
        q = p;
    }

    if(p-> next == NULL)
    {
         x-> next = q-> next;
         q-> next = x;
    }

    return head;
}

void print(struct node *head)
{
    struct node *p;

    p = head;

    while(p-> next != NULL)
    {
        p = p-> next;
        printf("%d ", p-> data);
    }

}

int main(void)
{
    int n, a;
    struct node *x, *h;

    scanf("%d", &n);

    h = creat(n);

    scanf("%d", &a);
    x = sert(head, a);
    print(x);

    return 0;
}
 

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值