头插法创建链表并且输出

来源:互联网 发布:网络口碑营销实例 编辑:程序博客网 时间:2024/06/05 20:26
1)#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct Node{char data;struct Node *next;}Node,*LinkList;LinkList InitList(LinkList L){L=(LinkList)malloc(sizeof(Node));(L)->next=NULL;return L;    //要返回值}void CreateFromHead(LinkList L){Node *s;char ch;printf("输入链表:");ch=getchar();while(ch!='@'){s=(Node *)malloc(sizeof(Node));s->data=ch;s->next=L->next;L->next=s;ch=getchar();}}void InputList(LinkList L){   L=L->next;while(L!=NULL){printf("%c",L->data);L=L->next;}}int main(){LinkList T=NULL;T=InitList(T);CreateFromHead(T);InputList(T);system("pause");return 0;}2)#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct Node{char data;struct Node *next;}Node,*LinkList;void CreateFromHead(LinkList L){Node *s;char ch;printf("输入链表:");ch=getchar();while(ch!='@'){s=(Node *)malloc(sizeof(Node));s->data=ch;s->next=L->next;L->next=s;ch=getchar();}}void InputList(LinkList L){   L=L->next;while(L!=NULL){printf("%c",L->data);L=L->next;}}int main(){LinkList T=NULL;T=(LinkList)malloc(sizeof(Node));T->next=NULL;CreateFromHead(T);InputList(T);system("pause");return 0;}3)#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct Node{char data;struct Node *next;}Node,*LinkList;LinkList InitList(LinkList *L){*L=(LinkList)malloc(sizeof(Node));(*L)->next=NULL;}void CreateFromHead(LinkList L){Node *s;char ch;printf("输入链表:");ch=getchar();while(ch!='@'){s=(Node *)malloc(sizeof(Node));s->data=ch;s->next=L->next;L->next=s;ch=getchar();}}void InputList(LinkList L){   L=L->next;while(L!=NULL){printf("%c",L->data);L=L->next;}}int main(){LinkList T=NULL;InitList(&T);     //要传递的是地址!CreateFromHead(T);InputList(T);system("pause");return 0;}

原创粉丝点击