M
MJ
Hi
I have created a prog for link list
I am addig a node, displaying the LL contents, reversing the LL and
deleting the LL
In the delete() if I use free I am getting memory exception
I have used malloc in the add()at the same time
If I used new and delete its working fine
Can any one tell me the reason
I have given the prog below for your referecne
Mayur
-------------------------------------------------
//This is the program to create a linkedlist
// and reverse the linkedlist
#include <iostream.h>
#include <malloc.h>
//------------------------------------------
typedef struct node
{
int value;
struct node *next;
}mynode;
//------------------------------------------
//Global Variables
mynode *head,*tail,*temp, *head1;
//------------------------------------------
//Display the contents of the linked list
Display()
{
mynode *temp1;
temp1 = head;
while (temp1) {
cout << temp1->value << "\n";
temp1 = temp1->next;
}
return 0;
}
//------------------------------------------
//Append a node in the linked list
void Add(int value)
{
//temp = (mynode *) malloc(sizeof(mynode*));
temp = new mynode;
//temp = (mynode *) malloc(sizeof(mynode*));
temp->value = value;
temp->next = (mynode *)0;
if(head == (mynode *)0) {
head = temp;
}
else
{
mynode *temp1;
temp1 =head;
while(temp1->next != (mynode*)0){
temp1 = temp1->next;
}
temp1->next = temp;
}
}
//------------------------------------------
//Reverse the linked list
mynode * reverse(mynode *ptr)
{
mynode *temp1;
if(!(ptr && ptr->next))
{
head1 = ptr;
return ptr;
}
temp1 = reverse(ptr->next);
temp1->next = ptr;
if(head == ptr)
{
head->next = (mynode*)0;
head = head1;
}
return ptr;
}
//------------------------------------------
//Delete the linked list
void Delete()
{
while(head)
{
mynode *temp;
temp = head;
head = head->next;
//free(temp);
delete temp;
}
}
//------------------------------------------
//Main Program
int main(void)
{
for (int i =1 ; i <= 20; i++) {
Add(i);
}
Display();
reverse(head);
cout << "--------------------\n";
Display();
Delete();
return 0;
}
//------------------------------------------
-------------------------------------------------
I have created a prog for link list
I am addig a node, displaying the LL contents, reversing the LL and
deleting the LL
In the delete() if I use free I am getting memory exception
I have used malloc in the add()at the same time
If I used new and delete its working fine
Can any one tell me the reason
I have given the prog below for your referecne
Mayur
-------------------------------------------------
//This is the program to create a linkedlist
// and reverse the linkedlist
#include <iostream.h>
#include <malloc.h>
//------------------------------------------
typedef struct node
{
int value;
struct node *next;
}mynode;
//------------------------------------------
//Global Variables
mynode *head,*tail,*temp, *head1;
//------------------------------------------
//Display the contents of the linked list
Display()
{
mynode *temp1;
temp1 = head;
while (temp1) {
cout << temp1->value << "\n";
temp1 = temp1->next;
}
return 0;
}
//------------------------------------------
//Append a node in the linked list
void Add(int value)
{
//temp = (mynode *) malloc(sizeof(mynode*));
temp = new mynode;
//temp = (mynode *) malloc(sizeof(mynode*));
temp->value = value;
temp->next = (mynode *)0;
if(head == (mynode *)0) {
head = temp;
}
else
{
mynode *temp1;
temp1 =head;
while(temp1->next != (mynode*)0){
temp1 = temp1->next;
}
temp1->next = temp;
}
}
//------------------------------------------
//Reverse the linked list
mynode * reverse(mynode *ptr)
{
mynode *temp1;
if(!(ptr && ptr->next))
{
head1 = ptr;
return ptr;
}
temp1 = reverse(ptr->next);
temp1->next = ptr;
if(head == ptr)
{
head->next = (mynode*)0;
head = head1;
}
return ptr;
}
//------------------------------------------
//Delete the linked list
void Delete()
{
while(head)
{
mynode *temp;
temp = head;
head = head->next;
//free(temp);
delete temp;
}
}
//------------------------------------------
//Main Program
int main(void)
{
for (int i =1 ; i <= 20; i++) {
Add(i);
}
Display();
reverse(head);
cout << "--------------------\n";
Display();
Delete();
return 0;
}
//------------------------------------------
-------------------------------------------------