原帖及讨论:http://bbs.bc-cn.net/dispbbs.asp?boardid=179&id=130712
#include<stdio.h> #include<malloc.h> 逆@风@者
typedef struct List_Node{ int info; struct List_Node *next; }node;//结点结构体
/******************************/ /* 尾插法建立带头结点的单链表 */ /******************************/ node* Creat_Node() { node *head,*pre,*p; int x; head=(node*)malloc(sizeof(node));; head->next=NULL; pre=head; printf("输入各结点的值,以0结束:"); while(EOF!=(scanf("%d",&x))&&x!=0) { p=(node*)malloc(sizeof(node)); p->info=x; p->next=pre->next; pre->next=p; pre=pre->next; } return head; }
/******************************/ /* 头插法建立带头结点的单链表 */ /******************************/ node* Build_Node() { node *head,*p; int x; head=(node*)malloc(sizeof(node));; head->next=NULL; printf("输入各结点的值,以0结束:"); while(EOF!=(scanf("%d",&x))&&x!=0) { p=(node*)malloc(sizeof(node)); p->info=x; p->next=head->next; head->next=p; } return head; }
/******************************/ /* 打印单链表 */ /******************************/
void Print_Node(node *head) { node *p=head->next; printf("输出该链表:"); while(p) { printf("%-5d--->",p->info); p=p->next; } if(p==NULL) { printf("^\n\n\n"); } }
#include"Head_Node.h"
int Count_Node(node *head) { node *p=head->next; int num=0; while(p!=NULL) { num ; p=p->next; } return num; }
int main() { node *head; head=Creat_Node(); Print_Node(head); printf("结点个数为:%d\n",Count_Node(head)); return 0; }
[1][2][3][4][5]下一页
|