Data Structure in C - ???? - PowerPoint PPT Presentation

1 / 62
About This Presentation
Title:

Data Structure in C - ????

Description:

Title: Introduction C Language Author: kurt Last modified by: Piggy Created Date: 2/15/2003 7:36:33 PM Document presentation format: – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 63
Provided by: kurt111
Category:
Tags: data | doubly | link | linked | list | structure

less

Transcript and Presenter's Notes

Title: Data Structure in C - ????


1
Data Structure in C -
????
2
??
  • ??????
  • ????
  • ??????
  • ???????

3
????
  • ?????????,????(insert)???(delete)????(node)?????
  • Ex. ?????a, b, d, e????,???c???d, e?????
  • Ex.?????a, b, d, e????,???d??????????????

??????(linked list) ????
4
???? (?)
  • ?? vs. ????

5
??????
  • ?????? (single linked list)
  • ???????????(node)??????????,??????(data)????(next)
    ,
  • ,???????struct??,?
  • struct node
  • int data
  • struct node next
  • struct node headNULL, tail, this, prev,
    x, p, ptr

6
?????? (?)
  • Ex. ??Aa, b, c, d, e,????????

?????????
?????????
7
?????? (?)
  • ???????????
  • ????????
  • ????????
  • ?????????????

8
????????
Step 1 x (struct node ) malloc(sizeof(struct
node)) x-gtnext NULL
Step 2 x-gtnext head
9
Step 3 head x
????????
Step 1 x (struct node ) malloc(sizeof(struct
node)) x-gtnext NULL
10
Step 2 tail-gtnext x
Step 3 tail x
???tail??,??? ???????????
11
?????????????
Assumption ?????x??ptr??????
Step 1 x (struct node ) malloc(sizeof(struct
node)) x-gtnext NULL
Step 2 x-gtnext ptr-gtnext
12
Step 3 ptr-gtnext x
13
  • void insert_node(struct node ptr, struct node
    head, struct node tail)
  • struct node this
  • if(head NULL) / ???????? /
  • ptr-gtnextNULL
  • headptr
  • tailptr
  • else
  • thishead
  • if(ptr-gtkey gt this-gtkey) / ??????? /
  • ptr-gtnextthis
  • headptr

14
  • else
  • while(this-gtnext ! NULL)
  • prevthis
  • thisthis-gtnext
  • if(ptr-gtkey gt this-gtkey) / ??????? /
  • ptr-gtnextthis
  • prev-gtnextptr
  • break
  • if(ptr-gtkey lt this-gtscore) / ??????? /
  • ptr-gtnextNULL
  • this-gtnextptr
  • tailptr

15
?????? (?)
  • ???????????
  • ?????????
  • ?????????
  • ??????????

16
?????????
Step 1 ptr head
Step 2 head ptr-gtnext
Step 3 free(ptr)
17
?????????
ptr head while (ptr-gtnext ! tail) ptr
ptr-gtnext
Step 1?????tail??????
Step 2 ptr-gtnext NULL
Step 4 tail ptr
Step 3 free(tail)
18
??????????
Step 1?????????,??????this?prev
??????????
???????
while (this-gtnext ! NULL) prev this
this this-gtnext
19
Step 2 prev-gtnext this-gtnext
Step 3 free(this)
20
  • void delete_f(int del_key, struct node head,
    struct node tail)
  • struct node clear, this, prev thishead
  • if(del_key head-gtkey) / ??????? /
  • clearthis
  • if(head-gtnext NULL) / ??????? /
  • tailNULL
  • headhead-gtnext
  • free(clear)
  • while(this-gtnext ! NULL) / ??????? /
  • prevthis
  • thisthis-gtnext
  • if (del_key this-gtkey)
  • clearthis
  • prev-gtnextthis-gtnext
  • free(clear)
  • if(del_key tail-gtkey) /??????? /
  • clearthis
  • prev-gtnextNULL
  • tailprev

21
?????? (?)
  • ??????
  • ????
  • x???NULL,y?????
  • x?????,y???NULL
  • x???y??????

22
  • void concatenate (struct node x, struct node y,
    struct node y)
  • struct node c
  • if (x NULL)
  • z y
  • else if (y NULL)
  • z x
  • else
  • zx
  • cx
  • while(c-gtnext ! NULL)
  • c c-gtnext
  • c-gtnext y

23
  • ??????
  • ??????????????????
  • Ex. ???????????,??????????
  • void invert(struct node head)
  • struct node this, prev, next_n
  • next_n head
  • this NULL
  • while(next_n ! NULL)
  • prev this
  • this next_n
  • next_n next-gtnext
  • this-gtnext prev
  • tail head
  • head this

