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

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

文章分类

全部博文(327)

我的朋友

分类: BSD

2008-05-24 13:43:14






  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 head;
  12.    Link before;
  13.    Link newnode;
  14.    Link tail;

  15.    int i, num;
  16.    char str[101];
  17.    //初始字符串的输入
  18.    scanf("%s",str);
  19.    num = strlen(str);

  20. // head = (Link)malloc(sizeof(struct node));
  21. // if(!head)
  22. // return;
  23.    head->c = str[0];
  24.    head->next = NULL;
  25.    before = head;
  26.    //讲初始字符串接入链表
  27.    for(i=1;i<num;i++){
  28.       newnode = (Link)malloc(sizeof(struct node));
  29.       if(!newnode)
  30.          return NULL;
  31.       newnode->c = str[i];
  32.       newnode->next = NULL;
  33.       before->next = newnode;
  34.       before = newnode;
  35.    }
  36.    newnode->next = head;
  37.    tail = newnode;
  38.    return tail;
  39. }

  40. void Insert(Link *L,char ch){
  41.    Link p,s;
  42.    p = *L;
  43.    s = (Link)malloc(sizeof(struct node));

  44.    s->c = ch;
  45.    //
  46.    s->next = p->next;
  47.    p->next = s;
  48.    //指针后移
  49.    p = p->next;
  50.    *L = p;
  51. }

  52. void Delete(Link *L){
  53.    Link p,q;
  54.    p = *L;
  55.    q = p->next;
  56.    p->next = q->next;
  57.    free(q);
  58.    *L = p;
  59. }
  60. void print(Link head, Link tail){
  61.    Link p;
  62.    //p = head;
  63.    tail->next=NULL;
  64.    do
  65.    {
  66.       p = head;
  67.       printf("%c",head->c);
  68.       head = head->next;
  69.       free(p);
  70.    }while(head!=NULL);
  71. }

  72. int _tmain(int argc, _TCHAR* argv[])
  73. {
  74.    char ch;
  75.    int i;
  76.    int playnum;
  77.    int choose;
  78.    Link head;
  79.    Link tail;
  80.    head = (Link)malloc(sizeof(struct node));
  81.    freopen("linklist_i.txt","r",stdin);
  82.    freopen("linklist_o.txt","w",stdout);
  83.    setbuf(stdout,NULL);
  84.    scanf("%d", &playnum);
  85.    tail=init(head);

  86.    for(i=0;i<playnum;i++){
  87.       scanf("%d", &choose);

  88.       if(choose==1){
  89.          scanf(" %c",&ch);
  90.          Insert(&tail,ch);
  91.       }else if(choose==2){
  92.          if(!(tail->next==tail)){
  93.             Delete(&tail);
  94.          }

  95.       }else if(choose==3){
  96.          tail = tail->next;
  97.       }
  98.    }

  99.    print(head,tail);
  100.    printf("\n");
  101.    return 0;
  102. }








出师一表真名世,千载谁堪伯仲间.


阅读(3288) | 评论(4) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-05-28 11:41:22

妈的,这时候还想发国难财,灭了他们!!

chinaunix网友2008-05-27 13:07:55

好!

chinaunix网友2008-05-26 16:13:55

打的确实好,不是一般的好!

chinaunix网友2008-05-26 12:58:38

打得好!