首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请问个关于链表的有关问题

2012-03-22 
请教个关于链表的问题.写了个创建链表的程序,目的是用来存放二维数组的元素,编译通过了,可是发现每次创建

请教个关于链表的问题.
写了个创建链表的程序,目的是用来存放二维数组的元素,编译通过了,可是发现每次创建的链表结点个数总是多一个.将链表打印出来发现第一个结点有两个,不知为什么,请教高手!
程序如下:
//   用单向链表结构表示方型二维数组,并求出其非对角线元素之和.
#include "iostream "
using   namespace   std;

struct   LinkNode
{
int   row;     //   行号

int   col;       //   列号

int   data;       //   元素值

LinkNode   *next;
};

void   push(LinkNode   *&head,int   rv,int   cv,int   da)   //   新结点.
{
        LinkNode   *node=new   LinkNode;

node-> row=rv;

node-> col=cv;

node-> data=da;

node-> next=head;

head=node;
}

void   create(LinkNode   *&head,int   num)   //   创建结点.
{
int   mm;

//LinkNode   *head=NULL;

LinkNode   *tail;

cout < < "请输入各结点的数值: " < <endl;

for(int   i=0;i <num;i++)

for(int   j=0;j <num;j++)

{      
cout < < "( " < <i < < "   ,   " < <j < < "): ";

cin> > mm;

if(i==0&&j==0)

{push(head,i,j,mm);

tail=head;}

push(tail-> next,i,j,mm);

tail=tail-> next;
}

}

double   sum(LinkNode   *head,int   num)
{
      double   sm=0;

      LinkNode   *current;

      current=head;

      for(;current!=NULL;current=current-> next)//  
      {
          if((current-> row==current-> col)||(current-> row+current-> col==num-1))

  continue;

  sm+=current-> data;

      }

    return   sm;

}

void   print(LinkNode   *&head)
{
        int   count=0;

while(head!=NULL)
        {
        ++count;

cout < <head-> data < < "   ";

head=head-> next;

if(count%4==0)

cout < <endl;
}

      cout < <count < <endl;
}

int   main()
{
LinkNode   *head=NULL;

int   num;

cout < < "请输入方型二维数组的行数: " < <endl;

cin> > num;

create(head,num);

cout < < "该二维数组非对角线元素之和为: ";

cout < <sum(head,num) < <endl;

print(head);

return   0;
}



[解决办法]
其它没看,可能是这里,你自己再分析分析:
if(i==0&&j==0)

push(head,i,j,mm);
tail=head;
}
else
{
push(tail-> next,i,j,mm);
tail=tail-> next;
}

[解决办法]
你有一个地方写错了,应该是这样的:

#include "iostream "
using namespace std;

struct LinkNode
{
int row; // 行号
int col; // 列号
int data; // 元素值


LinkNode *next;
};

void push(LinkNode *&head,int rv,int cv,int da) // 新结点.
{
LinkNode *node=new LinkNode;
node-> row=rv;
node-> col=cv;
node-> data=da;
node-> next=head;
head=node;
}

void create(LinkNode *&head,int num) // 创建结点.
{
int mm;
//LinkNode *head=NULL;
LinkNode *tail=NULL;
cout < < "请输入各结点的数值: " < <endl;
for(int i=0;i <num;i++)
for(int j=0;j <num;j++)
{
cout < < "( " < <i < < " , " < <j < < "): ";
cin> > mm;
if(i==0&&j==0)
{
push(head,i,j,mm);
tail=head;
}
else     //这里少了
{
push(tail-> next,i,j,mm);
tail=tail-> next;
}
}
}

double sum(LinkNode *head,int num)
{
double sm=0;
LinkNode *current;
current=head;
for(;current!=NULL;current=current-> next)//
{
if((current-> row==current-> col)||(current-> row+current-> col==num-1))
continue;
sm+=current-> data;
}
return sm;
}

void print(LinkNode *&head)
{
int count=0;
while(head!=NULL)
{
++count;
cout < <head-> data < < " ";
head=head-> next;
if(count%4==0)
cout < <endl;
}
cout < <count < <endl;
}

int main()
{
LinkNode *head=NULL;
int num;
cout < < "请输入方型二维数组的行数: " < <endl;
cin> > num;
create(head,num);
cout < < "该二维数组非对角线元素之和为: ";
cout < <sum(head,num) < <endl;
while(head!=NULL)
{
int count=0;
++count;
cout < <head-> data < < " ";
head=head-> next;
if(count%3==0)
cout < <endl;
}
//print(head);
return 0;
}

热点排行