递归H21-LinkedListCode.pdfVIP

  • 1
  • 0
  • 约1.11万字
  • 约 4页
  • 2017-08-11 发布于浙江
  • 举报
递归H21-LinkedListCode

CS106B Handout #21 J Zelenski Feb 4, 2008 Linked list code Were going to take a little diversion into recursive data structures for practice with recursion and pointers. A linked list is a nifty data structure that using pointers to manage a flexible, dynamic collection. Pointers are discussed in section 2.2-2.3 and linked lists are introduced in section 9.5 We will spend a lecture playing around with them to explore the idea of dynamic structures and recursive data and how you can write recursive algorithms to process these lists. Ill be presenting some code already written, and I thought it might help for you to have your own copy of the linked list code to follow along with. The explanatory text and comments are fairly sparse, so youll want to keep awake in lecture. First, heres our node structure: struct Entry { string name, address, phone; Entry *next; }; Each list node contains a pointer to another node, making for a recursive definition. A linked list consists of a node, followed by another, shorter linked list. Each node is dynamically allocated in the heap, using the new operator. The organization of the list is determined by how the pointers are wired up between nodes. Inserting or removing a node is merely a matter of re-writing a few pointers, which makes for a very flexible data structure. Here are the basic operations of creating and printing a node: Entry *GetNewEntry() { cout Enter name (ENTER to quit):; string name = GetLine(); if (name == ) return NULL; Entry *newOne = new Entry; // allocate in heap newOne-name = name; cout Enter address: ; newOne-address = GetLine(

文档评论(0)

1亿VIP精品文档

相关文档