Transcript
  • (Linked List)4-1 4-2 (Singly Linked List)4-3 4-4 (Circular Linked List)4-5 (Double Linked List)4-6 4-7

  • 4-1 (pointer) (pointer) (node)(Data)(Pointer)(Linked List)

    Node

    C(main())typedef struct node *node_pointer;typedefstructnode{int data;node_pointer link;};*node_pointerstruct nodenode_pointer

    Null

  • Cmalloc()free()malloc()malloc()(NULL)mallocfp = (*)malloc()(sizeof())mallocfpfree()malloc()free()free()(dangling reference) free(fp);

  • Cvoid main(){float *fp; /* */fp=(float*)malloc(sizeof(float)); /**/if(!fp) { /* !fp */printf(" !\n");exit(1); /**/}*fp=3.1415926; /* */printf("PI :%f\n",*fp);free(fp); /* */}

  • 4 . 1 . 1 C free()C(overflow)(pointer)(node)delete (Singly Linked List)AV(Available Node)newAV

  • 4-2(Singly Linked List)

  • ch04_01.c

    num

    name

    score

    num

    name

    score

    next

    next

    num

    name

    score

    next

    num

    name

    score

    next

    newnode

    ptr

    Null

    num

    name

    score

    next

    num

    name

    score

    next

    ptr

    Null

    delptr

    ptr

  • top=headhead=head->nextdelete top;

    Y=ptr-next;ptr->next=Y->next;free(Y);

    NULL ptr->next=tail;ptr->next=NULL;free(tail);

    Null

    head

    head

    head

    Null

    head

    Y

    ptr

    Null

    head

    tail

    ptr

  • newnode= ....malloc(...)newnode->next=head;head=newnode;

    N UL L newnode= ....malloc(...)ptr->next=newnode;

    X Y X Y newnode= ....malloc(...)X->next=newnode;newnode->next=Y;

    Null

    newnode

    Null

    head

    head

    Null

    newnode

    head

    Null

    X

    Y

    Null

    newnode

    head

    Null

    ptr

  • Procedure Invert(x)p x;q NIL;While pNIL dorq;qp;pLINK(p);LINK(q) r;ENDx q;END

    Null

    X

    Null

    X

    r

    q

    p

    r

    q

    p

  • CProcedure Invert(x)p x;q NIL;While pNIL dorq;qp;pLINK(p);LINK(q) r;ENDx q;ENDptr=head;before=NULL;printf("\n \n");while(ptr!=NULL) { /**/last=before;before=ptr;ptr=ptr->next;before->next=last;}ptr=before;

  • C struct NODE_TYPE{int Data;struct NODE_TYPE* pNext;};typedef struct NODE_TYPE NODE;NODE* Concatenate(NODE* head1,NODE* head2){NODE* ptr;ptr = head1;while(ptr->pNext != NULL)ptr = ptr->pNext;ptr->pNext = head2;return head1;}

    Null

    head1

    Null

    head2

  • 4-3 struct _NODE{int Data;struct _NODE* pNext;};typedefstruct_NODEnode;typedefnode*link;typedefnode_TOP;

    Null

    top

  • void AddNodeToStack(_TOP** top, int nVal){link pNewNode = (link)malloc(sizeof(node));pNewNode->Data = nVal;pNewNode->pNext = *top;*top = pNewNode;}void DelNodeFromStack(_TOP **top, int* nRetVal){link pDelNode = (link)malloc(sizeof(node));if(*top == NULL){printf("[]\n");*nRetVal = -32768;return;}else{pDelNode = *top;*nRetVal = (*top)->Data;*top = (*top)->pNext;free(pDelNode);}}

    Null

    nVal

    pNewNode

    top

    Null

    pDelNode

    top

  • void AddNodeToQueue(_FRONT** front, _REAR** rear, int nVal){link pNewNode = (link)malloc(sizeof(node));pNewNode->Data = nVal;pNewNode->pNext = NULL;if(*rear == NULL){*front = pNewNode;*rear = pNewNode;}else{(*rear)->pNext = pNewNode;*rear = pNewNode;}}void DelNodeFromQueue(_FRONT** front, int* nRetVal){link pDelNode = NULL;if(*front == NULL){printf("[]\n");*nRetVal = -32768;return;}*nRetVal = (*front)->Data;pDelNode = *front;*front = (*front)->pNext;free(pDelNode);}

    Null

    nVal

    pNewNode

    front

    rear

    Null

    Null

    pNewNode

    front

    rear

  • 4-4(Circular Linked List)

    A

  • newNode = ... malloc...;newNode->next=head;tail->next=newNode;head=newNode;

    I X I = ... malloc ..;I->next=X->next;X->next=I;

    tail

    head

    newNode

    head

    I

    tail

    X

  • pNewNode

    head

    pNewNode

    head

    pCurNode

    pCurNode

    head

    pNewNode

    tail

    after

  • tail=head->next;head=head->next;free(pDelNode);

    pPrevNode->next=del->next;

    head

    pDelNode

    head

    pCurNode

    pCurNode

    tail

    pCurNode

    head

    I

    pCurNode

    del

    pPrevNode

  • head

    pDelNode

    head

    pCurNode

    pCurNode

    pTailNode

    pCurNode

    head

    I

    pCurNode

    del

    pPrevNode

  • 1 2 W=X->next;X->next=Y->next;Y->next=W;

    1

    2

    9

    X

    A

    B

    Z

    Y

    NULL

    Y

    NULL

    1

    2

    9

    A

    X

    B

    Z

    Y

    1

    B

    Z

    X

    A

    2

    9

    Y

  • 4 . 4 . 1 () Procedure Invert(T)if T=nil then returnq T;p LINK(T)While p T doBEGINr qq pp LINK(p);LINK(q) rEndLINK(T) qT qend.O(1)

    q

    p

    T

    r

    r

    q

    p

    T

  • 4 . . 2 (circular linked list)(1)(2)()(1)Procedure CINSERT(A,X)if A=Nil then [A X;LINK(X) NIL]else [LINK(X) LINK(A)]LINK(A) Xend(2)Procedure Length(T)i 0if T NIL [P Twhile (P T) do[ i i+1 P LINK(P)]]return(i)END

    head

    pNewNode

    head

    pCurNode

    pCurNode

  • 4 . 4 . 4 (Erase)(1)(Single linked list)(2)(circular list)(3)()(1) TAV() AVT Procedure ERASE(T)if T=Nil then returnP Twhile LINK(P)Nil doP LINK(P)endLINK(P) AVAV Tend

    AV

    T

    NULL

    NULL

    AV

  • (2)TTAVAVTProcedure CERASE(T)if T=nil then returnX LINK(T)LINK(T)AVAVXend.(3)TT(Tn)O(n)AVO(1)

    AV

    NULL

    T

    AV

  • (Sparse Matrix) i j D R ai,j

    i

    D

    j

    R

    ai,j

  • 3-tuple

    3

    3

    0

    0

    0

    0

    0

    0

    A

    H1

    H2

    H3

    0

    0

    0

    0

    0

    0

    H1

    H2

    H3

    2

    1

    12

    3

    3

    -2

  • 4 . 4 . 6 AVL()()(1) (2) O(m+n+j)mn j

  • 4-5(Double Linked List) LLINK RLINK RLINK LLINK

    ptr ptr=RLINK(LLINK(ptr))=LLINK(RLINK(ptr)) struct _NODE{int DATA;struct _NODE* LLINK;struct _NODE* RLINK;};typedef struct _NODE node;typedef node* link;typedef node* _HEAD;typedef node* _INSERTAFTER;typedef node* _DELETE;

    DATA

    RLINK

    LLINK

    NULL

    NULL

    Head

  • For_HEAD CreateDoubly(int* arr,int Num){link LLINKNode = NULL;link pNewNode = NULL;int i = 0;_HEAD head = (_HEAD)malloc(sizeof(node));if(head == NULL){printf("[]\n");return NULL;}else{memset(head,0,sizeof(node));head->DATA = *(arr+0);LLINKNode = head;for(i=1;iDATA = *(arr+i);LLINKNode->RLINK = pNewNode;pNewNode->LLINK = LLINKNode;LLINKNode = pNewNode;}}return head;}

    NULL

    NULL

    Head

    pNewNode

    NULL

  • head->LLINK=pNewNode;pNewNode->RLINK=head;head=pNewNode;

    ptr->RLINK=pNewNode;pNewNode->LLINK=ptr;

    p t r pNewNode->RLINK=ptr->RLINK;ptr->RLINK->LLINK=pNewNode;ptr->RLINK=pNewNode;pNewNode->LLINK=ptr;

    NULL

    NULL

    Head

    pNewNode

    ptr

    1

    2

    3

    4

    NULL

    NULL

    Head

    pNewNode

    Head

    1

    2

    NULL

    NULL

    Head

    pNewNode

    1

    2

    ptr

  • head=head->RLINK;free(head->LLINK);head->LLINK=NULL;

    del->LLINK->RLINK=NULL;free(del);

    p t r del->LLINK->RLINK=del->RLINK;del->RLINK->LLINK=del->LLINK;free(del);

    NULL

    NULL

    Head

    Head

    NULL

    NULL

    Head

    del

    NULL

    NULL

    Head

    del

  • 4 . 5 . 1 ()

  • 4-6

    COEFEXPL INKn P(x)=an-1xen-1+an-2xen-2++a0

    A(x)=3X2+6X-2

    EXP

    LINK

    COEF

    en-1

    an-1

    en-2

    an-2

    e0

    a0

    NULL

    2

    3

    1

    6

    0

    -2

    NULL

  • ch04_05.cwhile(a!=NULL) /*1*/{b=ptr; /* AB */while(b!=NULL){if(a->exp==b->exp) { /* */

    sum[i]=a->coef+b->coef;a=a->next;b=b->next;i++;}else if(b->exp > a->exp) {/*BC*/sum[i]=b->coef;b=b->next;i++;}else if(a->exp > b->exp) { /*AC*/sum[i]=a->coef;a=a->next;i++;}}}return creat_link(sum);

  • 4 . 6 . 1 XAYBZC(1) X6-6XY5+5Y6(2) "0" (3) X6-3X5-4X4+2X3+3X+5

  • 4 . 6 . 2 P(x,y,z)=x10y3z10+2x8y3z2+3x8y2z2+x4y4z+6x3y4z+2yz

  • 4-7CPU(Allocation)(Release)( Dynamic Storage Management)SizeLink

  • 4-7-1(Bes t Fit) (First Fit) (Worst Fit)(Best Fit)

  • 4-7-2(Boundary Tag Method) (First Fit) (tag) (tag value)

  • ( H e a d e r ) (1) L I N K (2) TAGTAG= 0 T A G = 1 (3) S I Z E (4) R L I N K ( S p a c e ) ( T a i l e r ) (1) UP L INK(2) TAGTAG=0 TAG=1

  • (Buddy System)2 2 n 2k 2k - 1n2k2 2m 0~2m-1 2021222mm + 1

  • n 2k2k-1n2kAV( i ) k i m i headk< i 2 2i-1 head head+2i-12i-13k i-132khead 2k

  • 4 . 7 . 2 256k (Buddy System)(1) (Allocated Node)(Free Node)(2) 4 (program)P1=41kP2=15kP3=29kP4=115k

  • 4 . 7 . 3 (1) P3(2) P5=43k


Top Related