链表中指针指向未声明的可怕之处

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Diviner_wb/article/details/79117623
#include <iostream>
#include <stdlib.h>
#include <malloc.h>
#include <string>
#include <bits/stdc++.h>
#define len sizeof(Lnode)
using namespace std;
typedef struct Lnode
{
    int id;
    float score;
    char name[100];
    struct Lnode *next;
} Lnode,*Linklist;
Linklist info()
{
    int n;
    cout<<"请输入您要增加学生的数量"<<endl;
    cin>>n;//学生数量


    Lnode *p,*head,*tail;
    head=(Linklist)malloc(len);
    head->next=NULL;
    tail=head;


    for(int i=0; i<n; i++)
    {
        p=(Linklist)malloc(len);
        p->next=NULL;
        cout<<"请输入学生的学号:"<<endl;
        cin>>p->id;
        getchar();
        cout<<"请输入学生的姓名:"<<endl;
        cin>>p->name;
        cout<<"请输入学生的分数:"<<endl;
        cin>>p->score;


        tail->next=p;
        tail=p;
    }
    cout<<"增加学生信息成功!"<<endl;
    cout<<endl;
    return head;
}


void show_all(Lnode *head)
{
    Lnode *q;
    q = head->next;
    while(q!=NULL)
    {
        if(q!=NULL)
        {
            cout<<"学号:";
            cout<<q->id<<endl;
            cout<<"姓名:";
            cout<<q->name<<endl;
            cout<<"分数:";
            cout<<q->score<<endl;
        }


        /*if(q->next==NULL)
        {
            break;
        }*/
        cout<<endl;
        q=q->next;
    }
}
Linklist change(Lnode *head)
{
    int flag=0,num;//提示用户是否修改成功
    cout<<"请输入您要修改的学生的学号:"<<endl;
    cin>>num;//学生的学号
    Lnode *q;
    q=head->next;
    while(q!=NULL)
    {
        if(num==q->id)
        {
            cout<<"请输入您要修改的姓名:"<<endl;
            cin>>q->name;
            cout<<"请输入您要修改的分数:"<<endl;
            cin>>q->score;
            flag=1;
            break;
        }
        else
        {
            q=q->next;
        }
    }
    if(flag==1)
    {
        cout<<"修改成功!"<<endl;
    }
    else
    {
        cout<<"修改失败!"<<endl;
    }


}
/*Linklist delet(Lnode *head)
{
     Lnode *p,*q;
     p=head;
     while(p!=NULL)
     {
      q=p->next;
      while(q!=NULL)
      {
      if(q->data==id)
      {
      p->next=q->next;
      free(q);
      }
           else
           {
            p=p->next;
            q=q->next;
           }
      }
     }
}*/
Linklist add(Lnode *head)
{
    Lnode *q;
    q=head->next;
    while(q->next!=NULL)
    {
        q=q->next;
    }
    Lnode *t;
    t=(Linklist)malloc(len);
    cout<<"请输入新增学生的学号:"<<endl;
    cin>>t->id;
    cout<<"请输入新增学生的姓名:"<<endl;
    cin>>t->name;
    cout<<"请输入新增学生的分数:"<<endl;
    cin>>t->score;
    q->next=t;
}
void showMain()
{
    cout<<"欢迎来到学生信息管理系统"<<endl;
    cout<<"1.【初始化学生信息】"<<endl;
    cout<<"2.【显示学生信息】"<<endl;
    cout<<"3.【修改学生信息】"<<endl;
    cout<<"4.【新增学生信息】"<<endl;
    cout<<"0.【退出本系统啊】"<<endl;
}
int main()
{
    int Flag=1;
    Lnode *head;
    showMain();
    int choose;
    while(Flag)
    {
        cout<<"请输入你的选择"<<endl;
        cin>>choose;
        switch(choose)
        {
        case 1:
            head=info();
            break;
        case 2:
            show_all(head);
            break;
        case 3:
            change(head);
            break;
        case 4:
            add(head);
            break;
        case 0:
            Flag=0;
            break;
        }
    }
    cout<<"谢谢使用!"<<endl;
    return 0;
}
展开阅读全文

