| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 949 人关注过本帖
标题:新人求助。 链表的创建和输出
只看楼主 加入收藏
q572259028
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-7-3
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
新人求助。 链表的创建和输出
程序代码:
#include<stdio.h>
#include<malloc.h>
struct ok{
    int a;
    int b;
    struct ok *next;
}; 
int n;
struct ok *creat(){
    n=0;
    struct ok *p1,*p2,*head;
    p1=p2=(struct ok*)malloc(sizeof(struct ok));
    scanf("%d,%d",&p1->a,&p1->b);
    head=NULL; 
    for(;p1->b!=0;){
        n=n+1;
        if(n==1)head=p1;
        else p2->next=p1;
        
            
        p2=p1;
        p1=(struct ok*)malloc(sizeof(struct ok));
        scanf("%d,%d",&p1->a,&p1->b);
        
    }
    p2->next=NULL;
    return head;
}
void print(struct ok *head){
    struct ok *p=head;
    if(head!=NULL){
    
        do{
        printf("%d,%d\n",p->a,p->b);
        p=p->next;}
    while(p!=NULL);
    }
}
void main(){
    struct ok *head;
    head=creat();
    print(head);
    
    
}
搜索更多相关主题的帖子: 链表 struct next head NULL 
2018-07-03 11:41
星泪成寒
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:6
帖 子:77
专家分:549
注 册:2013-5-19
收藏
得分:10 
程序代码:
#include <stdio.h>
#include <malloc.h>

struct ok {
    int a;
    int b;
    struct ok *next;
};


struct ok *creat()
{
    struct ok *p1,*p2,*head;
   

    head = (struct ok*)malloc(sizeof(struct ok));
    head->next = NULL;
    p2 = head;

    printf("== input data: "); fflush(stdout);
    scanf("%d,%d",&p2->a,&p2->b);
   

    while (p2->b){
        p1 = (struct ok*)malloc(sizeof(struct ok));
        printf("== input data: "); fflush(stdout);
        scanf("%d,%d",&p1->a,&p1->b);
        p1->next = NULL;
        p2->next = p1;
        p2 = p1;
    }
   

    return head;
}

void print(struct ok *head)
{
    while (head) {
        printf("%d,%d\n",head->a,head->b);
        head = head->next;
    }
}

void destroy(struct ok *head)
{
    struct ok * tmp;
   

    while (head) {
        tmp = head;
        head = head->next;
        free(tmp);
    }
}

int main(void)
{
    struct ok *head;

    head = creat();
    print(head);
    destroy(head);

    return 0;
}
2018-07-03 14:20
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:10 
#include <stdio.h>
#include <stdlib.h>

struct ok
{
    int a;
    int b;
    struct ok *next;
};

void *_malloc(size_t size)
{
    void *p = malloc(size);
    if (p == NULL)
    {
        printf("分配内存失败!\n");
        exit(0);
    }
    return p;
}

void _free(struct ok *p)
{
    struct ok *q;
    while (p)
    {
        q = p->next;
        free(p);
        p = q;
    }
}

int _input(struct ok *p)
{
    int ret;
    printf("输入(a,b): ");
    ret = scanf("%d,%d", &p->a,&p->b)==2;
    if (!ret || p->b==0)
        return 0;
    while (getchar() != '\n');
    return 1;
}

struct ok *creat()
{
    struct ok *head, *p, st;
    if (!_input(&st))
        return NULL;
    head = (struct ok *)_malloc(sizeof(struct ok));
    p = head;
    *p = st;
    while (_input(&st))
    {
        p->next = (struct ok *)_malloc(sizeof(struct ok));
        p = p->next;
        *p = st;
    }
    p->next = NULL;
    return head;
}

void print(struct ok *p)
{
    for (; p; p=p->next)
        printf("%d,%d\n",p->a,p->b);
}

void main()
{
    struct ok *head = creat();
    print(head);
    _free(head);
}
2018-07-03 14:37
快速回复:新人求助。 链表的创建和输出
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016830 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved