This presentation is the property of its rightful owner.
Sponsored Links
1 / 45

第十二讲 链表插入结点 PowerPoint PPT Presentation


  • 38 Views
  • Uploaded on
  • Presentation posted in: General

第十二讲 链表插入结点. 链表插入结点. 原则: 1 、插入操作不应破坏原链接关系 2 、插入的结点应该在它该在的位置。应该有一个插入位置的查找子过程。. 先看下面一个简单的例子: 已有一个如图所示的链表。它是按结点中的整数域从小到大排序的。现在要插入一个结点,该节点中的数为 10 。. head. 此结点已插入链表. 待插入结点. 参考程序. // 结构 7.c #include <stdio.h> // 预编译命令 #include <malloc.h> // 内存空间分配 #define null 0 // 定义空指针常量

Download Presentation

第十二讲 链表插入结点

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


6582228


6582228

  • 1

  • 2


6582228

10

head


6582228

// 7.c

#include <stdio.h>//

#include <malloc.h>//

#define null 0//

#define LEN sizeof(struct numST)//

struct numST//

{

int num;//

struct numST *next;// numST

};


6582228

// insert()

void insert (struct numST **phead, struct numST *p)

{//

struct numST *q,*r;// q,r

if ((*phead)==null)//

{

*phead = p;// p

return;//

}

else//

{

// pnumnum

if ((*phead)->num > p->num )

{// p

p->next = *phead;// pnext(*phead)

*phead = p;// p

return;//

}


6582228

//

r = *phead;// r

q = (*phead)->next;// q

while (q!=null) //

{

// numpnum

if (q->num < p->num)

{

r = q;// rqq

q = q->next;// q

}

else//

break;//

}

// p

r->next = p;

p->next = q;

}

}


6582228

// ST

void print(struct numST *head)

{

int k=0;//

struct numST * r;// rST

r=head;// rhead

while(r != null)//

{//

k=k+1;// 1

printf("%d %d\n",k,r->num);

r=r->next;//

}//

}


6582228

void main()//

{//

struct numST *head, *p;// ST

head = null;// headnull

// 3ST

head = (struct numST *) malloc(LEN);

head->next = (struct numST *) malloc(LEN);

head->next->next = (struct numST *) malloc(LEN);

// 3num51015

head->num = 5;

head->next->num = 10;

head->next->next->num = 15;

head->next->next->next = null; //

// p

p = (struct numST *) malloc(LEN);

p->num = 12;

p->next = null;

insert(&head, p);// insertp

print(head);// print

}//


6582228

1ST*head*phead=null

23ST

1head=(struct numST*) malloc(LEN);

2head->next=(struct numST*) malloc(LEN);

3head->next->next=(struct numST*) malloc(LEN);

head

head->next

head->next->next

3ST


6582228

3numnull

4head->num=5;

5head->next->num=10;

6head->next->next->num=15;

7head->next->next->next=null;

4

head

head


6582228

3pp12

1p=(struct numST*) malloc(LEN);

2p->num=12;;

3p->next=null;

4insertp

insert(&head,p);

pheadhead&head


6582228

1 insert(head,p);void insert(struct munST *phead, struct numST*p);

head

p

p

phead


6582228

headpheadphead

head

phead

pheadp45pphead


6582228

head

phead

phead

headheadheadhead


6582228

2 insert(&head,p);

void insert(struct munST **phead, struct numST*p);struct numST **phead

*pheadnumST*pheadnumST,phead*pheadphead=&head


6582228

&head

head

phead

headpheadpheadhead


6582228

headpheadhead*phead*pheadhead

5printhead


6582228

insert

insert*phead=headpqr

*phead==nullp

*phead=p; // p

return; //

headp


6582228

pnumnum(*phead)->num>p->nump

p->next=*phead; // p

*phead=p; // pheadp

return; //

*phead

*phead

null

p


6582228

pnumnumprqrq

r->num < p->num < q->num

prq

r

q

r

q

head

p

null


6582228

r=*phead q=*phead->next

(1) qrpr

r->next=p;

p->next=q;

(2) q!=nullpnumqnumpqrq

r=q;

q=q->next;


6582228

(2)

q!=nullp->num<=q->numwhileprq

r->next=p;

p->next=q;


6582228

1


6582228

2

head

(1)head->name==namehead=head->next;

(2)rqr=head; q=head->next;


6582228

(3)q!=nullq->namename r->next=q->next; rq r=q; q=q->next;(3)

(4)qnullq

r->next = q->next;


6582228


6582228

root

4

6

2

7

3

5

1


6582228

  • 1

  • 2root

  • 3LR

  • 4

  • 5


6582228


6582228

1

1.1

1.2

1.3

2

2.1

2.2

2.3

3

3.1

3.2

3.3


6582228

  • p

    • struct TREE

    • {

    • int data;

    • struct TREE *L, *R;

    • };

  • pTREE

  • struct TREE *p;

  • LNR(P)p


  • 6582228

    • 1ApLNR(p)pABpC

    • 2C

    • 2.1 DpLNR(p->L);

    • 2.2 Epp

    • 2.3 FpLNR(p->R)


    6582228

    3LNR(p)LNR(p->L)LNR(p->R)pp->Lp->Rp->Lp->R

    213


    6582228

    13


    6582228

    dataLRTREE

    struct TREE

    {

    int data;

    struct TREE *L, *R;

    }


    6582228

    1

    void insert(struct TREE **proot, struct TREE *p)

    p

    proot

    insertp*proot


    6582228

    insert(proot, p)


    6582228

    prootprootinsert

    &root

    proot

    &rootp

    proot, p


    6582228

    #include <stdio.h>//

    #include <malloc.h>//

    #define null 0//

    #define LEN sizeof(struct TREE)//

    struct TREE//

    {

    int data;//

    struct TREE *L,*R;// TREE

    };


    6582228

    // insert

    void insert (struct TREE **proot, struct TREE* p)

    {//

    if (*proot==null)//

    {

    *proot = p;// p

    return;//

    }

    else//

    {

    // p

    if (p->data <= (*proot)->data)

    insert( &((*proot)->L), p);//

    else// p

    insert( &((*proot)->R), p);//

    }

    }//


    6582228

    // TREE

    void print(struct TREE *root)

    {//

    if (root == null)//

    return;//

    print(root->L);//

    printf("%d",root->data);//

    print(root->R);//

    }//

    void main()//

    {//

    struct TREE *root, *p;// TREE

    int temp;//

    root = null;//

    p = null;//

    printf("\n");//

    printf("-1\n");//

    scanf("%d",&temp);//


    6582228

    while(temp != -1)// -1

    {//

    //

    p = (struct TREE *) malloc(LEN);

    p->data = temp;// tempp

    p->L = p->R = null;// p

    insert( &root, p );// proot// &root

    printf("\n");//

    printf("-1\n");//

    scanf("%d",&temp);//

    }//

    if (root==null) //

    printf("\n");//

    else//

    print(root);// print

    }//


    6582228


  • Login