class list{∥表类定义(嵌套方式) public. 链表操作 p rivate class listnode{∥l套链表结点类 publico int data Listnode links Listnode*rst2*lat;表头和表尾指针
class List { //链表类定义(嵌套方式) public: //链表操作 ……… private: class ListNode { //嵌套链表结点类 public: int data; ListNode *link; }; ListNode *first, *last; //表头和表尾指针 };
单链表中的插入与删除 插入 第一种情况:在第一个结点前插入 newnode-link=first first=newnode; newnode newnode x first (插入前) (插入后)
单链表中的插入与删除 插入 第一种情况:在第一个结点前插入 newnode→link = first ; first = newnode; (插入前) (插入后)
第二种情况:在链表中间插入 newnode-link=p→link p-link= newnode newnan neun x x 插入前) 插入后)
(插入前) (插入后) 第二种情况:在链表中间插入 newnode→link = p→link; p→link = newnode;
第三种情况:在链表末尾插入 newnode-link=p-link; p-link=last= newnode; neon wnoce Last last (插入前) (插入后)
第三种情况:在链表末尾插入 newnode→link = p→link; p→link = last = newnode; (插入前) (插入后)
int List: Insert( const int x, const int i)t /在链表第i个结点处插入新元素x Node p=first; int k=0; while(p !- null & ki-I {p=p→link;k++;}/找第i1个结点 if(p== null & first I = NULL)& cout <<"Invalid position for Insertation!n?: return 0 Node newnode= new Node(x, null); /创建新结点,其数据为x,指针为0
int List::Insert ( const int x, const int i ) { //在链表第 i 个结点处插入新元素 x Node *p = first; int k = 0; while ( p != NULL && k< i -1 ) { p = p→link; k++; } //找第i-1个结点 if ( p == NULL && first != NULL ) { cout << “Invalid position for Insertation!\n”; return 0; } Node *newnode= new Node(x, NULL); //创建新结点,其数据为x,指针为0