求助,,运行程序无限循环“屯”

weixin_41928634 2018-10-02 07:33:07
头文件:
#include<stdio.h>
#include <iostream.h>
#include<stdlib.h>
const int MaxSize=50;
typedef char ElemType;
typedef struct LNode
{
ElemType data;
struct LNode*next;
}LinkNode;

void InitList(LinkNode*&L)
{
L=new LinkNode;
L->next=NULL;
}

void DestroyList(LinkNode*&L)
{
LinkNode *pre=L,*p=L->next;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}

bool ListEmpty(LinkNode*L)
{
return(L->next==NULL);
}

int ListLength(LinkNode*L)
{
int n=0;
LinkNode*p=L;
while(p->next!=NULL)
{
n++;
p=p->next;
}
return(n);
}

void DispList(LinkNode*L)
{
LinkNode*p=L->next;
while(p!=NULL)
cout<<L->data;
cout<<endl;
}

bool GetElem(LinkNode*L,int i,ElemType&e)
{
int j=0;
LinkNode*p=L;
if(i<=0)return false;
while(j<i&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
e=p->data;
return true;
}
}

int LocateElem(LinkNode*L,ElemType e)
{
int i=1;
LinkNode*p=L->next;
while(p!=NULL&&p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)
return(0);
else
return(i);
}

bool ListInsert(LinkNode*&L,int i,ElemType e)
{
int j=0;
LinkNode*p=L,*s;
if(i<=0) return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
s=new LinkNode;
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
}

bool ListDelete(LinkNode*&L,int i,ElemType&e)
{
int j=0;
LinkNode*p=L,*q;
if(i<=0)return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
q=p->next;
if(q==NULL)
return false;
e=p->data;
p->next=q->next;
free(q);
return true;
}
}

主函数:
#include"linklist.h"
#include<stdio.h>
#include<iostream.h>
void InitList(LinkNode*&L);
void DestroyList(LinkNode *&L);
bool ListEmpty(LinkNode*L);
void DispList(LinkNode*L);
bool GetElem(LinkNode*L,int i,ElemType&e);
int LocateElem(LinkNode*L,ElemType e);
bool ListInsert(LinkNode*&L,int i,ElemType e);
bool ListDelete(LinkNode*&L,int i,ElemType&e);
void main()
{
LinkNode *h;
ElemType e;
InitList(h);
ListInsert(h,1,'a');
ListInsert(h,2,'b');
ListInsert(h,3,'c');
ListInsert(h,4,'d');
ListInsert(h,5,'e');
DispList(h);
cout<<"(4)单链表h长度为:"<<ListLength(h)<<endl;
printf("(5)单链表h为%s\n",(ListEmpty(h)?"空":"非空"));
GetElem(h,3,e);
cout<<"单链表h的第3个元素是:"<<e<<endl;
cout<<"元素a的位置:"<<LocateElem(h,'a')<<endl;
ListInsert(h,4,'f');
DispList(h);
ListDelete(h,3,e);
DispList(h);
DestroyList(h);
}
谢谢各位大神!!!
...全文
689 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
SD_LTF 2018-12-16
  • 打赏
  • 举报
回复
没初始化的问题
赵4老师 2018-10-29
  • 打赏
  • 举报
回复
windows里常见的内存填充数据含义
 * 0xABABABAB : Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory
漱 * 0xABADCAFE : A startup to this value to initialize all free memory to catch errant pointers
涵? * 0xBAADF00D : Used by Microsoft's LocalAlloc(LMEM_FIXED) to mark uninitialised allocated heap memory
很? * 0xBADCAB1E : Error Code returned to the Microsoft eVC debugger when connection is severed to the debugger
撅饰 * 0xBEEFCACE : Used by Microsoft .NET as a magic number in resource files
烫烫 * 0xCCCCCCCC : Used by Microsoft's C++ debugging runtime library to mark uninitialised stack memory
屯屯 * 0xCDCDCDCD : Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory
葺葺 * 0xDDDDDDDD : Used by Microsoft's free() or delete to mark freed heap memory
蕲蕲 * 0xDEADDEAD : A Microsoft Windows STOP Error code used when the user manually initiates the crash
 * 0xFDFDFDFD : Used by Microsoft's C++ debugging heap to mark "no man's land" guard bytes before and after allocated heap memory
 * 0xFEEEFEEE : Used by Microsoft's HeapFree() to mark freed heap memory
赵4老师 2018-10-29
  • 打赏
  • 举报
回复
windows里常见的内存填充数据含义
 * 0xABABABAB : Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory
