| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 613 人关注过本帖
标题:线性表的操作。。。为什么一直不让运行呢!?
只看楼主 加入收藏
蜀山小二√
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-10-4
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
线性表的操作。。。为什么一直不让运行呢!?
#include <iostream.h>
#include <malloc.h>

typedef int DataType; /* 定义DataType为int类型 */
typedef struct LNode  
{
    DataType data;
    DataType length;
    struct LNode *next; /* 后继指针 */
    struct LNode *prior; /* 后继指针 */
}LNode,*LinkedList;   /*  单链表的结点类型 */
LNode *p,*q,*head;   /* 定义结构体指针  */

void LinkedListInit()  /* 初始化空链表 */
{
    p = (LNode *)malloc(sizeof(LNode)); /* 开辟新空间 */
    p->next = NULL;
    head->length = 0;
    p->data = NULL;
    head = p;
}

void LinkedListClear(LinkedList L) /* 清空单链表 */
{
    while (head->length > 1)
    {
        q = p->next;
        q->prior = p->prior;
        q = p->prior->next;
        free(p);
        head->length--;
    }

    if (head->length == 1)
    {
    head->next = NULL;
    free(p);
    head->length = 0;
    cout<<"单链表清空完成!"<<endl;
    }

    if(head->length < 1)
    {
        cout<<"单链表本来就是空链表!"<<endl;
    }
}

int LinkedListEmpty(LinkedList L) /* 检查单链表是否为空 */
{   
//    p = head;
    if(head->next == NULL)
        cout<<"此链表为空!"<<endl;
    else
        cout<<"此链不为空!"<<endl;
}

void LinkedListTraverse(LinkedList L) /* 遍历单链表 */
{   
//    p = head;
    if(head->next == NULL)
        cout<<"此链表为空";
    else
        {
            p = head->next;
            cout<<"此链表元素为:"<<p->data<<"-->";
            p = p->next;
            while(p)
                {
                    cout<<p->data<<"-->";
                    p = p->next;
                }
            cout<<"Tail."<<endl;
        }
}

int  LinkedListLength(LinkedList L) /* 求单链表的长度 */
{
//    p = head;
    L->length = 0;
    if(head->next == NULL)
        cout<<"此链表为空,链表长为0."<<endl;
    while(p->next)
        {
            p = p->next;
            head->length++;  
        }/* 记录结点数 */   
    cout<<"链表长为"<<head->length<<endl;
}

void  LinkedListGet(LinkedList L,int  j) /* 从单链表表中查找元素 */
{//L是带头结点的链表的头指针, 返回第 j 个元素
    cout<<"请输入要查找元素的位置:"<<endl;
    int i,a;
    cin>>j;
    if(j+1 <= head->length)
    {
        for(i=1; i=<j; i++)
        {
            p = p->next;
        }      /* 直到找到正确的位置 */
        a = p->data;
        cout<<a<<endl;
    }
    else
        cout<<"输入错误,请认真查看";
}

void  LinkedListLocate(LinkedList L, DataType x) /* 从单链表表中查找与给定元素值相同的元素在链表中的位置 */
{
    cout<<"请输入要查找的元素(数字):"<<endl;
    int i=1,a;
    cin>>a;
    //p = head;
    while(p->next)
    {
        if(p->next->data != a) i++;
        if(p->next->data == a) break;
        p = p->next;
    }
    if(p->next == NULL)
        cout<<"对不起,没有您所要查找的元素!"<<endl;
    else
        cout<<"您所要查找的元素为:"<<i<<endl;
}

void  LinkedListInsert(LinkedList L,int i,DataType  x) /* 向单链表中插入元素 */
{// L 为带头结点的单链表的头指针,本算法在链表中第 i 个结点之前插入新的元素 x
    cout<<"请输入要插入元素的位置:"<<endl;
    cin>>i;
    if (i > head->length)
        cout<<"输入位置有误!"<<endl;
    else
        {
            q = (LNode *)malloc(sizeof(LNode)); /* q为插入元素指针 */
            cout<<"请输入要插入的元素(数字):"<<endl;
            cin>>x;
            q->data = x;
            for(int j=1; j<i; j++) p = p->next; //找到制定位置的前一个元素
            q->next = p->next;
            p->next = q;      /* 元素后移 */
            head->length++;  /* 链表长度加 */
        }
}

