栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

7-1 C语言程序设计 实验6-1 单向链表的建立

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

7-1 C语言程序设计 实验6-1 单向链表的建立

本题目要求实现下面三个函数,实现如下功能:

输入若干个正整数,以-1结束,采取向链表中添加节点的方式来建立一个单向链表。
遍历并输出这个单向链表。

向链表尾部添加节点函数:
struct link *AppendNode(struct link *head,int data);
  • head:链表头节点指针,如果head为NULL,则会新建头节点

  • data:要添加的节点data数值

  • 函数返回:添加过节点后的链表头节点指针

遍历显示链表函数:
void DisplyNode(struct link *head);
  • head :链表头节点指针

删除链表内存函数:
void DeleteMemory(struct link *head);
  • head:链表头节点指针

主函数样例:
#include 
#include 

struct link
{
    int data;
    struct link *next;
};

struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);

int main()
{
    char    c;
    int data = 0;
    struct link *head = NULL;      
    while (1)
    {
        scanf("%d",&data);
        if (data==-1)
            break;

        head = AppendNode(head,data);
    }
    DisplyNode(head);        
    DeleteMemory(head);           
    return 0;
}





#include 
#include 

struct link
{
    int data;
    struct link *next;
};

struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);

int main()
{
    char    c;
    int data = 0;
    struct link *head = NULL;      
    while (1)
    {
        scanf("%d",&data);
        if (data==-1)
            break;

        head = AppendNode(head,data);
    }
    DisplyNode(head);        
    DeleteMemory(head);           
    return 0;
}





 

#include 
#include 
struct link
{
    int data;
    struct link *next;
};

struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);

int main()
{
    char    c;
    int data = 0;
    struct link *head = NULL;      
    while (1)
    {
        scanf("%d",&data);
        if (data==-1)
            break;

        head = AppendNode(head,data);
    }
    DisplyNode(head);        
    DeleteMemory(head);           
    return 0;
}
struct link *AppendNode(struct link *head,int data){
    struct link *p=NULL, *q=head;
  
    p=(struct link*)malloc(sizeof(struct link));
    if(p==NULL){
        exit(0);
    }
    if(head==NULL){
        head=p;
    }
    else{
       while(q->next!=NULL){
           q=q->next;
           }
        q->next=p;
    }  
    p->data=data;
    p->next=NULL;
    return head;
}
void DisplyNode(struct link *head){
    struct link*p;
    p=head;
    int i=1;
    while(p!=NULL)
    {
        
        if(i==1){
            printf("%d",p->data);
        }else{
        printf("->%d",p->data);
        }
        i++;
        p=p->next;
    }
}
void DeleteMemory(struct link *head)
{
   struct link *p = NULL;
	struct link *pr = head;
	while(pr!=NULL)
	{
		p = pr;
		pr = pr->next;
		free(p);
	}
}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号