데이터 구조 탑승 실험: 단일 체인 테이블 조작

6630 단어 데이터 구조
#include <iostream>
#include <malloc.h>
using namespace std;

typedef struct Node
{
    char c;
    struct Node *next;
}*LinkList,LNode;

//      h
LinkList Init(LinkList &h)
{
    h=(LNode*)malloc(sizeof(LNode));
    if(h==NULL)
    {
        cout<<"         "<<endl;
        exit(0);
    }
    h->next=NULL;
    return h;
}
//       
void InsertNum(LinkList &h,int n)
{
    LinkList r,s;
    r=h;
    for(int i=0;i<n;i++)
    {
        s=(LNode*)malloc(sizeof(LNode));
        r->next=s;
        cin>>s->c;
        r=s;
    }
    r->next=NULL;
}
//     h
void outputLinkList(LinkList h)
{
    LinkList p=h->next;
    while(p!=NULL)
    {
        cout<<p->c<<" ";
        p=p->next;
    }
}
//    h   
int GetLength(LinkList h)
{
    LinkList p;
    p=h->next;
    int count=0;
    while(p!=NULL)
    {
        count++;
        p=p->next;
    }
    return count;
}
//        
void outputListLength(LinkList h)
{
    cout<<"        : "<<GetLength(h)<<endl;
}
//     h     
void Empty(LinkList h)
{
    if (h->next==NULL)
        cout<<"     !"<<endl;
    else
        cout<<"      !"<<endl;
}
//     h  n   
void outputNthNum(LinkList h,int n)
{
    int curLen=GetLength(h);
    if(n<=0||n>curLen)
    {
        cout<<"      !"<<endl;
        exit(0);
    }
    else
    {
        LinkList p=h->next;
        for(int i=1;i<n;i++)
        {
            p=p->next;
        }
        cout<<"     "<<n<<"    : "<<p->c<<endl;
    }
}
//    a   
void outputNumPosition(LinkList h,char a)
{
    LinkList p=h->next;
    int position=1;
    while(p!=NULL)
    {
        if(p->c==a)
        {
            cout<<"  "<<a<<"         :"<<position<<endl;
        }
        p=p->next;
        position++;
    }
    if(position-GetLength(h)>1)
        cout<<"  "<<a<<"       !"<<endl;
}
//  N        f
void InsertNumOnth(LinkList &h,int n,char a)
{
    int len=GetLength(h);
    if(n<=0||n>len)
    {
        cout<<"    ,    !"<<endl;
        exit(0);
    }
    else
    {
        LinkList p=h->next;
        LinkList temp;
        temp=(LNode*)malloc(sizeof(LNode));
        if(n==1)
        {
            temp->next=p;
            h->next=temp;
            temp->c=a;
        }
        else
        {
            for(int i=2;i<n;i++)
            {
                p=p->next;
            }
            temp->next=p->next;//3
            p->next=temp;
            temp->c=a;
        }
    }
}
//      n   
void deleteNthNum(LinkList &h,int n)
{
    if(n<=0||n>GetLength(h))
    {
        cout<<"        !"<<endl;
        exit(0);
    }
    else
    {
       int j=0;
       LinkList p=h,q;
       while(j<n-1&&p!=NULL)
       {
           j++;
           p=p->next;
       }
       q=p->next;
       p->next=q->next;
       free(q);
    }
}
//    
void freeList(LinkList &h)
{
    free(h);
}

int main()
{
    LinkList h;
    cout<<"           ,         :"<<endl;
    cout<<endl;
    cout<<"1.                2.  n              3.     "<<endl;
    cout<<"4.             5.      n      6.    a   "<<endl;
    cout<<"7.             8.  n       f  9.   n   "<<endl;
    cout<<endl;
    cout<<"    1~8      ,  0     !(         ,          )"<<endl;
    cout<<"     :"<<endl;
    int op;
    Init(h);
    while(cin>>op&&op)
    {
        switch(op)
        {
        case 2:
            {
                int n;
                cout<<"  n  :";cin>>n;
                cout<<"     n    :";
                InsertNum(h,n);
                cout<<"    !"<<endl;
                break;
            }
        case 3:
            {
                cout<<"     :"; outputLinkList(h);
                cout<<endl;break;

            }
        case 4:
            {
                outputListLength(h);break;
            }
        case 5:
            {
                int n;
                cout<<"  n  :";cin>>n;
                outputNthNum(h,n);break;
            }
        case 6:
            {
                char a;
                cout<<"  a  :";cin>>a;
                outputNumPosition(h,a);break;
            }
        case 7:
            {
                Empty(h);break;
            }
        case 8:
            {
                int n;char f;
                cout<<"  n  : ";cin>>n;
                cout<<"  f  : ";cin>>f;
                InsertNumOnth(h,n,f);
                cout<<"    !"<<endl;
                break;
            }
        case 9:
            {
                int n;
                cout<<"  n  : ";cin>>n;
                deleteNthNum(h,n);
                cout<<"    !"<<endl;
                break;
            }
        }
    }
    return 0;
}

실행:

좋은 웹페이지 즐겨찾기