struct list - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

struct list

Description:

printf('%d ',head- value); PrintList(head- next); return; ????? ????? ... ifp = fopen(name,'r'); if (ifp == NULL) printf('Can't open %s for reading!n', name) ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 50
Provided by: csta3
Category:
Tags: define | list | struct

less

Transcript and Presenter's Notes

Title: struct list


1
????? ????? 13
  • ????
  • 24.1.06

2
?????? ???????
???? ????
  • ???? ???? ?????? ??? ?????
  • ??? ???? ?? ????? ??????? ?????
  • ???? ????. ???? ?? ???? ????? ??????.

????
????

????? ?????
????? ???? ??????
????
????
????
????
????
????
????
????




7
1
8
5
????? ?????
????? ?????
????? ?????
????? ?????
3
????

struct list int value struct list
next typedef struct list Item
????? ?????
  • ?????? ?? ??????
  • ????? ?????? ?? ?????
  • ????? ???? ??????? ??????
  • ????? ??? ??????
  • ?????/????? ?? ???? ??????
  • ????? ?????

4
?????? ??????? ?-C
  • ????? ????? ??? ???? ????? ???? ?????? ???? ?
    main.
  • ???? ??? ??????? ????? ??? ????? ?????? ??????
    ???? ??????.
  • ???????? ??? ???? ????? ???? ?????? ???? ??????
    ?????? ?? ????? ???? ??????.

5
?????? ??????? ?-C
  • int main()
  • Item L_head NULL
  • L_head Add(L_head,5)
  • L_head Add(L_head,3)
  • L_head Add(L_head,7)

????? ???? ??????
6
  • Item Add(Item L_head, int value)
  • Item L_tail L_head
  • Item I_new malloc(sizeof (Item))
  • I_new-gtvalue value
  • I_new-gtnext NULL
  • if (L_tail NULL)
  • return I_new
  • while (L_tail-gtnext ! NULL)
  • L_tailL_tail-gtnext
  • L_tail-gtnext I_new
  • return L_head

7
?????? ??????? ?-C
  • int main()
  • Item L_head NULL
  • L_head Delete(L_head,3)

????? ???? ??????
8
  • Item Delete(Item head,int value)
  • Item curr head
  • Item prev head
  • if (curr-gtvalue value)
  • curr curr-gtnext
  • free(head)
  • return curr

????? ?????? ??? ??????
9
  • while (curr ! NULL)
  • if (curr-gtvalue value)
  • prev-gtnext curr-gtnext
  • free(curr)
  • return head
  • prev curr
  • curr curr-gtnext
  • return head

????? ?????? ????
10
????? ?????
void PrintList(Item head) if (head
NULL) printf("\n") return else p
rintf("d ",head-gtvalue) PrintList(head-gtnext)
return
11
  • ???? ??????? ?????? ????? ?????? ?????? ???
    ?????? ?? ??????

12
int Count(Item L) int counter0 while (L
! NULL) counter LL-gtnext return
counter int CountR(Item L) if (L
NULL) return 0 return 1 CountR(L-gtnext)
13
  • ???? ??????? ?????? ????? ?????? ?????? ?? ??????

14
void DeleteList(Item head) Item
currhead while (curr ! NULL)
headhead-gtnext free(curr)
currhead return
15
int DeleteList(Item head) if (head ! NULL)
DeleteList(head-gtnext)
free(head) return
16
  • ???? ??????? ?????? ??????? ???? ?????? ???????
    ?????? ????.

????? 2
????? 1
17
Item Meld(Item L1,Item L2) Item L1_head
L1 if (L1 NULL) return L2 if (L2
NULL) return L1 while (L1-gtnext ! NULL)
L1L1-gtnext L1-gtnext L2 return L1_head
18
????? ??????
????? - ????? ?????-???
19
????? ?????? ?? ?????
  • ?????? ???? ????? ?????/????? ?????, ??? ?? ?????
    ??????? ?????? ????.
  • ????? ????? ???? ?? ????? (????? ????/??? ????)
    ?? ?????? stdio.h ???? ??? FILE, ?????????
    ???/??? ??????? ????.
  • ???? ?? ???? ?????? ???.

20
????? ?????? ?? ???? ????? ?????
  • ??? ????? ?? ????, ???? ???? ?? ????? ???? ?????
    ???????? fopen. ???? fopen(info.txt,
    r)
  • fopen ????? ?? ?? ?????, ??? ??? ?????? ????
  • ????? "r", ????? "a", ????? w.
  • ????? ???? ???? ?????? ((w ????? ?? ?????
    ?????. ?? ????? ????? ?? ????? ????? ????? ??????
    (a) .
  • ???????? ???? ?????? ????? ?????? ???? FILE.
  • ??????
  • FILE fp
  • fp fopen(info.txt, r)

21
????? ????? - ????
  • ?? ?????? ????? ???? ?????? ??? ??? ?? ????
    ????? NULL.
  • ?? ?????? ????? ???? ?????? ?? ????? ??? ??? ??
    ???? ????? ???? ????? ???? ??? ???. ?? ??? ??
    ????? (???? ?? ??? ???? ?????) ????? NULL.

22
????? ???? - ?????
  • ??? ???? ????? ???? ?? ????? ??????? ??????.
    ????
  • FILE fp
  • fp fopen(in.txt, r)
  • if (fp NULL)
  • printf(The file could not be opened\n)

23
????? ????
  • ?? ??? ??????? ?? ????? ?? ???? ??????
  • FILE fp
  • fp fopen(in.txt, r)
  • .
  • fclose(fp)
  • ?????? ????? ?"? ????? ???????? fclose ??
    ????? ????? (????? FILE) ??????.

24
????? ?????
  • ???? ????? ????? ?? ?? ????? ???? ??????.
  • ???? ???? ?? ?????? ?"? fscanf, ?????? ??? scanf,
    ?? ????? ?????? ????? ????? ?????. ????
  • FILE ifp
  • ifp fopen(in.txt, r)
  • fscanf(ifp, d c, num, tav)

(???? ????? ?????? ????)
  • ????? FILE ???? ?? ????? ?????? ???? ?????, ??
  • ??????? ???? ????? ???.
  • ???????? fscanf ?????? ?? ???? ??????? ????
    ????.

25
????? ????? ???????? ??????
  • ???? ????? ?????? ?? ?"? ????????
  • char fgets(char str, int n, FILE ifp)
  • ?? ???? ??? gets, ???? ?? ????? ???? (?? \n,
    ????) ?? ??? ????? n-1 ????? (????? ????? \0
    ????? ?????? ??????).
  • ?????? ?????? ??? ????? ?????.
  • ?? ????? ?????? ????? NULL (????? ??????? ?????).
  • ???? ?????? ?? ?-getc(ifp) ??? ????? ??????
    ?-getchar().
  • ????? ??? ??? ?????.

26
????? ????? ??? ?????
  • ?? ?????? ????? ????? ?????? ??????, ?? getc ?-
    fscanf ?????? ?? ???? 1-, ???????? ??? ????.
  • ?????? stdio.h ????? ????? EOF (End Of File)
    ????? 1-.
  • ????? ??????-???????, ????? ????? ??? ??????? ???
    ????? ???? (????? ????? 1-)
  • if (fscanf(d, i) ! EOF)

27
????? ???? ??????
  • fopen(in.txt, w)
  • ???????? ?????? ????? ?-FILE, ??? ?????
  • FILE ofp
  • ofp fopen(in.txt, w)

28
????? ?????
  • ???? ????? ????? ?? ?? ????? ???? ??????.
  • ?????? ????? ???? ?"?
  • fprintf (ofp, d c, num, tav)
  • ?????? ??? ?????? ?-printf ?? ??????? ?????? ???
    ????? ????? ????? ?????.

29
????? ???????
  • ??? C ??????? ?? ?? ???? (???????) ????? (??
    ????) ??? ?????/????? ?? ?????.
  • ?????? ??????? ?????? ????? ??????? ?????? ????
    ?? (??????? ??????).
  • stdin ???? ???????? (???????).
  • stdout ???? ???????? (????).
  • stderr ??? ?? ?????? ????? (????? ?? ??? ????).
  • ?? ???? ?????? printf(d, 10) ??? ?????
  • fprintf (stdout, d, 10)

30
????? ??????? - ?????
  • ???? ?? ????? ????? ????-??? ??????? ???????,
    ??? ?????? ?? ???? ?????? ?-20 (???? ????? ?????
    ??- 0\), ?? ???? ?????? ?-
  • fgets(str, 20, stdin)
  • (?? ??? ?????? ?????? ???? ?????-???? ???? ???
    ????? ????? ??????)

31
????? ?? ????? - ?????
  • ????? ?????? ?????? ?? ?? ???? ?????? ???
    ????? ?? ??.
  • ???? ????? ?? ???? ????? ?? ????? ??? 80 ???
    ?????.

32
?????? ?????? ????? ?????
  • define SIZE 81
  • int main()
  • FILE ifp
  • int count0
  • char lineSIZE, filenameSIZE
  • fgets(filename, SIZE, stdin)
  • ifp fopen(filename, r)

?????? ?? ?? ?????
????? ????? ??????
33
?????? ?? ?????? ??????
  • if (ifp NULL)
  • printf(File could not be opened\n)
  • return -1
  • while(fgets(line, SIZE, ifp) ! NULL)
  • count
  • printf(There are d lines\n, count)
  • fclose(ifp)
  • return 0

?????? ????? ?? ??? ??, ??????? ????
????? ????? ?????
34
?????
  • ???? ?????? ?????? ???? ???? ?????? ?? ????? ????

35
  • int main(void)
  • FILE ifp
  • char name20 in.txt"
  • char c
  • ifp fopen(name,"r)
  • if (ifp NULL)
  • printf("Can't open s for reading!\n",
    name)
  • return 1

36
  • c fgetc(ifp)
  • while(c ! EOF)
  • putchar(c)
  • c fgetc(ifp)
  • fclose(ifp)
  • return 0

37
?????
  • ???? ?????? ?????? ???? ????, ?????? ???? ????
    ???? ?? ???? ?????? ?? ????? ????? ???? ???

38
  • int main(void)
  • FILE ifp, ofp
  • char iname20 in1.txt"
  • char oname20 out.txt"
  • char c
  • ifp fopen(iname,"r)
  • if(ifp NULL)
  • printf("Can't open s for reading!\n", iname)
  • return 1
  • ofp fopen(oname,"w)
  • if(ofp NULL)
  • printf("Can't open s for writing!\n", oname)
  • fclose(ifp)
  • return 1

39
  • fprintf(ofp, "This is a doubled space version of
    the file s\n\n",iname)
  • c fgetc(ifp)
  • / write contents of file to another /
  • while(c ! EOF)
  • fputc(c,ofp)
  • / if newline, add another new line /
  • if(c '\n')
  • fputc('\n',ofp)
  • c fgetc(ifp)
  • fclose(ifp)
  • fclose(ofp)
  • return 0

40
????? -?????
  • ???? ???? ?- C ???/??? ?? ????? ????? ????/??? ??
    ?????? ?????.
  • ???? ????? ????? ?? ????? ?????? ?- mode ??????
    ?????? ???? ????? ???????.

41
?????? ???????
  • ???? ????? ?????? ??????? ?"? ????????
  • int rand(void)
  • ???????? ?????? ???? ????? ??? ????? 0 ??
    RAND_MAX
  • ????? ?-RAND_MAX ??? ????? 32,767
  • ???????? ?????? ?-stdlib.h

42
???????
  • ????? ???? ????? ??? ??? 0 ?-19 (????)
  • rand() 20
  • ????? ???? ????? ??? ??? 10 ?-20 (????)
  • rand() 11 10
  • ????? ???? ????? ???? ????? 0,1
  • (double) rand() / RAND_MAX

43
????? ?????? ??????? - ?????
  • ??? ????????? ???? ???? ?? ??????? ?????? ????
    ???? ?? ?????? ???????.
  • ?? ????? ??????, ?-seed, ???? ????? ?"? ????????
    ???? ??????? ?-stdlib.h
  • void srand(unsigned int seed)
  • ??? ??-seed ???? ???? ??? ????, ?? ????? ??????
    ??????? ?? ?????? ????
  • srand(time(NULL))
  • ???? ???????? time() ?????? ?-time.h

44
?????
  • ???? ?????? ??????? 7 ?????? ???????
  • ????? ????? 5 ?? 50 (????), ???"? ??????
  • 7 ?????? ??????? ?????? ????? 0,1.

????? ???? 23 12 27 31 46 47 31 0.016877 0.554064
0.400006 0.784295 0.481857 0.487777 0.724479
45
?????
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include lttime.hgt
  • int rand_int()
  • return rand() 46 5
  • double rand_real()
  • return (double) rand() / RAND_MAX

46
????? - ????
  • int main()
  • int k
  • srand(time(NULL))
  • for ( k0 klt7 k )
  • printf("d\n",rand_int())
  • for ( k0 klt7 k )
  • printf("lf\n",rand_real())
  • return 0

47
?????
  • ???? ??? ?????? ??? ???.
  • ?????? ????? ???? ??? ?????? ?? ??? ????,
  • ??????? ?? ???? ???? ?????? ???.
  • ?? ?????? ?????? ?? ??? ?? ???? ??????
  • ?????? ??????? ??? ????? ??? ????
  • ???? ???????? ?? ????? ??.

48
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include lttime.hgt
  • double rand_real()
  • return (double) rand() / RAND_MAX
  • int main()
  • int all 0,good 0
  • double x,y,temp
  • srand(time(NULL))

49
  • while (1)
  • x rand_real()
  • y rand_real()
  • if ( x gt y )
  • temp x x y y temp
  • if ( x gt 0.5 y - x gt 0.5 1 - y gt 0.5 )
  • good
  • all
  • if ( all 1000000 0 )
  • printf("d / d lf\n",
  • good,all,(double) good/all)
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com