DS2数据结构课件.pptVIP

  • 2
  • 0
  • 约1.04万字
  • 约 51页
  • 2017-07-03 发布于湖北
  • 举报
DS2数据结构课件概要1

Deleting from the front of the list is a special case, because it changes the start of the list; careless coding will lose the list; The wrong code: free(L); The right one: p=L; L=L-next; free(p); A1 A2 A3 A4 ^ L × p A1 A2 A3 A4 ^ p q A third problem concerns deletion in general. Although the pointers moves above are simple, the deletion algorithm requires us to keep track of the cell before the one that we want to delete. Improve our linked list It turns out that one simple change solves all problems. We will keep a sentinel node, which is sometimes referred to as a header or dummy node. … header A2 A3 AN ^ L 2.4 Circularly Linked List A popular convention is to have the last cell keep a pointer back to the first. This can be done with or without a header( if the header is present, the last cell points to it.) … header A1 A2 AN L Creation of Linked List Method one: Inserting nodes into the head of the empty linked list. … header A1 A2 AN ^ s X L LinkList Creat_LinkList1( ) { LinkList L=NULL; LNode *s; int x; scanf(“%d”,x); while(x!=flag) { s=(LNode *)malloc(sizeof(LNode)); s-data=x; s-next=L; L=s; scanf(“%d”,x); } return L; } Creation of Linked List Inserting nodes into the tail of the linked list. … header A1 A2 AN ^ L s X ^ r LinkList Creat_LinkList( ) { LinkList L, s, r; int x; L=(LNode *)malloc(sizeof(LNode)); L-next=NULL; r=L; scanf(“%d”,x); while(x!=flag) { s=(LNode *)malloc(sizeof(LNode)); s-data=x; s-next=r-next; r-next=s; r=r-next; scanf(“%d”,x); } return L; } (2)Get the length of linked list int Length_LinkList1(LinkList L) { LNode *p=L-next; int j=0; while(p) { p=p-next; j++; } return j; } (3)Search in the linked list by number(Method1) LNode *Get_LinkList(LinkList L,int i)

文档评论(0)

1亿VIP精品文档

相关文档