c语言结构体通风巷道输入风阻,初始节点,末节点,分支编号怎么写
按你的要求写了个,供参考:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 #include "stdio.h"#include "stdlib.h"struct node{ int data; struct node *pNext;};/*建立n个元素的链表*/struct node *CreatLink(int n){ int i; struct node *pHead, *p,*q; if(n<=0)return NULL; pHead=(struct node*)malloc(sizeof(struct node)); if(NULL == pHead) return NULL; q = pHead; q->pNext = NULL; printf(" 创建链表:请输入%d个整数:",n); scanf("%d",&(q->data)); for(i=0;i<n-1;i++) { p=(struct node*)malloc(sizeof(struct node)); if(p == NULL) { printf("alloc error "); exit(1); } else { q->pNext = p; q=p; q->pNext = NULL; scanf("%d",&(q->data)); } } return pHead;} /*输出链表*/int PrintLink(struct node *pHead){ struct node *p,*q; printf(" 输出链表:"); if(NULL == pHead) { printf("空链表!"); return 0; } q = pHead; p=q->pNext; while(NULL != p) { printf("%d -> ",q->data ); q=p; p=q->pNext; } printf("%d .",q->data ); return 0;}/*插入链表,在第 n 个位置 插入元素 Element */int InsertLink(struct node *pHead, int n, int Element ){ int i; struct node *p,*q ; if(NULL == pHead) return 0; q = pHead; p=q->pNext; i=1; printf(" 插入链表,在第 %d 个位置 插入元素 %d .",n, Element); while(i<n && NULL!=p) { q=p; p=q->pNext; i++; } if(i < n) { printf(" 错误,原链表长度小于 %d !",n); return -1; } else { p=(struct node*)malloc(sizeof(struct node)); if(p == NULL) { printf("alloc error "); exit(1); } else { p->data = Element; p->pNext = q->pNext ; q->pNext = p; } } return 0;}/*单向链表演示程序能不能提供一个c语言结构体单向链表插入节点的程序?用户每输入一个数据链表就多一节。希望用malloc,不要用new,不要用typedef,不要附加其他功能,现在有关的程序都看不懂……谢谢!*/int main(){ struct node *head; head = CreatLink(6); PrintLink(head); InsertLink(head,3,1111); PrintLink(head); system("pause"); return 0;}
下一篇:oracle的api怎么下载
上一篇:如何改变音色