Merge Linked List segmentation fault (core dumped) error please help

This is my code. Everytime I attempted to run it it gives me a segmentation fault and the core dump error. Please help!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include<iostream>
using namespace std;

struct node
{
  int data;
  struct node *next;
};

class list
{
  struct node *start;
  public:
	void create();
	void show();
	void merge(list,list);
};

void list::create()
{
  struct node *nxt_node, *pre_node;
  int value, no, i;
  start=nxt_node=pre_node=NULL;
  cout<<"\nHow many nodes:";
  cin>>no;
  cout<<"Enter "<<no<<" Eelements:";
  for(i=1;i<=no;i++)
  {
	cin>>value;
	nxt_node=new node;
	nxt_node->data=value;
	nxt_node->next=NULL;

	if (start==NULL)
	start=nxt_node;
	else
	pre_node->next=nxt_node;
	pre_node=nxt_node;
  }
  	
}

void list::show()
{
  struct node *ptr=start;

  
  while(ptr!=NULL)
  {
	cout<<ptr->data<<"->'";
	ptr=ptr->next;
  }
}

void list::merge(list l1,list l2)
{
  struct node *nxt_node, *pre_node, *pptr, *qptr;
  int dat;

  pptr=l1.start;
  qptr=l2.start;
  start=nxt_node=pre_node=NULL;
  while(pptr!=NULL && qptr!=NULL)
  {
	if(pptr->data<=qptr->data)
	{
		dat=pptr->data;
		pptr=pptr->next;
	}
	else
	{
		dat=qptr->data;
		qptr=qptr->next;
	}
  	
	nxt_node=new node;
	nxt_node->data=dat;
	nxt_node->next=NULL;
	
	if(start==NULL)
	{
	  start=nxt_node;
	}	
	else
	{
	  pre_node->next=nxt_node;
	  pre_node=nxt_node;
	}
  }
  if(pptr==NULL)
  {
    while(qptr!=NULL)
    {
	nxt_node=new node;
	nxt_node->data=qptr->data;
	nxt_node->next=NULL;
	
	if(start==NULL)
	   start=nxt_node;
	else
	   pre_node->next=nxt_node;
	   pre_node=nxt_node;
	   qptr=qptr->next;	
    }
  }
  else if (qptr==NULL)
  {
	while(pptr!=NULL)
	{
		nxt_node=new node;
		nxt_node->data=pptr->data;
		nxt_node->next=NULL;
		if(start==NULL)
		   start=nxt_node;
		else
		   pre_node->next=nxt_node;
		   pre_node=nxt_node;
		   pptr=pptr->next;
	}
  }
  
  return;
}

int main()
{
	list l1,l2,l3;
	cout<<"Enter the first list in ascending order.";
	l1.create();
	cout<<"Enter the Second list in ascending order.";
	l2.create();
	cout<<"The first list is:";
	l1.show();
	cout<<"The second list is"<<endl;
	l2.show();
	l3.merge(l1,l2);
	l3.show();


return (0);
}

1
2
3
4
5
6
7
8
9
10
11
12
//list::merge
start=nxt_node=pre_node=NULL;
//...
	if(start==NULL)
	{
	  start=nxt_node;
	}	
	else
	{
	  pre_node->next=nxt_node; //crash
	  pre_node=nxt_node;
	}
How would you fix this crash? I have been sitting infront of this thing for to long. My brain is fried.
Have you ever heard of gdb?

(gdb) run
Starting program: /student/mac/JD/Desktop/data 
Reading symbols for shared libraries ++......................... done
Enter the first list in ascending order.
How many nodes:4
Enter 4 Eelements:4 5 6 7
Enter the Second list in ascending order.
How many nodes:4
Enter 4 Eelements:4 5 6 7
The first list is:4->'5->'6->'7->'The second list is

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000008
0x00000001000009ff in list::merge (this=0x7fff5fbff9f0, l1={start = 0x100100a30}, l2={start = 0x100100a70}) at Node.cc:86
86	            pre_node->next=nxt_node;
Last edited on
I have not. But I already know where the crash occurs from ne555 answer. But I need to find out how to correct it.
Topic archived. No new replies allowed.