回 帖 发 新 帖 刷新版面

主题:js算法,是用单链表编写的,里面有一点问题,请指教!

#include <iostream.h>
#include <malloc.h>
#include <iomanip.h>
#include <fstream.h>
//#include <stdlib.h>

//using namespace std;

typedef int day;
typedef int count;
typedef double value;

typedef struct linknode
{
    count queue;
    day workdays;
    value price;
    struct linknode *next;
}nodetype;


void print()
{
      int i;
    cout<<endl;
        for(i=1;i<40;i++)
        {
            cout<<"*";
        }
        cout<<endl;
}
nodetype *create()
{
    day queue;
    day workdays;
    value price;

    nodetype *h=NULL,*s,*t,*h0;

    int i=1;

    cout<<"输入一个单链表:"<<endl<<endl;
    while(1)
    {
         cout<<"输入第 "<<i<<" 个节点queue域值:";
        cin>>queue;
        cout<<"输入第 "<<i<<" 个节点workdays域值:";
         cin>>workdays;
        cout<<"输入第 "<<i<<" 个节点price域值:";
         cin>>price;

        
         print();

        if(price==0||queue==0||workdays==0)
            break;
          if(i==1)
            {
                h=(nodetype *)malloc(sizeof(nodetype));
                h->queue=queue;
                h->workdays=workdays;
                h->price=price;
                h->next=NULL;
                t=h;
            }
         else
            {
                 s=(nodetype *)malloc(sizeof(nodetype));
                 s->queue=queue;
                 s->workdays=workdays;
                 s->price=price;
                 s->next=NULL;
                 t->next=s;
                 t=s;
            }
     i++;
    }
    h0=(nodetype *)malloc(sizeof(nodetype));
    h0->queue=0;
    h0->workdays=0;
    h0->price=0;
    h0->next=h;
    h=h0;
    return(h);
}

void disp(nodetype *h)
{
    nodetype *p;
    p=h->next;
    if(p==NULL)
        cout<<"空表!"<<endl;
    
    else
    {
        cout<<setw(10)<<"queue"<<setw(10)<<"workdays"<<setw(10)<<"price"<<endl;
         while(p!=NULL)
            { 
                cout<<setw(10)<<p->queue<<setw(10)<<p->workdays<<setw(10)<<p->price<<endl;
                p=p->next;
            }
    }

    cout<<endl;
}
int locate1(nodetype *h,nodetype *s)
{
    int i=1;
    nodetype *p=h;
    while(p->next!=s)
    {
        p=p->next;
        i++;
    }
  return(i);
}//locate1
nodetype *locate2(nodetype *h,nodetype *s)
{
    nodetype *p=h;

    while(p->next!=s)
        p=p->next;

return(p);
}//locate2
void dispose(nodetype *h)
{
    nodetype *pa=h,*pb;
    if(pa!=NULL)
    {
        pb=pa->next;
        if(pb==NULL)
            free(pa);
        else
        {
            while(pb!=NULL)
            {
                free(pa);
                pa=pb;
                pb=pb->next;
            }
        free(pa);
        }
    }
}//dispose


转下页!
[em7]

回复列表 (共1个回复)

沙发

提个意见,一个主题不要发多个帖子,帖子是不断流动的,这样会很浪费

同一个主题发在一起,发不下的自己回复发。

PS:什么是JS算法?

我来回复

您尚未登录,请登录后再回复。点此登录或注册