首页 > 技术文章 > 链表应用能力自测

jeseesmith 2020-10-30 20:37 原文

#include <stdio.h>
#include<string.h>
#include<stdlib.h>

//第一关代码

struct node
{//此处填写代码,定义链表结点类型,包含一个存放整型数据的 成员,和一个指向下一个结点的成员
    
       int data;
    struct node* next;
};
typedef struct node *PLIST;
typedef struct node NODE;
struct node *mycreateList()
{//此处填写代码,创建一个只有一个头结点的空链表,头节点的数据域赋值为0,并将表头结点的地址返回
    struct node *li = (struct node*)malloc(sizeof(struct node));
    li->data = 0;
    li->next = NULL;
    return li;

}


//第二关代码

void myinsertHead(struct node * head, int insData )
{
    /*在此处完成任务,实现在head为表头d 链表的头插数据元素insData的功能*/
    //begin
   struct node *p ;
   p = (struct node *)malloc(sizeof(struct node));
   p->data = insData;
   p->next = head->next ;
   head->next = p ;
    
    //end 
}

void myinsertTail(struct node *  head , int insData )
{
    /*在此处完成任务,在head为表头的单链表表尾插入数据元素insData*/
    //begin
    struct node *p ,*q ;
   p = (struct node *)malloc(sizeof(struct node));
   p->data = insData;
   p->next = NULL;
   q = head ;
   while(q->next!=NULL) 
     q = q->next;

   q->next = p ;
    
    //end     
}

void myprintList(struct node *L)
{
     /*在此处完成任务,输出head为表头链表中的数据,每输出一个数据换一行*/
    //begin
    
     struct node *p = L->next ;
    while(p)
     {
         printf("%d\n",p->data);
      //printf(“%d\n”,p->data);
      p = p->next ;
      }
    //end 
    
}

//第三关代码
 void reverseList_link( struct node *L)
 {
    //请在此处填入代码,实现链表逆置功能 
     //begin
     

    PLIST p,q;
    p=L->next;
    L->next=NULL;
    while(p){
        q=p;
        p=p->next;
        q->next=L->next;
        L->next=q;
    }

      
    //end 
 }

 

推荐阅读