链表中存储的是指向int型的指针的问题

10-07

链表中存储的是指向int型的指针的问题 rnrn#include rn#include rntypedef struct linkedListStruct *linkedList;rntypedef struct mapStruct *map;rntypedef void *poly;rntypedef void *key;rntypedef void *value;rnstruct linkedListStructrnrn map data;rn linkedList next;rn;rnrnstruct mapStructrnrn poly key;rn poly value;rn;rnrnrnvoid mapInsert(linkedList m, key k, value v)rnrn linkedList l_temp = (linkedList)malloc(sizeof(*l_temp));rn rn map m_temp = (map)malloc(sizeof(*m_temp));rn m_temp->key = k;rn m_temp->value = v;rnrn l_temp->data = m_temp; rnrn l_temp->next = m->next;rn m->next = l_temp;rn return;rnrnlinkedList newLinkedList ()rnrn linkedList l = (linkedList)malloc(sizeof (*l));rn l->data = NULL;rn l->next = NULL;rn return l;rnrnrnint main ()rnrn linkedList m = newLinkedList();rn int ik;rn int iv;rn void *k;rn void *v;rn printf("请输入映射:");rn while( scanf("%d %d",&ik,&iv) == 2)rn rnrn k = &ik;rn v = &iv;rn mapInsert( m, k, v);rn rnrn return 0;rnrn程序如上,当我分别输入 1 2 3 4 时: rn首先:k指向1,v指向2,并把指针k和v放入到链表中的第一个存储结点, rn然后:k指向3,v指向4, 并把指针k和v放入到链表中的第二个存储结点, rn问题就出在这里了,链表中的第一个存储结点也指向3和4了, rn我想法是让第一个存储结点指向1和2,第二个存储结点指向3和4 rnrn我需要怎么改main()中的输入部分啊? rnrn在线等各位大侠的帮忙了,谢谢! 论坛

链表指针的问题

07-28

请看下面的程序:rn#includernclass CATrnrnpublic:rn CAT ()itsAge=1;rn CAT(int age):itsAge(age)rn ~CAT();rn int GetAge()constreturn itsAge;rnprivate:rn int itsAge;rn;rn#if !defined(AFX_NODE_H__67D9A589_1C2B_449F_B9FB_FEF64A828786__INCLUDED_)rn#define AFX_NODE_H__67D9A589_1C2B_449F_B9FB_FEF64A828786__INCLUDED_rnrn#if _MSC_VER > 1000rn#pragma oncern#endif // _MSC_VER > 1000rnrnclass Node rnrnpublic:rn rn Node();rn Node(CAT*);rn virtual ~Node();rn void SetNext (Node*node)itsNext=node;rn Node*GetNext()constreturn itsNext;rn CAT*GetCat()constreturn itsCat;rn void Insert(Node*);rn void Display();rn rnprivate:rn CAT*itsCat;rn Node*itsNext;rn rnrn;rn#include "Node.h"rnrn//////////////////////////////////////////////////////////////////////rn// Construction/Destructionrn//////////////////////////////////////////////////////////////////////rnrnNode::Node(CAT*pCat):rnitsCat(pCat),rnitsNext(0)rnrnrnrnrnrnNode::~Node()rnrncout<<"deleting node...\n;";rnrndelete itsCat;rnitsCat=0;rndelete itsNext;rnitsNext=0;rnrnrnvoid Node::Insert(Node *newNode)rnrnif (!itsNext)rnitsNext=newNode;rnelsernrn int NextCatAge=itsNext->GetCat()->GetAge();rn int NewAge=newNode->GetCat()->GetAge();rn int ThisNodeAge=itsCat->GetAge();rn rnrn if (NewAge>ThisNodeAge&&NewAgeSetNext(itsNext);rn itsNext=newNode;rn rn elsern itsNext->Insert(newNode);rnrnrnrnvoid Node::Display()rnrnif (itsCat->GetAge()>0)rncout<<"Mycat is"<GetAge()<<"years old\n";rnif (itsNext)rnitsNext->Display();rnrnrnrn#endif // !defined(AFX_NODE_H__67D9A589_1C2B_449F_B9FB_FEF64A828786__INCLUDED_)rnvoid main()rnrn Node*pNode=0;rn CAT *pCat=new CAT(0);rn int age;rn Node *pHead=new Node(pCat);rn while(1)rn rn cout<<"new cat's age?(0 to quit):";rn cin>>age;rn if(!age)rn break;rn pCat=new CAT (age);rn pNode=new Node(pCat);rn pHead->Insert(pNode);rn rn pHead->Display();rn delete pHead;rn cout<<"Existing...\n\n";rnrnrn教科书上说if (NewAge>ThisNodeAge&&NewAgeSetNext(itsNext);rn itsNext=newNode;rn上面两句就能实现新Node指向当前Node所指向的同一位置,当前Node指向新Node.rn我不明白itsNext已经有了地址,再用newNode->SetNext(itsNext);会发生什么,rn再用itsNext=newNode;又会发生什么,请高手指点,这个程序我已经看了三天了,还是不明白它是如何运行的。 论坛

