我如何在下面的代码中解决我的问题 [英] How can I solve my problem in the below code
本文介绍了我如何在下面的代码中解决我的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <iostream>
using namespace std;
typedef struct node{
public:
struct node *column;
bool flag;
struct node *row;
int ii,jj;
union{
int val;
struct node *next;
}u;
}nod,*ptr;
static ptr strt1;
static ptr strt2;
static ptr y;
int t;
int tt;
void AddLink(int i,int j){
int count = 0;
ptr q = strt2;
ptr p = strt1;
for (int k=0;k<i;k++){
q = q->u.next;
}
for (int k=0;k<j;k++){
p = p->u.next;
}
if (q->column==NULL && p->row==NULL){
ptr m = (node *)malloc(sizeof(node));
q->column = m;
m->u.val = 1;
m->column = q;
m->ii = i;
m->flag = false;
p->row = m;
m->row = p;
m->jj= j;
count =1;
}
if (q->column!=NULL && p->row ==NULL){
ptr h=q;
ptr w = q;
q = q->column;
while (q->jj<j&&q!=w){
h =q;
q = q->column;
}
ptr mm = (node *)malloc(sizeof(node));
h->column = mm;
mm->column = q;
mm->ii = i;
mm->u.val = 1;
mm->flag = false;
mm->jj = j;
mm->row = p;
count = 1;
p->row = mm;
}
if (p->row!=NULL && q->column==NULL){
ptr hh=p;
ptr ww = p;
p = p->row;
while (p->ii<i&&p!=ww){
hh =p;
p = p->row;
}
ptr mmm = (node *)malloc(sizeof(node));
hh->row = mmm;
mmm->row = p;
mmm->jj = j;
mmm->u.val = 1;
mmm->flag = false;
mmm->ii = i;
mmm->column = q;
count = 1;
q->column = mmm;
}
if (p->row!=NULL && q->column!=NULL && count==0){
ptr hhh = p;
ptr www = p;
ptr g = q;
ptr gg = q;
q = q->column;
p = p->row;
while (q->jj<j&&q!=gg){
g =q;
q = q->column;
}
while ((p->ii)<i&&p!=www){
hhh =p;
p = p->row;
}
if (q->jj!=j && p->ii!=i){
ptr mmmm = (node *)malloc(sizeof(node));
hhh->row = mmmm;
mmmm->row = p;
g->column = mmmm;
mmmm->column = q;
mmmm->flag = false;
mmmm->u.val = 1;
mmmm->ii = i;
mmmm->jj = j;
}
else{
p->u.val +=1;
}
}
}
void DeleteLink(int i,int j){
ptr q = strt2;
ptr p = strt1;
for (int k=0;k<i;k++){
q = q->u.next;
}
ptr w =q;
ptr g = q;
q = q->column;
while (g->jj<j&&q!=w){
q = g->column;
if (q->jj==j){
q->u.val-=1;
}
g=q;
}
}
int RetrieveValue(int i,int j){
ptr q = strt2;
ptr p = strt1;
int c = 0;
for (int k=0;k<i;k++){
q = q->u.next;
}
ptr w =q;
ptr g = q;
q = q->column;
while (g->jj<j&&q!=w){
q = g->column;
if (q->jj==j){
c = q->u.val;
}
g=q;
}
return c;
}
int RetrieveRowSumUptoKthColumn(int i,int j){
ptr p = strt1;
int c=0;
for (int k=0;k<i;k++){
p = p->u.next;
}
ptr w = p;
p = p->row;
while (p->ii<=j&&p!=w){
c+=p->u.val;
p = p->row;
}
return c;
}
int RetrieveColumnSumUptoKthRow(int i,int j){
ptr q=strt2;
int c=0;
for (int k=0;k<i;k++){
q= q->u.next;
}
ptr w = q;
q = q->column;
while (q->jj<=j &&q!=w){
c+=q->u.val;
q = q->column;
}
return c;
}
int m;
int n;
ptr a;
ptr b;
int main(){
a = (node *)malloc(sizeof(node));
b = (node *)malloc(sizeof(node));
strt1 = a;
strt2 = b;
cout<<a->u.next;
cout<<a->column;
cout<<a->row;
for(int i=0;i<10000000;i++){
ptr ar =(node *) malloc(sizeof(node));
ar->flag = true;
ar->jj = i;
a->u.next = ar;
a = a->u.next;
}
a->u.next = strt1;
for(int i=0;i<10000000;i++){
ptr br =(node *) malloc(sizeof(node));
br->ii = i;
br->flag = true;
b->u.next = br;
b = b->u.next;
}
b->u.next = strt2;
int e;
int cc;
cin>>cc;
for(int i=0;i<cc;i++){
cin>>e;
cin>>m;
cin>>n;
if (e==1)
AddLink(m,n);
if (e==2)
DeleteLink(m,n);
if (e==3)
cout<<RetrieveValue(m,n)<<endl;
if (e==4)
cout<<RetrieveRowSumUptoKthColumn(m,n)<<endl;
if (e==5)
cout<<RetrieveColumnSumUptoKthRow(m,n)<<endl;
}
}
我尝试过的事情:
在执行代码时,我在这里遇到分段错误.请解决此问题,即确切告诉我问题出在哪里.
What I have tried:
Here i am getting segmentation fault on executing the code.Please solve this problem i.e. tell me exactly where the problem is occuring.
推荐答案
查找导致seg的行故障需要调试您的应用程序.
但是,您的代码有三个大问题:
Finding the line that results in the seg fault requires debugging your application.
However, your code has three big problems:
- 您尚未初始化分配的结构的成员
- 您没有检查有效的指针(对于链接列表,使用NULL初始化指针并进行检查是必不可少的)
- 您拥有一个联合身份,可以在其中以
int
身份访问struct node *next
成员
- You did not initialise the members of your allocated structures
- You did not check for valid pointers (it is essential for linked lists to initialise pointers with NULL and check them)
- You have a union where you can access the
struct node *next
member asint
特别是最后一个将在此行中创建一个无效的指针:
Especially the last one will create an invalid pointer in this line:
q->u.val-=1;
我的建议:
- 在分配后初始化所有结构成员,方法是将它们设置为零或使用
calloc()
而不是malloc()
- 使用它们之前,请检查结构中的所有指针是否为
NULL
- 请勿使用此类联合
- Initialise all struct members after allocation by setting them to zero or use
calloc()
instead ofmalloc()
- Check all pointers from the struct for being
NULL
before using them
- Don''t use such kinds of unions
My suggestions:
这篇关于我如何在下面的代码中解决我的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文