漱 * 0xABADCAFE : A startup to this value to initialize all free memory to catch errant pointers
涵? * 0xBAADF00D : Used by Microsoft's LocalAlloc(LMEM_FIXED) to mark uninitialised allocated heap memory
很? * 0xBADCAB1E : Error Code returned to the Microsoft eVC debugger when connection is severed to the debugger
撅饰 * 0xBEEFCACE : Used by Microsoft .NET as a magic number in resource files
烫烫 * 0xCCCCCCCC : Used by Microsoft's C++ debugging runtime library to mark uninitialised stack memory
屯屯 * 0xCDCDCDCD : Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory
葺葺 * 0xDDDDDDDD : Used by Microsoft's free() or delete to mark freed heap memory
蕲蕲 * 0xDEADDEAD : A Microsoft Windows STOP Error code used when the user manually initiates the crash
 * 0xFDFDFDFD : Used by Microsoft's C++ debugging heap to mark "no man's land" guard bytes before and after allocated heap memory
 * 0xFEEEFEEE : Used by Microsoft's HeapFree() to mark freed heap memory
动力风暴 2018-10-28
  • 打赏
  • 举报
回复
楼上厉害!
楼主可以单步调试下。对自己使用的内存进行初始化,应该就没有“屯”了。
zangfong 2018-10-02
  • 打赏
  • 举报
回复
1. new和delete搭配,malloc和free搭配,正确配对,干活不累 2. 你的DispList函数,这个肯定不对啊。。。 linklist.h
#include<stdio.h>
#include <iostream>
#include<stdlib.h>
using namespace std;
const int MaxSize=50;
typedef char ElemType;
typedef struct LNode
{
    ElemType data;
    struct LNode*next;
} LinkNode;

void InitList(LinkNode*&L)
{
    L=new LinkNode;
    L->next=NULL;
}

void DestroyList(LinkNode*&L)
{
    LinkNode *pre=L,*p=L->next;
    while(p!=NULL)
    {
        delete pre;
        pre=p;
        p=pre->next;
    }
    delete pre;
}

bool ListEmpty(LinkNode*L)
{
    return(L->next==NULL);
}

int ListLength(LinkNode*L)
{
    int n=0;
    LinkNode*p=L;
    while(p->next!=NULL)
    {
        n++;
        p=p->next;
    }
    return(n);
}

void DispList(LinkNode*L)
{
    LinkNode*p=L->next;
    while(p!=NULL)
    {
        cout<<p->data;
        p = p->next;
    }

    cout<<endl;
}

bool GetElem(LinkNode*L,int i,ElemType&e)
{
    int j=0;
    LinkNode*p=L;
    if(i<=0)return false;
    while(j<i&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
        return false;
    else
    {
        e=p->data;
        return true;
    }
}

int LocateElem(LinkNode*L,ElemType e)
{
    int i=1;
    LinkNode*p=L->next;
    while(p!=NULL&&p->data!=e)
    {
        p=p->next;
        i++;
    }
    if(p==NULL)
        return(0);
    else
        return(i);
}

bool ListInsert(LinkNode*&L,int i,ElemType e)
{
    int j=0;
    LinkNode*p=L,*s;
    if(i<=0) return false;
    while(j<i-1&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
        return false;
    else
    {
        s=new LinkNode;
        s->data=e;
        s->next=p->next;
        p->next=s;
        return true;
    }
}

bool ListDelete(LinkNode*&L,int i,ElemType&e)
{
    int j=0;
    LinkNode*p=L,*q;
    if(i<=0)return false;
    while(j<i-1&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
        return false;
    else
    {
        q=p->next;
        if(q==NULL)
            return false;
        e=p->data;
        p->next=q->next;
        delete q;
        return true;
    }
}

main.cpp
#include"linklist.h"
#include<stdio.h>
#include<iostream>
void InitList(LinkNode*&L);
void DestroyList(LinkNode *&L);
bool ListEmpty(LinkNode*L);
void DispList(LinkNode*L);
bool GetElem(LinkNode*L,int i,ElemType&e);
int LocateElem(LinkNode*L,ElemType e);
bool ListInsert(LinkNode*&L,int i,ElemType e);
bool ListDelete(LinkNode*&L,int i,ElemType&e);

int main()
{
    LinkNode *h;
    ElemType e;
    InitList(h);
    ListInsert(h,1,'a');
    ListInsert(h,2,'b');
    ListInsert(h,3,'c');
    ListInsert(h,4,'d');
    ListInsert(h,5,'e');
    DispList(h);
    cout<<"(4)单链表h长度为:"<<ListLength(h)<<endl;
    printf("(5)单链表h为%s\n",(ListEmpty(h)?"空":"非空"));
    GetElem(h,3,e);
    cout<<"单链表h的第3个元素是:"<<e<<endl;
    cout<<"元素a的位置:"<<LocateElem(h,'a')<<endl;
    ListInsert(h,4,'f');
    DispList(h);
    ListDelete(h,3,e);
    DispList(h);
    DestroyList(h);
    return 0;
}

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