构造一个带头结点的双链表,实现前插,后插,删除一系列操作。要带头结点啊!谢谢!
谢谢啊!
#include <iostream> using namespace std; static UINT m_x=0; struct Node{//BYTE == unsigned char UINT == uisigned int BYTE mm;//数据结构段 UINT nn;//标记结点 struct Node *my_head; struct Node *my_end; }; struct Node *head; bool CreateMyList(){//创建 Node *p1,*p2; do{ p1=new Node; cin>>p1->mm; m_x+=1; p1->nn = m_x; if(p1->mm==0) break; if(p1->nn==1) p2 = head = p1; else{ p2 ->my_end = p1; p1->my_head = p2; p2 = p1; } }while(true); if(p1->nn == 1){ delete head; return 1; } p2->my_end = head; head->my_head = p2; return 1; } bool DeleteMyList(int i){ Node *p1 = head; Node *p2; do{ if(p1->nn == i){ p2=p1; goto next; } p1 = p1->my_end; }while(p1->nn!=head->nn); return 0; next: if(p2->nn == head->nn){ head = head->my_end; p2->my_head->my_end= head; head->my_head = p2->my_head; delete p2; return 1; } else if(p2->nn == head->my_head->nn){ head->my_head = p2->my_head; p2->my_head->my_end = head; delete p2; return 1; } else{ p2->my_head->my_end = p2->my_end; p2->my_end->my_head = p2->my_head; delete p2; return 1; } } void PrintMyList(){ Node *p2 = head; do{ cout<<"head->mm="<<p2->mm<<" head->nn="<<p2->nn<<endl; p2 = p2->my_end; }while(p2->nn!=head->nn); } int main(){ CreateMyList(); PrintMyList(); DeleteMyList(1); PrintMyList(); DeleteMyList(3); PrintMyList(); return 1; }