24
?????? (?)
  • ???????
  • ?????????????????,???????????NULL,?????????,??????
    NULL??
  • int length(struct node head)
  • struct node this
  • this head
  • int leng0
  • while (this ! NULL)
  • leng
  • this this-gtnext
  • return leng

25
????
  • ????????????????????????,?????????(circular list)
  • ?????????????,??????????????

26
???? (?)
  • ?????????
  • ????????
  • ???????(NULL)
  • ????????
  • ????????
  • ?????????????

27
???????? - ??????

Step 1 head x tailx
Step 2 x-gtnext x
???????? - ???????
Step 1 x-gtnext head
28
Step 2 tail-gtnext x
Step 3 head x
29
????????
Step 2 x-gtnext head
Step 1 tail-gtnext x
Step 3 tail x
30
  • void insert_node(struct node ptr, struct node
    head, struct node tail)
  • struct node prev, this
  • if(head NULL) / ???????? /
  • ptr-gtnext ptr
  • head ptr
  • tail ptr
  • else
  • this head
  • if(ptr-gtkey lt this-gtkey) / ??????? /
  • ptr-gtnext this
  • head ptr
  • tail-gtnext head
  • else while(this-gtnext ! head)
  • prev this
  • this this-gtnext

31
  • if(ptr-gtkey lt this-gtkey) / ??????? /
  • ptr-gtnext this
  • prev-gtnext ptr
  • break
  • if( ptr-gtkey gt this-gtkey) / ??????? /
  • ptr-gtnext head
  • this-gtnext ptr
  • tail ptr

32
???? (?)
  • ?????????
  • ?????????
  • ?????????
  • ??????????

33
?????????
Step 1 tail-gtnext head-gtnext
Step 2 ptrhead head head-gtnext
Step 3 free(ptr)
34
?????????
ptr head while (ptr-gtnext ! tail) ptr
ptr-gtnext
Step 1 ?????tail??????
Step 2 ptr-gtnext hear
Step 3 free(tail) tailptr
35
  • void delete_node(int del_key, struct node head,
    struct node tail)
  • struct node clear, prev, this
  • this head
  • if(del_key head-gtkey) / ??????? /
  • clear head
  • if(head-gtnext head) / ??????? /
  • head NULL tail NULL
  • else
  • head head-gtnext
  • tail-gtnext head

36
  • while(this-gtnext ! head head ! NULL) /
    ??????? /
  • prev this
  • this this-gtnext
  • if(del_key ,this-gtkey)
  • clear this
  • prev-gtnext this-gtnext
  • tail prev
  • if(del_key tail-gtkey) /??????? /
  • clear tail
  • prev-gtnext head
  • tail prev
  • free(clear)

37
??????
Step 1 Atail-gtnext Bhead
Step 2 Btail-gtnext Ahead
38
??????
  • ??????(doubly linked list)
  • ?????????,?????(LLink),????(Data),?????(RLink)
  • ??
  • ??ptr????????,?
  • ptr ptr-gtllink-gtrlink ptr-gtrlink-gtllink
  • ????????????,????????

39
?????? (?)
  • ??
  • ??????,????????????
  • ???????????????????
  • ???????????????????????
  • ??
  • ????????
  • ??????????(??????????)
  • ??????????(??????????)

40
?????? (?)
  • ???????????
  • ????????
  • ????????
  • ?????????????

void init_head(struct node ptr, struct node
head, struct node tail) ptr (struct node )
malloc (sizeof(struct node)) ptr-gtkey NULL
ptr-gtllink ptr ptr-gtrlink ptr head
ptr tail ptr
??head????????
41
????????
Step 1 x-gtrlink head
Step 2 x-gtllink tail
Step 3 tail-gtrlink x
42
Step 4 head-gtrlink x
Step 5 tail x
43
????????
Assumption ????????
Step 1 x-gtrlink tail-gtrlink
Step 2 tail-gtrlink x
44
Step 3 x-gtllink tail
Step 4 head-gtllink x
Step 5 tail x
45
?????? (?)
  • ?????????????
  • ????????????,????????key???????,???????
  • prev head
  • ptr head-gtrlink
  • while(ptr ! head x-gtkey ltp-gtkey)
  • prev ptr
  • ptr ptr-gtrlink

46
  • void insert_node(struct node ptr, struct node
    head, struct node tail)
  • struct node this
  • this head-gtrlink
  • while(this ! head)
  • if(ptr-gtkey lt this-gtkey) / ??????? /
  • ptr-gtrlink this
  • ptr-gtllink this-gtllink
  • this-gtllink-gtrlink ptr
  • this-gtllink ptr
  • break
  • this this-gtrlink
  • / ??????? /
  • if(head-gtrlink head this head)
  • ptr-gtrlink head
  • ptr-gtllink head-gtllink
  • head-gtllink-gtrlink ptr
  • head-gtllink ptr
  • tail ptr

