正在加载图片...
#include<stdio. h> struct snode char name[ 15]: truct snode* next: struct snode*create list(int n) struct snode*head, tail,*p head=tail=NULL, printf(请输入姓名:n”) for(i=0; i<n; i++) p=( struct snode*) malloc(sizeof(( struct snode),/为节点分配空间* scanf("%s"p->name p->next=NULL; 新加入的节点链入表尾,故其next指针均置为NULL*/ if(head==NULL) 第一个节点 head=tail=p /*只有一个节点时,head和tail均指向它 /*当前尾节点的next指针指向新节点* /*尾指针向后移指向新节点 return head mal q=create list(5); /*创建一个链表,包含5个节点 printf(生成的字符串链表为:m”); while(q!=NULL) printf("%sIn", ->name);#include<stdio.h> #include<malloc.h> struct snode { char name[15]; struct snode*next; }; struct snode*create_list(int n) { int i; struct snode*head,*tail,*p; head=tail=NULL; printf(“请输入姓名:\n”); for(i=0;i<n;i++) { p=(struct snode*)malloc(sizeof(struct snode)); /*为节点分配空间*/ scanf("%s",p->name); p->next=NULL; /*新加入的节点链入表尾,故其 next 指针均置为 NULL*/ if(head==NULL) /*第一个节点*/ head=tail=p; /*只有一个节点时,head 和 tail 均指向它*/ else { tail->next=p; /*当前尾节点的 next 指针指向新节点*/ tail=p; /*尾指针向后移指向新节点*/ } } return head; } main() { struct snode*q; q=create_list(5); /*创建一个链表,包含 5 个节点*/ printf(“生成的字符串链表为:\n”); while(q!=NULL) { printf("%s\n",q->name); q=q->next; } }
©2008-现在 cucdc.com 高等教育资讯网 版权所有