c 链表实例
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; typedef struct node NODE; typedef struct node * PNODE; PNODE constructlist( PNODE head, int num ); void outlist( PNODE head ); void deleteneg( PNODE head ); int main(int argc, char *argv[]) { printf("Hello, world\n"); int num=1; PNODE head; head = (PNODE)malloc( sizeof(NODE) ); head->next = NULL; head->data = -1; while ( num!=0 ) { scanf("%d", &num); if ( num!=0 ) constructlist (head, num); } deleteneg( head ); outlist( head ); return 0; } PNODE constructlist( PNODE head, int num ) { PNODE p; p = (PNODE)malloc( sizeof(NODE) ); p->data = num; p->next = head->next; head->next = p; return head; } void outlist( PNODE head ) { PNODE p; p = head->next; while ( p != NULL ) { printf("%d\n", p->data); p = p->next; } } //删除所有无效数值结点的函数原型: void deleteneg(PNODE head) { PNODE p,prev; p = head->next; prev = NULL; while ( p != NULL ) { if( p->data < 0) { if(prev == NULL) { head->next = p->next; free(p); p = head->next; } else { prev->next = p->next; free(p); p = prev; p = p->next; } } else { prev = p; p = p->next; } } }