链表指向的内存问题

09-12

void polydd(PolyList polya, PolyList polyb,PolyList polyc)rn/*将两个多项式相加,然后将和多项式存放在多项式polya中,并将多项式polyb删除*/rnrn printf("\t\t开始\n\n");rn Polynode *p,*q,*r,*s;rn int sum;rn p=polya->next;rn /*令p和q分别指向两个多项式链表中的第一个节点*/rn q=polyb->next;rn r=polyc;rn while (p->next!=NULL&&q->next!=NULL)rn rn if (p->expexp)rn /*规则1,如果p指向的多项式的指数小于q的指数,将p节点加入到多项式和中*/rn rn printf("\t\tAcoef=p->coef;rn s->exp=p->exp;rn r->next=s;rn r=s;rn p=p->next;rn rn else if(p->exp==q->exp)rn /*规则2:若指数相等,则相应的系数相加*/rn rn printf("\t\tA==b\n");rn sum=p->coef+q->coef;rn if (sum!=0)rn /*如果系数和不是零,则系数和置入节点p,释放节点q,并将指针后移*/rn rn s=(Polynode *)malloc(sizeof(Polynode));rn s->coef=sum;rn s->exp=p->exp;rn r->next=s;rn r=s;rn p=p->next;rn q=q->next;rn rn printf("%4d\n",sum);rn rn elsern /*规则3:将q节点加入到"和多项式中"*/rn rn printf("\t\tA>B\n");rn s=(Polynode *)malloc(sizeof(Polynode));rn s->coef=q->coef;rn s->exp=q->exp;rn r->next=s;rn r=s;rn q=q->next;rn rn rn if (p->next!=NULL)rn rn printf("\t\tA wanl \n");rn s=(Polynode *)malloc(sizeof(Polynode));rn s->coef=q->coef;rn s->exp=q->exp;rn r->next=s;rn r=s;rn p=p->next;rn rn// r->next=p; /*多项式A中还有剩余,则将剩余的节点加入到多项式中*/rn elsern rn printf("\t\tB wan l \n\n");rn s=(Polynode *)malloc(sizeof(Polynode));rn s->coef=p->coef;rn s->exp=p->exp;rn r->next=s;rn r=s;rn p=p->next;rn rn rnrnrnvoid Print(PolyList poly)rnrn Polynode *r;rn r=poly->next;rn while (r!=NULL)rn rn printf("%d,%d; ",r->coef,r->exp);rn r=r->next;rn rn printf("\n\n");rnrnrnPolyList polya, PolyList polyb,PolyList polyc为三个链表表头,用尾插法建立polya 和polyb,为什么按照上面的规则运算之后,再将polyc输出就会便是内存错误? 论坛

没有更多推荐了,返回首页