逆序/顺序(头插法/尾插法)建链表

逆序建链表(头插法)

//输入五个数,逆序建带头结点的单链表(head)
//举个栗子:11 12 13 14 15    建一个如下图的带头结点的单链表(emmmm图有点丑)

(1)首先定义链表:

struct node 
{
    int data;//数据域
    struct node *next;//指针域
}*head;//链表的头指针head

(2)建立只含有头结点的单链表

head=(struct node *)malloc(sizeof(node));//给头结点申请空间
head->next=NULL;//初始化头结点,指针域next置为空

(3)新建第一个结点,在空链表的头结点后,插入第一个结点

p=(struct node *)malloc(sizeof(struct node)); //为结点分配存储空间
scanf(“%d”,&p->data); //给结点的数据域data赋值
p->next=head->next; //给结点的指针域next赋值,指向head->next
head->next=p; //将结点连接到头结点之后

(4)重复(3),将新建结点插入到头结点head之后,直到输入数据结束

 

完整代码:

#include<stdio.h>
#include<stdlib.h>
struct node//定义链表类型
{
    int data;//数据域
    struct node *next;//指针域
};
struct node *creat1(int n);//逆序建立链表函数,返回指针值
void display(struct node *head);//显示链表中结点值
int main()
{
    int n;//结点个数
    struct node *head;//定义链表头指针
    printf("input node number:\n");
    scanf("%d",&n);
    head=creat1(n);//调用逆序建立链表函数,返回值赋给head
    display(head);//显示链表结点值
    return 0;
}
struct node *creat1(int n)
{
    struct node *head,*p;//头指针和游动指针
    int i;
    //初始化头结点head,分配空间,next域置NULL
    head=(struct node *)malloc(sizeof(node));
    head->next=NULL;
    //循环创建新节点p,并插入到头结点head后面
    for(i=1;i<=n;i++)
    {
        //为新节点分配储存空间
        p=(struct node *)malloc(sizeof(node));
        //读入数据,赋给新节点数据域
        scanf("%d",&p->data);
        //让新节点指针域指向原来头结点后面的结点
        p->next=head->next;
        //让头结点next域指向新节点p
        head->next=p;
    }
    return head;//返回头结点
}
void display(struct node *head)
{
    struct node *p;//定义游动指针
    p=head->next;//让p指向第一个结点
    while(p!=NULL)//当P为空时退出循环
    {
        if(p->next==NULL)//如果已经是最后一个结点,输出结点值后换行
            printf("%d\n",p->data);
        else//否则输出结点值都,输出空格
            printf("%d ",p->data);
        p=p->next;//p指向下一个结点
    }
}

正序建链表(尾插法)

  • 顺序建链表是在最后一个结点之后插入新结点。
  • 最后一个结点——尾结点。
  • 指针变量tail保存尾结点地址——尾指针。
  • 插入结点后尾指针tail的值要随之修改,指向新的尾结点。

(1)建立一个只含有头结点的空链表,头指针,尾指针均指向头结点。.

head=(struct node *)malloc(sizeof(node));
head->next=NULL;
tail=head;

(2)在空链表头结点后面插入第一个节点(画技有限QAQ)

 

p=(struct node *)malloc(sizeof(node));//为新节点分配空间
scanf("%d",&p->data);//赋值
p->next=NULL;
tail->next=p;
tail=p;

顺序建链表代码:

//顺序建链表
#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
};
struct node *create2(int n);
void display(struct node *head);
int main()
{
    int n;
    struct node *head;
    printf("input node number:\n");
    scanf("%d",&n);
    head = create2(n);
    display(head);
    return 0;
}
struct node *create2(int n)
{
    struct node *head,*tail,*p;
    int i;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    for(i=1;i<=n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    return head;
};
void display(struct node *head)
{
    struct node *p;
    p=head->next;
    while(p!=NULL)
    {
        if(p->next==NULL)
            printf("%d\n",p->data);
        else
            printf("%d ",p->data);
        p=p->next;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值