Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1548956
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2016-10-28 17:24:02


  1. #include "stdafx.h"
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>

  5. struct node{
  6.    char c;
  7.    struct node *next;
  8. };
  9. typedef struct node *Link;

  10. Link init(Link head){
  11.    Link before;
  12.    Link newnode;
  13.    Link tail;

  14.    int i, num;
  15.    char str[101];
  16.    scanf("%s",str);
  17.    num = strlen(str);

  18.    head->c = str[0];
  19.    head->next = NULL;
  20.    before = head;
  21.    for(i=1;i<num;i++){
  22.       newnode = (Link)malloc(sizeof(struct node));
  23.       if(!newnode)
  24.          return NULL;
  25.       newnode->c = str[i];
  26.       newnode->next = NULL;
  27.       before->next = newnode;
  28.       before = newnode;
  29.    }
  30.    newnode->next = head;
  31.    tail = newnode;
  32.    return tail;
  33. }

  34. void Insert(Link* hd,Link *tl,char ch){
  35.    Link s,p,q;
  36.    p = *hd;
  37.    q = *tl;
  38.    s = (Link)malloc(sizeof(struct node));\
  39.    s->c = ch;

  40.    s->next = p;
  41.    q->next = s;
  42.    q = q->next;
  43.    *tl=q;
  44. }

  45. void Delete(Link *hd,Link *tl){
  46.    Link p,q,r;
  47.    p = *hd;
  48.    r = p;
  49.    q = *tl;
  50.    p = p->next;
  51.    q->next = p;
  52.    free(r);
  53.    *hd = p;
  54.    *tl = q;
  55. }
  56. void print(Link *hd, Link *tl){
  57.    Link p,q;
  58.    p = *tl;
  59.    q = *tl;
  60.    do{
  61.       printf("%c",q->c);
  62.       q = q->next;
  63.    }while(p!=q);
  64. }

  65. int _tmain(int argc, _TCHAR* argv[])
  66. {
  67.    char ch;
  68.    int i;
  69.    int playnum;
  70.    int choose;
  71.    Link head;
  72.    Link tail;
  73.    Link tmp;
  74.    head = (Link)malloc(sizeof(struct node));
  75.    freopen("linklist_i.txt","r",stdin);
  76.    freopen("linklist_o.txt","w",stdout);
  77.    setbuf(stdout,NULL);
  78.    scanf("%d", &playnum);
  79.    tail=init(head);

  80.    for(i=0;i<playnum;i++){
  81.       scanf("%d", &choose);
  82.       if(choose==1){
  83.          scanf(" %c",&ch);
  84.          Insert(&head, &tail, ch);
  85.       }else if(choose==2){
  86.          if(!(tail->next==tail)){
  87.             Delete(&head, &tail);
  88.          }

  89.       }else if(choose==3){
  90.          tail = head;
  91.          head = head->next;
  92.       }
  93.    }

  94.    print(&head, &tail);
  95.    printf("\n");
  96.    while(head!=tail)
  97.    {
  98.       tmp=head;
  99.       head=head->next;
  100.       free(tmp);
  101.    }
  102.    return 0;
  103. }

  104. /************************************************************************\
  105. 学校有个神奇的机器,他里面的数据呈环状排列.机器有以下操作
  106. 1. 往操作位置顺时针的下一位插入一个元素,并将操作位置移到新元素上.
  107. 2. 删掉操作位置顺时针方向的下一位.如果没有数据,不做操作.
  108. 3. 使得操作位置顺时针移动一位.
  109. 现在机器中有一组长度不超过100的字符串数据,我们希望知道对这些数据进行n次操
  110. 作以后的序列情况.(第一次操作从现存数据的最后一位开始)
  111. \************************************************************************/

  112. /* Input
  113. 6 操作次数
  114. adc 初始字符串
  115. 1 a 下面是6次操作
  116. 2
  117. 2
  118. 3
  119. 1 m
  120. 3
  121. */
  122. /* Output
  123. acm
  124. */

阅读(1412) | 评论(0) | 转发(0) |
0

上一篇:20161026 ADV

下一篇:邻接链表及图的遍历

给主人留下些什么吧!~~