Download presentation
Presentation is loading. Please wait.
Published byBarry Whitehead Modified over 8 years ago
2
第二章 线性表 第二节 线性链表
3
5. 线性表的链式表示 顺序表的优点是可以随机选取表中元素 缺点是插入删除操作复杂。 用指针将互不相连的内存结点串成的 线性表叫线性链表。 结点 node 由一个数据元素域,一个或几个 指针域组成。单链表的结点只有一个指针域。
4
几个结点,前一个结点的指针, 指向后一个结点,就连接成一个 线性链表。 线性链表的优点则是插入,删除 快捷,缺点是选取复杂。
5
6. 结点类的定义 #include <stdlib.h 〉 #include template class Node { Node *next; //next 是下一个结点的地址 public: T data; // the data is public Node (const T& item, Node * ptrnext = NULL); // list modification methods void InsertAfter(Node *p); Node *DeleteAfter(void); // obtain the address of the next node Node *NextNode(void) const; };
6
// constructor. initialize data and // pointer members template Node ::Node(const T& item, Node * ptrnext) : data(item), next(ptrnext) ) { }
7
// return value of private member next template Node * Node ::NextNode(void) const { return next; }
8
// insert a node p after the current one template void Node ::InsertAfter(Node *p) { // p points to successor of the current // node, and current node points to p. p->next = next; next = p; }
9
// delete the node following current and return its address template Node * Node ::DeleteAfter(void) { // save address of node to be deleted Node * tempPtr = next; // if there isn't a successor, return NULL if (next == NULL) return NULL; // current node points to successor of tempPtr. next = tempPtr->next; // return the pointer to the unlinked node return tempPtr; }
10
2 .人工建立一个链表 void main(void) { Node *a,*b,*c; a=new Node ('a'); b=new Node ('b'); c=new Node ('c'); Node *head,*p; head=new Node (' '); p=head; head->InsertAfter(a); head->InsertAfter(b); head->InsertAfter(c); while(p!=NULL) { cout data<<" "; p=p->NextNode( ); } } 测试结果:打印 c b a
11
3. 定义线性链表的一些操作 #include "node.h" // allocate a node with data member item and pointer nextPtr template Node *GetNode(const T& item, Node *nextPtr = NULL) { Node *newNode; // allocate memory while passing item and NextPtr to // constructor. terminate program if allocation fails newNode = new Node (item, nextPtr); if (newNode == NULL) { cerr << "Memory allocation failure!" << endl; exit(1); } return newNode; }
12
enum AppendNewline {noNewline,addNewline}; template // print a linked list void PrintList(Node *head, AppendNewline addnl = noNewline) { // currPtr chains through the list, starting at head Node *currPtr = head; // print the current node's data until end of list while(currPtr != NULL) { // output newline if addl == addNewline if(addnl == addNewline) cout data << endl; else cout data << " "; // move to next node currPtr = currPtr->NextNode( ); } }
13
// find an item in a linked list head; return TRUE and // value of previous pointer if found; otherwise return FALSE template int Find(Node *head, T& item, Node * &prevPtr) { Node *currPtr = head; // begin traversal at first node prevPtr = NULL; // cycle through the list until end of list while(currPtr != NULL) { if (currPtr->data == item) {item = currPtr->data;return 1;} prevPtr = currPtr; currPtr = currPtr->NextNode( );} return 0; // failed to locate item }
14
// insert item at the front of list template void InsertFront(Node * & head, T item) { // allocate new node so it points to original list head // update the list head head = GetNode(item,head); }
15
// find rear of the list and append item template void InsertRear(Node * & head, const T& item) { Node *newNode, *currPtr = head; if (currPtr == NULL) // if list is empty, insert item at the front InsertFront(head,item); else { // find the node whose pointer is NULL while(currPtr->NextNode( ) != NULL) currPtr = currPtr->NextNode( ); // allocate node and insert at rear (after currPtr) newNode = GetNode(item); currPtr->InsertAfter(newNode); }
16
// delete the first node of the list template void DeleteFront(Node * & head) { // save the address of node to be deleted Node *p = head; // make sure list is not empty if (head != NULL) { // move head to second node and delete original head = head->NextNode( ); delete p; }
17
// delete the first occurrence of key in the list template void Delete (Node * & head, T key) { Node *currPtr = head, *prevPtr = NULL; if (currPtr == NULL) return; while (currPtr != NULL && currPtr->data != key) { prevPtr = currPtr; currPtr = currPtr->NextNode( ); } if (currPtr != NULL) { if(prevPtr == NULL) head = head->NextNode(); else prevPtr->DeleteAfter(); delete currPtr; } }
18
// insert item into the ordered list template void InsertOrder(Node * & head, T item) { Node *currPtr, *prevPtr, *newNode; prevPtr = NULL; currPtr = head; while (currPtr != NULL) { if (item data) break; prevPtr = currPtr; currPtr = currPtr->NextNode( ); } if (prevPtr == NULL) InsertFront(head,item); else { newNode = GetNode(item); prevPtr->InsertAfter(newNode); } }
19
// delete all the nodes in the linked list template void ClearList(Node * &head) { Node *currPtr, *nextPtr; currPtr = head; while(currPtr != NULL) { nextPtr = currPtr->NextNode(); delete currPtr; currPtr = nextPtr; } head = NULL; }
20
// insert item into the ordered list template void InsertOrder(Node * & head, T item) { Node *currPtr, *prevPtr, *newNode; prevPtr = NULL; currPtr = head; while (currPtr != NULL) { if (item data) break; prevPtr = currPtr; currPtr = currPtr->NextNode( ); } if (prevPtr == NULL) InsertFront(head,item); else { newNode = GetNode(item); prevPtr->InsertAfter(newNode); } }
21
链表插入排序 #include #pragma hdrstop #include "node.h" #include "nodelib.h"
22
template void LinkSort(T a[], int n) { Node *ordlist = NULL, *currPtr; int i; for (i=0;i < n;i++) InsertOrder(ordlist, a[i]); currPtr = ordlist; i = 0; while(currPtr != NULL) { a[i++] = currPtr->data; currPtr = currPtr->NextNode( ); } ClearList(ordlist); }
23
// scan the array and print its elements void PrintArray(int a[], int n) { for(int i=0;i < n;i++) cout << a[i] << " "; }
24
/*void main(void) { // initialized array with 10 integer values int A[10] = {82,65,74,95,60,28,5,3,33,55}; LinkSort(A,10); // sort the array cout << "Sorted array: "; PrintArray(A,10); // print the array cout << endl; } */ #endif // NODE_LIBRARY
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.