单链表的创建

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node{
       int data;
       struct node *next;
       }Node;
int main()

{
    Node * createNode(int n);
    Node *createNode2(int n);//声明函数
    Node *head,*p;
   p=head=createNode(9);
   for(int i=0;i<9;i++)

  {      
    printf("%d",p->next->data);//       对于头插法的遍历。
    p=p->next;
    }
    p=createNode2(10);
    printf(" 第二轮输出");
    while(p->next!=NULL)

                        {
                          printf("%d",p->data);//对于未插法的遍历。
                          p=p->next;
                          }
   
  
    system("pause");
    return 1;
    }
  Node * createNode(int n)

  {//头插法
       Node *L,*head;
      int i;
      Node *p;
   head=L=(Node *)malloc(sizeof(Node));
      for(i=0;i<n;i++)

                     {
                       p=(Node *  )malloc(sizeof(Node));
                       p->data=i;
                       L->next=p;
                       L=p;
                      L->next=NULL;
                       }  
         return head;
         }
Node *createNode2(int n)

{//尾插法创建链表
     Node *L;
     int i;
     Node *p;
     L=(Node *)malloc(sizeof(Node));
     L->next=NULL;
     for(int i=0;i<n;i++)

         {
             p=(Node *)malloc(sizeof(Node));
             p->data=i;
             p->next=L;
             L=p;
            
             }
             return L;
     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值