Chinaunix首页 | 论坛 | 博客
  • 博客访问: 434873
  • 博文数量: 103
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 971
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-11 17:22
文章分类
文章存档

2008年(77)

2007年(26)

我的朋友

分类: C/C++

2008-05-10 10:44:29

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

typedef struct node{
        int date;
        struct node *next;
}node;

struct node *create(int n)
{
        node *head = NULL, *tail = NULL;
        node *p = NULL;
        int i;

        for(i = 0; i < n; i++){
                p = (node *)malloc(sizeof(node));
                p->date = i;
                p->next = NULL;

                if(head == NULL){
                        head = p;
                }
                else{
                        tail->next = p;
                }
                tail = p;
        }

        return head;
}

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

        for(p; p != NULL; p = p->next){
                printf("%d ", p->date);
        }
        printf("\n");
}

node *invert(node *head)
{
        node *p = NULL, *q = NULL;//通常逆转线性表都是分情况讨论,

//比如空表,一个结点的情况,多加了一个q,就只有一种情况了


        while(head != NULL){
                q = p;
                p = head;//操作顺序是q-p-head,保持这个顺序向后移动

                head = head->next;//一直移到head = NULL

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

int main(int argc, char **argv)
{
        node *head = create(10);

        print(head);
        head = invert(head);
        print(head);

        return 0;
}

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

上一篇:字符串匹配算法

下一篇:后缀树/后缀数组

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