wsf
//程序實現在單鏈表的某給定字符節點后插入新結點算法,如找不到則查在尾部;
//查找給定結點并刪除的算法
//單鏈表逆轉算法#include "iostream.h"const MAXLEN=10;
typedef struct _nodetype{char elem;struct _nodetype* lp;
} node;int myinterface()
{int i;cout<<endl<<" List Operation"<<endl;cout<<" 0 --- Create List"<<endl;cout<<" 1 --- Delete Node"<<endl;cout<<" 2 --- Insert Node"<<endl;cout<<" 3 --- Show List"<<endl;cout<<" 4 --- Reverse List"<<endl;cout<<" 5 --- Exit"<<endl;cin>>i;return i;
}void insert(node*&head,char a)
{//向鏈表頭插入新結點node*p=new node;p->elem=a;p->lp=head->lp;head->lp=p;p=NULL;
}void create(node*& head)
{
// cout<<"head"<<head<<endl;node* p=new node;//創建有空頭節點的鏈表head=p;head->lp=NULL;cout<<"請輸入一串字符,并以?結尾"<<endl;char temp;while(cin>>temp){if(temp=='?') break;insert(head,temp);}cout<<"You Have Established a List, ASSHOLE"<<endl;}void del(node* head,char temp)
{node *p=head;node *q=head->lp;while(q!=NULL && q->elem!=temp){p=q;q=q->lp;}if(q==NULL)cout<<"The Char you want to kill isn't here. Go somewhere else"<<endl;else{p->lp=q->lp;delete q;cout<<"Your enemy is killed, happy?"<<endl;}p=NULL;q=NULL;
}void show(node *&head)
{node*p=head->lp;while(p!=NULL){cout<<p->elem<<'\t';p=p->lp;}cout<<endl;p=NULL;}void total(node*head)
{int num=0;node *p=head->lp;while(p!=NULL){num++;p=p->lp;}cout<<"total number is:"<<num;
}void clear(node *&head)
{node *p=head->lp;while(p!=NULL){del(head,p->elem);p=head->lp;}head=NULL;p=NULL;
}void reverse(node *&head)
{node*p=new node;p->lp=NULL;node*q=head->lp;while(q!=NULL){insert(p,q->elem);q=q->lp;}clear(head);head=p;p=NULL;
}int main(int argc, char* argv[])
{node *r=NULL;int i;char temp;while(i=myinterface(),i>=0&&i<10){
// cout<<'r'<<r<<endl;switch(i){case 0: {create(r);break;}case 1: { char a=NULL;cout<<"Input the Character you desperately want to elimilate"<<endl;while(a==NULL)cin>>a;del(r,a);break;}case 2:{cin>>temp;insert(r,temp);break;}case 3:{show(r);break;}case 4:{reverse(r);break;}case 5:{return 0;}case 6:{total(r);break;}}}return 0;
}
總結
- 上一篇: Jmeter性能测试环境安装
- 下一篇: STM32F429通用定时器(TIM)