데이터 구조의 링크 의 기본 조작

4910 단어
링크 의 기본 조작.
#include<iostream>
#include<cstring>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
using namespace std;
typedef int ElemType;
typedef int Status;

typedef struct LNode{
	ElemType data;
	struct LNode *next;
}LNode,*LinkList;
Status InitList(LinkList &L){//        
	L=new LNode;
	L->next=NULL;
	return OK;
}

Status InputList(LinkList &L,ElemType n){//            
      LNode *p,*r;//LNode *p LinkList p        
      int i;
   //   L=new LNode;
      r=L;
      cout<<"   "<<n<<"   :"<<endl;
      for(i=0;i<n;i++){ //for(i=n-1;i>=0;i--)      
         p=new LNode;
         cin>>p->data;
         p->next=NULL;
         r->next=p;
         r=p;
      }
    return OK;
}

Status GetNumber(LinkList L,int i,ElemType &num){//                  
     LNode *p;
     int j;
     j=0;
     p=L->next;
     while(p&&j<i){
        p=p->next;
        ++j;
     }
     if(!p||j>i)
        return ERROR;
     num=p->data;
     return OK;
}

Status GetAdress(LinkList L,ElemType num){//                    
    LNode *p;
    int cas=1;
    p=L->next;
    while(p&&p->data!=num){
       p=p->next;//printf("right
"); ++cas; } return cas; } Status InsertList(LinkList &L,int i,ElemType &num){// LNode *p,*s; int j=0; p=L; while(p&&j<i-1){ p=p->next; ++j; } if(!p||j>i-1) return ERROR; s=new LNode; s->data=num; s->next=p->next; p->next=s; return OK; } Status DeleteList(LinkList &L,int i,ElemType &n){// LNode *p,*q; p=L; int j=0; while(p->next&&j<i-1){ p=p->next; ++j; // cout<<"ERROR"<<endl; } if(!(p->next)||j>i-1) return ERROR; q=p->next; p->next=q->next; n=q->data; delete q; return OK; } void OutputList(LinkList L){// LinkList p; p=L->next; while(p){ // cout<<ERROR; cout<<p->data<<" "; p=p->next; } cout<<endl; // return OK; } int main(){ int temp,num_a,num_b,num_c,num_d; LinkList L; LinkList R; int num_deal,flag; cout<<"1.
"; cout<<"2.
"; cout<<"3.
"; cout<<"4.
"; cout<<"5.
"; cout<<"6.
"; cout<<"7.
"; cout<<"0.

"; while(true){ cout<<" :"<<endl; cin>>temp; if(temp==0) break; flag=0; switch(temp){ case 1: flag=InitList(L); if(flag) cout<<" !"<<endl; else cout<<" !"<<endl; break; case 2: int ans_fir; cout<<" :"<<endl; cin>>ans_fir; flag=InputList(L,ans_fir); if(flag) cout<<" !"<<endl<<endl; else cout<<" !"<<endl<<endl; break; case 3: int ans_sec; cout<<" :"<<endl; cin>>ans_sec; flag=GetNumber(L,ans_sec-1,num_a); if(flag) cout<<" "<<num_a<<endl<<endl; else cout<<" !"<<endl<<endl; break; case 4: int ans_thi; cout<<" :"<<endl<<endl; cin>>ans_thi; flag=GetAdress(L,ans_thi); if(flag) cout<<" "<<flag<<endl<<endl; else cout<<" !"<<endl; break; case 5: int ans_for,ans_fif; cout<<" :"<<endl<<endl; cin>>ans_for>>ans_fif; flag=InsertList(L,ans_for,ans_fif); if(flag) cout<<" !"<<endl<<endl; else cout<<" !"<<endl<<endl; break; case 6: int ans_six; cout<<" :"<<endl<<endl; cin>>ans_six; flag=DeleteList(L,ans_six,num_c); if(flag) cout<<" :"<<num_c<<endl<<endl; else cout<<" !"<<endl<<endl; break; case 7: OutputList(L); break; default: cout<<" !"<<endl<<endl; break; } } return 0; }

좋은 웹페이지 즐겨찾기