void LinkedListDel(LinkedList  L,int x) /* 从单链表中删除元素 */
{//删除以 L 为头指针的单链表中第 x 个结点
    if(head->next == NULL)
        cout<<"此链表为空!"<<endl;
    if(head->next)
    {
        cout<<"请输入要删除的元素的位置:"<<endl;
        int i = 1;
        cin>>x;
        if (x >= head->length || x<0) /* 对x的正确性进行验证 */
            {
                cout<<"输入位置不存在!"<<endl;
            }
        while(p->next)
        {
            if(x == i)
            {
                if(p->next->next == NULL) /* 删除元素在链尾 */
                {
                    q = p->next;
                    free(q); /* 释放空间 */
                    break;
                }
                else
                {
                    q = p->next;
                    p->next = q->next;
                    free(q);
                    head->length--;
                    break;
                }
            }
            p = p->next;
            i++;
        }
    }
}

void  LinkedListCreat(LinkedList L) /* 用尾插法建立单链表 */
{//尾插法建立带头结点的单链表
    L = (LinkedList) malloc (sizeof (LNode));
    L->next = NULL; /* 建立带头结点的单链表 */
    q = L;                       /* 使q指向表尾 */
    cout<<"是否利用尾差法插入元素(Y/N)"<<endl;
    char ch;
    int cs;
    cin>>ch;
    while (ch == 'Y' || ch == 'y')
    {
        cout<<"请输入插入元素(数字):"<<endl;
        cin>>cs;
        p = (LinkedList) malloc (sizeof (LNode));    /* 开辟新位置 */
        p->data = cs;
        p->next = NULL;
        q->next = p;
        q = p; /* 完成尾插 */
        cout<<"是否继续利用尾差法插入元素(Y/N)"<<endl;
        cin>>ch;
    }
     
}

void LinkedListExit()
{
    cout<<"按任意键退出!"<<endl;
    char eh = '*';
    cin>>eh;
    if(eh != '*') exit(0);
}

void main()
{
    cout<<"☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆"<<endl;
    cout<<"☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ①初始化空链表                     ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ②清空单链表                       ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ③检查单链表是否为空               ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ④遍历单链表                       ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑤从单链表表中查找元素             ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑥按元素查位置                     ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑦向单链表中插入元素               ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑧从单链表中删除元素               ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑨用尾插法建立单链表               ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑩退出                             ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆"<<endl;
    cout<<"☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆"<<endl;
cout<<"请选择(按序号):";
    for(;;) /* 无限循环 */
    {
        int i;
        cin>>i;
        switch(i)
        {
         case 1:LinkedListInit();break;
         case 2:LinkedListClear();break;
         case 3:LinkedListEmpty();break;
         case 4:LinkedListTraverse();break;
         case 5:LinkedListGet();break;
         case 6:LinkedListLocate();break;
         case 7:LinkedListInsert();break;
         case 8:LinkedListDel();break;
         case 9:LinkedListCreat();break;
         case 10:LinkedListExit();break;
         default:cout<<"输入错误";break;
        }
    }
}
大神们解围呀~
搜索更多相关主题的帖子: 空间 include 线性表 结构体 
2011-10-04 14:26
jcw08120110
Rank: 8Rank: 8
来 自:南京
等 级:蝙蝠侠
帖 子:272
专家分:742
注 册:2009-6-8
收藏
得分:5 
  吃完晚饭了~ 回头看看 晕发现你的程序到处都是漏洞~~ 汗了~~这个程序不能这样写!!!!!!!!!!!!!!!

给你一个思路~ 你的程序必须逻辑上说的通~       你可以建立一个类型作为结点!然后用这个类型建立对象!建立好的对象 可以拥有的操作!有插入删除...    使用C++就用类~ 别再用C那些过时的东西~  而你所用的这些操作必须独立~ 调用函数参数必须是对的~ 所有这些操作封装在一个类中!  而建立空链表这个也是基本功能可以独立出来~ 构造函数实现这些功能~  

[ 本帖最后由 jcw08120110 于 2011-10-4 19:40 编辑 ]

