| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 374 人关注过本帖
标题:一大堆错误,帮我改改
只看楼主 加入收藏
mike_ge
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-5-8
结帖率:0
收藏
已结贴  问题点数:10 回复次数:1 
一大堆错误,帮我改改
带头结点的循环链表表示队列 只设一个队尾指针,写置空,判队空,入队,出队,初始化队

#include <stdio.h>
#include<stdio.h>
#include<malloc.h>

typedef struct node{
        int data;
    struct node *next;
}Linklist;

typedef struct{
    Linklist *rear;
}LinkQ;

LinkQ *SetQueue(){
    LinkQ *Q;
    Q=(LinkQ *)malloc(sizeof(LinkQ));
    Q->rear->next=Q->rear;
    return Q;
}

int EmptyQueue(LinkQ *Q){
    if(!Q->rear)
        return 1;
    else
        return 0;
}

LinkQ *Add(LinkQ *Q,int x){
    LinkQ *p;
    p=(LinkQ *)malloc(sizeof(LinkQ));
    p->rear->data=x;
    p->next=Q->rear->next;
    Q->rear->next=p;
    Q->rear=p;
    return Q;
}

LinkQ *Delete(LinkQ *Q){
    LinkQ *p;
    if(!EmptyQueue){
        p=Q->rear->next;
        Q->rear->next=p->next;
        free(p);
    }
    return Q;
}

LinkQ InitQueue(LinkQ *Q){
    Q->rear=Q->rear->next;
    return Q;
}

void main(){
    int n,choice;
    LinkQ *S;
    do{
    printf("1.建空队\n");
    printf("2.判空队\n");
    printf("3.入队\n");
    printf("4.出队\n");
    printf("5.置空队\n");
    printf("6.退出\n");
    printf("请选择:");
    scanf("%d",&choice);
    switch(choice){
    case 1:SetQueue(); break;
    case 2:EmptyQueue(S);break;
    case 3:printf("输入元素,以-1结束:");
        scanf("%d",&n);
        while(n!=-1){
        Add(S,n);
        scanf("%d",&n);
        };break;
    case 4:Delete(S);break;
    case 5:InitQueue(S);break;
    }
    }while(choice!=6);
}






搜索更多相关主题的帖子: return 
2011-05-08 23:15
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:10 
程序代码:
/*带头结点的循环链表表示队列FIFO*/
#include <iostream>
using namespace std;

class Node
{
public:
    Node(Node *node=NULL, int data=0)
    {
        data = 0;
        next = node;
    }
public:
    int data;
    Node *next;
};

class Link_Q
{
public:
    Link_Q();//构造函数
    bool EmptyQueue();//判断队列是否为空
    bool EnQueue(Node *node);//进队列
    Node * DeQueue();//出队列
    void PrintQueue();//打印输出
    void ResetQueue();//重置

private:
    Node *rear;
};

Link_Q::Link_Q()
{//构造队列
    rear = new Node();
    rear->next = rear;
}

bool Link_Q::EmptyQueue()
{//判断队列是否为空 若为空则返回true 否则返回false
    if (rear == rear->next)
    {
        return true;
    }

    return false;
}

bool Link_Q::EnQueue(Node *node)
{//进队列 成功返回true 失败返回false
    Node *temp = rear->next;
   
    while (temp->next != rear)
    {
        temp = temp->next;
    }
    node->next = temp->next;
    temp->next = node;

    return true;
}

Node * Link_Q::DeQueue()
{//摘掉第一个有效数据结点
    Node *temp = rear->next;
    rear->next = temp->next;

    return temp;
}

void Link_Q::PrintQueue()
{//打印输出队列信息
    Node *temp = rear->next;

    while (temp != rear)
    {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;
}

void Link_Q::ResetQueue()
{
    Node *temp = rear->next;
    while (temp != rear)
    {
        rear->next = temp->next;
        delete temp;
        temp = rear->next;
    }
}

void Menu()
{
    cout << "\t1. 建立空队列" << endl;
    cout << "\t2. 判断队列是否为空" << endl;
    cout << "\t3. 入队列" << endl;
    cout << "\t4. 出队列" << endl;
    cout << "\t5. 队列置空" << endl;
    cout << "\t6. 打印队列" << endl;
    cout << "\t7. 退出" << endl;
}

void deal(Link_Q &queue, int choice)
{
    switch (choice)
    {
    case 1:
        break;
    case 2:
        if (queue.EmptyQueue())
        {
            cout << "是空队列" << endl;
        }
        else
        {
            cout << "不是空队列" << endl;
        }
        break;
    case 3:
        Node *te;
        te = new Node();
        cout << "输入数据:" ;
        cin >> te->data;
        queue.EnQueue(te);
        break;
    case 4:
        Node *temp;
        temp = queue.DeQueue();
        cout << "出队列的元素为" << temp->data << endl;
        delete temp;
        break;
    case 5:
        queue.ResetQueue();
        break;
    case 6:
        queue.PrintQueue();
        break;
    case 7:
        return;
        break;
    default:
        cout << "未定义" << endl;
    }
}

void factory()
{
    int choice;
    Link_Q queue;

    while (true)
    {
        Menu();
        cin >> choice;
        deal(queue, choice);
       
        if (7 == choice)
        {
            return;
        }
    }
}

int main()
{
    factory();

    return 0;
}
2011-05-09 20:53
快速回复:一大堆错误,帮我改改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013880 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved