xiaozhu2007
全部博文(103)
2008年(77)
2007年(26)
xiaobo20
cynthia
浪花小雨
GLM89122
Mr_Ran
sun2011y
feel_hyq
tinnal
竟成007
lovebing
分类: 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; }
上一篇:字符串匹配算法
下一篇:后缀树/后缀数组
登录 注册