君生我未生 我生君以老
2011-10-04 15:17
蜀山小二√
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-10-4
收藏
得分:0 
呃。。。原来是这个样子的啊。。。谢谢您啦!
2011-10-05 14:41
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
收藏
得分:15 
修改正确代码如下:(给分哦)
#include <iostream.h>
#include <malloc.h>
#include<stdlib.h>

typedef int DataType; /* 定义DataType为int类型 */
typedef struct LNode  
{
    DataType data;
    DataType length;
    struct LNode *next; /* 后继指针 */
    struct LNode *prior; /* 后继指针 */
}LNode,*LinkedList;   /*  单链表的结点类型 */
LNode *p,*q,*head;   /* 定义结构体指针  */

void LinkedListInit()  /* 初始化空链表 */
{
    p = (LNode *)malloc(sizeof(LNode)); /* 开辟新空间 */
    p->next = NULL;
    p->length = 0;
    p->data = NULL;  //////////////////////???????
    head = p;
}

void LinkedListClear(LinkedList L) /* 清空单链表 */
{
    while (head->length > 1)
    {
        q = p->next;
        q->prior = p->prior;
        q = p->prior->next;
        free(p);
        head->length--;
    }

    if (head->length == 1)
    {
    head->next = NULL;
    free(p);
    head->length = 0;
    cout<<"单链表清空完成!"<<endl;
    }

    if(head->length < 1)
    {
        cout<<"单链表本来就是空链表!"<<endl;
    }
}

void LinkedListEmpty(LinkedList L) /* 检查单链表是否为空 */
{   
//    p = head;
    if(head->next == NULL)
        cout<<"此链表为空!"<<endl;
    else
        cout<<"此链不为空!"<<endl;
}

void LinkedListTraverse(LinkedList L) /* 遍历单链表 */
{   
//    p = head;
    if(head->next == NULL)
        cout<<"此链表为空";
    else
        {
            p = head->next;
            cout<<"此链表元素为:"<<p->data<<"-->";
            p = p->next;
            while(p)
                {
                    cout<<p->data<<"-->";
                    p = p->next;
                }
            cout<<"Tail."<<endl;
        }
}

void  LinkedListLength(LinkedList L) /* 求单链表的长度 */
{
//    p = head;
    L->length = 0;
    if(head->next == NULL)
        cout<<"此链表为空,链表长为0."<<endl;
    while(p->next)
        {
            p = p->next;
            head->length++;  
        }/* 记录结点数 */   
    cout<<"链表长为"<<head->length<<endl;
}

void  LinkedListGet(LinkedList L,int  j) /* 从单链表表中查找元素 */
{//L是带头结点的链表的头指针, 返回第 j 个元素
    cout<<"请输入要查找元素的位置:"<<endl;
    int i,a;
    cin>>j;
    if(j+1 <= head->length)
    {
        for(i=1; i<=j; i++)
        {
            p = p->next;
        }      /* 直到找到正确的位置 */
        a = p->data;
        cout<<a<<endl;
    }
    else
        cout<<"输入错误,请认真查看";
}

void  LinkedListLocate(LinkedList L, DataType x) /* 从单链表表中查找与给定元素值相同的元素在链表中的位置 */
{
    cout<<"请输入要查找的元素(数字):"<<endl;
    int i=1,a;
    cin>>a;
    //p = head;
    while(p->next)
    {
        if(p->next->data != a) i++;
        if(p->next->data == a) break;
        p = p->next;
    }
    if(p->next == NULL)
        cout<<"对不起,没有您所要查找的元素!"<<endl;
    else
        cout<<"您所要查找的元素为:"<<i<<endl;
}

void  LinkedListInsert(LinkedList L,int i,DataType  x) /* 向单链表中插入元素 */
{// L 为带头结点的单链表的头指针,本算法在链表中第 i 个结点之前插入新的元素 x
    //cout<<"请输入要插入元素的位置:"<<endl;
    //cin>>i;
    if (i > head->length)
        cout<<"输入位置有误!"<<endl;
    else
        {
            q = (LNode *)malloc(sizeof(LNode)); /* q为插入元素指针 */
            //cout<<"请输入要插入的元素(数字):"<<endl;
            //cin>>x;
            q->data = x;
            for(int j=1; j<i; j++) p = p->next; //找到制定位置的前一个元素
            q->next = p->next;
            p->next = q;      /* 元素后移 */
            head->length++;  /* 链表长度加 */
        }
}

