一:解密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;
}
*/