47
?????? (?)
  • ???????????
  • ?????????
  • ?????????
  • ??????????

48
?????????
Step 1 ptr head-gtrlink
Step 2 head-gtrlink ptr-gtrlink
Step 3 p-gtrlink-gtllink p-gtllink
Step 4 free(ptr)
49
?????????
Step 1 tail-gtllink-gtrlink head
Step 2 head-gtllink tail-gtllink
Step 3 ptr tail
Step 5 free(ptr)
Step 4 tail tail-gtllink
50
??????????
Assumption ?????this?????
Step 1 this-gtllink-gtrlink this-gtrlink
Step 2 this-gtrlink-gtllink this-gtllink
Step 3 free(this)
51
  • void delete_node(int del_key, struct node head,
    struct node tail)
  • struct node clear, this
  • this head-gtrlink
  • while(this ! head) / ??????? /
  • if(del_key this-gtkey)
  • clear this
  • this-gtllink-gtrlink this-gtrlink
  • this-gtrlink-gtllink this-gtllink
  • if(this tail)
  • tail this-gtllink
  • break
  • this this-gtrlink
  • free(clear)

52
???????
  • ?????????
  • ?????????
  • ????????????

53
??????? (?)
  • ?????????
  • ????????????????????
  • Push ????????????
  • Pop ?????????

54
??????? (?)
  • ????????? - Push
  • void push_stack(int data, struct node ptr,
    struct node top)
  • ptr (struct node ) malloc(sizeof(struct
    node))
  • ptr-gtitem data
  • ptr-gtnext top
  • top ptr

55
??????? (?)
  • ????????? - Pop
  • void pop_stack(int data, struct node top)
  • struct node clear
  • if(top NULL)
  • printf(stack-empty)
  • clear top
  • data top-gtitem
  • top top-gtnext
  • free(clear)

56
??????? (?)
  • ?????????
  • ????????????????????
  • Enqueue????????????
  • Dequeue?????????

???????
???????
57
??????? (?)
  • ????????? - Enqueue
  • void enqueue(int data, struct node front, struct
    node rear)
  • ptr (struct node ) malloc(sizeof(struct
    node))
  • ptr-gtitem data
  • ptr-gtnext NULL
  • if(rear NULL)
  • front rear ptr
  • else
  • rear-gtnext ptr
  • rear ptr

58
??????? (?)
  • ????????? - Dequeue
  • void dequeue(int data, struct node front)
  • struct node clear
  • if(front NULL)
  • printf(stack-empty)
  • data front-gtitem
  • clear front
  • front front-gtnext
  • free(clear)

59
??????? (?)
  • ????????????
  • ??????????????,??????
  • Coef???????
  • Exp???????
  • Link?????????
  • ??(??A?B????)
  • Exp(A) Exp(B)
  • Exp(A) gt Exp(B)
  • Exp(A) lt Exp(B)

60
  • void poly_add(void)
  • struct poly this_n1, this_n2, prev
  • this_n1 eq_h1
  • this_n2 eq_h2
  • prev NULL
  • while(this_n1 ! NULL this_n2 ! NULL) /
    ?????????????? /
  • ptr (struct poly ) malloc(sizeof(struct
    poly))
  • ptr-gtnext NULL
  • / ???????????????? /
  • if(this_n1 ! NULL (this_n2 NULL
    this_n1-gtexp gt this_n2-gtexp))
  • ptr-gtcoef this_n1-gtcoef
  • ptr-gtexp this_n1-gtexp
  • this_n1 this_n1-gtnext

61
  • else
  • / ???????????????? /
  • if(this_n1 NULL this_n1-gtexp lt
    this_n2-gtexp)
  • ptr-gtcoef this_n2-gtcoef
  • ptr-gtexp this_n2-gtexp
  • this_n2 this_n2-gtnext
  • else / ?????????,???? /
  • ptr-gtcoef this_n1-gtcoef this_n2-gtcoef
  • ptr-gtexp this_n1-gtexp
  • if(this_n1 ! NULL) this_n1 this_n1-gtnext
  • if(this_n2 ! NULL) this_n2 this_n2-gtnext

62
  • if(ptr-gtcoef ! 0) / ????????0,????????? /
  • if(ans_h NULL) ans_h ptr
  • else prev-gtnext ptr
  • prev ptr
  • else free(ptr)
Write a Comment
User Comments (0)
About PowerShow.com