void LinkedListDel(LinkedList  L,int x) /* 从单链表中删除元素 */
{//删除以 L 为头指针的单链表中第 x 个结点
    if(head->next == NULL)
        cout<<"此链表为空!"<<endl;
    if(head->next)
    {
        cout<<"请输入要删除的元素的位置:"<<endl;
        int i = 1;
        cin>>x;
        if (x >= head->length || x<0) /* 对x的正确性进行验证 */
            {
                cout<<"输入位置不存在!"<<endl;
            }
        while(p->next)
        {
            if(x == i)
            {
                if(p->next->next == NULL) /* 删除元素在链尾 */
                {
                    q = p->next;
                    free(q); /* 释放空间 */
                    break;
                }
                else
                {
                    q = p->next;
                    p->next = q->next;
                    free(q);
                    head->length--;
                    break;
                }
            }
            p = p->next;
            i++;
        }
    }
}

void  LinkedListCreat(LinkedList L) /* 用尾插法建立单链表 */
{//尾插法建立带头结点的单链表
    L = (LinkedList) malloc (sizeof (LNode));
    L->next = NULL; /* 建立带头结点的单链表 */
    q = L;                       /* 使q指向表尾 */
    cout<<"是否利用尾差法插入元素(Y/N)"<<endl;
    char ch;
    int cs;
    cin>>ch;
    while (ch == 'Y' || ch == 'y')
    {
        cout<<"请输入插入元素(数字):"<<endl;
        cin>>cs;
        p = (LinkedList) malloc (sizeof (LNode));    /* 开辟新位置 */
        p->data = cs;
        p->next = NULL;
        q->next = p;
        q = p; /* 完成尾插 */
        cout<<"是否继续利用尾差法插入元素(Y/N)"<<endl;
        cin>>ch;
    }
     
}

void LinkedListExit()
{
    cout<<"按任意键退出!"<<endl;
    char eh = '*';
    cin>>eh;
    if(eh != '*') exit(0);
}

void main()
{
    cout<<"☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆"<<endl;
    cout<<"☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ①初始化空链表                     ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ②清空单链表                       ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ③检查单链表是否为空               ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ④遍历单链表                       ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑤从单链表表中查找元素             ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑥按元素查位置                     ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑦向单链表中插入元素               ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑧从单链表中删除元素               ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑨用尾插法建立单链表               ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆             ⑩退出                             ☆☆"<<endl;
    cout<<"☆☆                                                ☆☆"<<endl;
    cout<<"☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆"<<endl;
    cout<<"☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆"<<endl;
cout<<"请选择(按序号):";
    for(;;) /* 无限循环 */
    {
        int i;
        cin>>i;
        LinkedList L;
        L = (LNode *)malloc(sizeof(LNode));
        switch(i)
        {
         case 1:LinkedListInit();break;
         case 2:LinkedListClear(L);break;
         case 3:LinkedListEmpty(L);break;
         case 4:LinkedListTraverse(L);break;
         case 5:{
                  int j=0;
                  cin>>j;
                 LinkedListGet(L,j);
                 break;
                }
         case 6:{
                 DataType e;
                 cin>>e;
                 LinkedListLocate(L,e);
                 break;
                }
         case 7:{
                 int i;
                 DataType e;
                 cin>>i>>e;
                 LinkedListInsert(L,i,e);
                 break;
                }
         case 8:{
                 int i;
                 cin>>i;
                 LinkedListDel(L,i);
                 break;
                }
         case 9:LinkedListCreat(L);break;
         case 10:LinkedListExit();break;
         default:cout<<"输入错误";break;
        }
    }
}
2011-10-05 16:06
蜀山小二√
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-10-4
收藏
得分:0 
回复 4楼 mengcan555
太感谢您了!
我一定会好好对比学习的!
2011-10-06 12:33
飞扬_佳
Rank: 2
等 级:论坛游民
帖 子:19
专家分:20
注 册:2011-4-14
收藏
得分:0 
我也学学
2011-10-06 16:33
快速回复:线性表的操作。。。为什么一直不让运行呢!?
数据加载中...
 
   



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

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