Doubly Linked List insert function - data-structures

I am having problem with this code.Can someone help me to fix it ?
Problem is in insert function especially with malloc(allocating).I don't understand why I cannot malloc.It is not printing the debug text.
#include<stdlib.h>
#include<stdio.h>
#define TRUE 1
#define FALSE 0
struct Node{
int data;
struct Node* next;
struct Node* prev;
};
struct ListRecord
{
struct Node *head;
struct Node *tail;
int length;
};
typedef struct ListRecord *DoubleList;
DoubleList createList()
{
return NULL;
}
DoubleList MakeEmptyList(DoubleList l)
{
l = (struct ListRecord*)malloc(sizeof(struct ListRecord));
if(l==NULL)
{
printf("Memory allocation failed!");
}
else
{
l->head->next=NULL;
l->head->prev=NULL;
l->tail=l->head;
l->length=0;
}
}
DoubleList InsertListAtPosition(DoubleList l,int pos,int val)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
printf("debug");
if(newNode==NULL)
{
printf("debug");
newNode->data=val;
newNode->next=NULL;
newNode->prev=NULL;
if(pos > l->length+1)
{
pos=l->length+1;
}
else if(pos==l->length+1)
{
struct Node *iterator=l->head;
while(iterator->next != NULL)
{
iterator=iterator->next;
}
iterator->next=newNode;
newNode->prev=iterator;
newNode->next=NULL;
}
else
{
struct Node* iterator=l->head;
int i;
for(i=0;i<pos;i++)
iterator=iterator->next;
newNode->next=iterator->next;
newNode->prev=iterator;
iterator->next->prev=newNode;
iterator->next=newNode;
}
l->length++;
}
else
{
printf("Memory allocation failed!");
}
}
int DeleteListAtPosition(DoubleList l,int pos)
{
int value;
if(l==NULL)
{
printf("List is empty");
return 0;
}
else
{
if(pos==1)
{
struct Node* iterator=l->head;
value=iterator->data;
free(iterator);
return value;
}
else
{
struct Node* iterator=l->head;
int i;
for(i=1;i<pos;i++)
iterator=iterator->next;
if(iterator->next==NULL)
{
value=iterator->data;
free(iterator);
return value;
}
else
{
iterator->prev->next=iterator->next;
iterator->next->prev=iterator->prev;
free(iterator);
}
}
}
}
void printList(DoubleList l) {
struct Node* temp = l->head;
printf("Forward: ");
while(temp != NULL) {
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
int main(){
DoubleList myList;
int exit,pos,value;
char command;
myList=createList();
exit=FALSE;
while(!exit)
{
fflush(stdin);
printf("\nMenu:\n m)akeEmpty\n i)nsert\n e)xit\n");
scanf("%c", &command);
fflush(stdin);
switch(command)
{
case 'm':
myList = MakeEmptyList(myList);
break;
case 'i':
printf("Enter position to be added: ");
scanf("%d",&pos);
printf("Enter value to be added: ");
scanf("%d",&value);
InsertListAtPosition(myList,pos,value);
//printList(myList);
break;
case 'e':
exit = TRUE;
break;
default:
printf("command not recognized\n");
break;
}
}
printf("\n\n");
system("PAUSE");
return 0;
}

Here is the working code,
#include<stdlib.h>
#include<stdio.h>
#include <bits/stdc++.h>
#define TRUE 1
#define FALSE 0
using namespace std;
struct Node{
int data;
struct Node* next;
struct Node* prev;
};
struct ListRecord
{
struct Node *head;
struct Node *tail;
int length;
};
typedef struct ListRecord *DoubleList;
DoubleList createList()
{
return NULL;
}
DoubleList MakeEmptyList()
{
DoubleList l;
l = (struct ListRecord*)malloc(sizeof(struct ListRecord));
if(l==NULL)
{
printf("Memory allocation failed!");
}
else
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->next = NULL;
newNode->prev = NULL;
l->head = newNode;
l->tail=l->head;
l->length=0;
}
return l;
}
DoubleList InsertListAtPosition(DoubleList l,int pos,int val)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if(newNode != NULL)
{
newNode->data=val;
newNode->next=NULL;
newNode->prev=NULL;
if(pos > l->length)
{
pos=l->length+1;
}
else if(pos==l->length)
{
struct Node *iterator=l->head;
while(iterator->next != NULL)
{
iterator=iterator->next;
}
iterator->next=newNode;
newNode->prev=iterator;
newNode->next=NULL;
}
else
{
struct Node* iterator=l->head;
int i;
for(i=0;i<pos;i++)
iterator=iterator->next;
newNode->next=iterator->next;
newNode->prev=iterator;
iterator->next->prev=newNode;
iterator->next=newNode;
}
l->length++;
}
else
{
printf("Memory allocation failed!");
}
}
int DeleteListAtPosition(DoubleList l,int pos)
{
int value;
if(l==NULL)
{
printf("List is empty");
return 0;
}
else
{
if(pos==1)
{
struct Node* iterator=l->head;
value=iterator->data;
free(iterator);
return value;
}
else
{
struct Node* iterator=l->head;
int i;
for(i=1;i<pos;i++)
iterator=iterator->next;
if(iterator->next==NULL)
{
value=iterator->data;
free(iterator);
return value;
}
else
{
iterator->prev->next=iterator->next;
iterator->next->prev=iterator->prev;
free(iterator);
}
}
}
}
void printList(DoubleList l) {
struct Node* temp = l->head;
printf("Forward: ");
while(temp != NULL) {
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
int main(){
DoubleList myList;
int exit,pos,value;
char command;
exit=FALSE;
while(!exit)
{
fflush(stdin);
printf("\nMenu:\n m)akeEmpty\n i)nsert\n e)xit\n");
scanf("%c", &command);
fflush(stdin);
switch(command)
{
case 'm':
myList = MakeEmptyList();
break;
case 'i':
printf("Enter position to be added: ");
scanf("%d",&pos);
printf("Enter value to be added: ");
scanf("%d",&value);
if(myList == NULL) {
myList = MakeEmptyList();
}
InsertListAtPosition(myList,pos,value);
//printList(myList);
break;
case 'e':
exit = TRUE;
break;
default:
printf("command not recognized\n");
break;
}
}
printf("\n\n");
system("PAUSE");
return 0;
}
So you were making the following mistakes,
Your MakeEmptyList method is returning nothing, it should return l.
Inside you insert you were checking with length + 1, instead of only length, so it was causing pointer related errors.

Related

Inaccuracy in output of deletion function in binary search tree

I am making a binary search tree code, in that 'am facing an issue in the deletion function. After executing the function, the code is showing inorder display incorrectly. Basically, it is incorrect, and I'am not able to understand where the problem is.
In the delete function, I have used a search function that will find the node which the user has to delete. And I have used inorder successor method for deletion so the minimum function will find the successor. All other functions like insertion, minimum, and inorder are working fine.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
} node;
struct node *insert(struct node *tnode, int data)
{
if (tnode == NULL)
{
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = data;
ptr->left = NULL;
ptr->right = NULL;
return ptr;
}
if (data > (tnode->data))
{
tnode->right = insert(tnode->right, data);
return tnode;
}
if (data < (tnode->data))
{
tnode->left = insert(tnode->left, data);
return tnode;
}
}
void in_order(struct node *tnode)
{
if (tnode == NULL)
return;
else
{
in_order(tnode->left);
printf("%d\t", tnode->data);
in_order(tnode->right);
}
}
struct node *search(struct node *tnode, int data)
{
if (tnode == NULL)
{
return NULL;
}
else
{
if (data == tnode->data)
{
return tnode;
}
else
{
if (data > tnode->data)
{
search(tnode->right, data);
}
else
{
search(tnode->left, data);
}
}
}
}
struct node *minimum(struct node *tnode)
{
if (tnode == NULL)
{
printf("No node present\n");
return NULL;
}
else
{
if (tnode->left == NULL)
return tnode;
else
minimum(tnode->left);
}
}
struct node *deletenode(struct node *tnode, int data)
{
struct node *s;
s = search(tnode, data);
if (s == NULL)
{
printf("Value not found\n");
return NULL;
}
else
{
if (s->left == NULL && s->right == NULL) // No child case
{
free(s);
return NULL;
}
else
{
if (s->left == NULL && s->right != NULL) // single child case(right child)
{
struct node *temp = s->right;
free(s);
return temp;
}
else if (s->right == NULL && s->left != NULL) // single child case(left child)
{
struct node *temp = s->left;
free(s);
return temp;
}
else
{
struct node *temp;
temp = minimum(s->right);
s->data = temp->data;
s->right = deletenode(s->right, temp->data);
}
}
}
return s;
}
int main()
{
struct node *root = NULL, *del;
int n, i, data, key;
printf("Enter how many nodes you have to enter in a tree\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter data\n");
scanf("%d", &data);
root = insert(root, data);
}
printf("\nIN-Order display\n");
in_order(root);
int d;
printf("\nEnter data to be delete\n");
scanf("%d", &d);
del=deletenode(root, d);
printf("\nIN-Order display\n");
in_order(root);
return 0;
}
There are several places missing return (in minimum, search and deletenode). You can easily find them if you compile with -Wall -Wextra compiler options.
It is not clear, what do you want to return from deletenode. I assume that it is a tree with deleted element. Then either search should return a parent node (so it is possible to change pointers to freed node), or deletenode should move to parent node.
Also, use in_order(del) instead of in_order(root) because root can be changed while deleting.

Error in code when searching through the right subtree in my binary search tree

In one of my classes at Uni we are creating Binary search trees and inserting data and looking them up. My code make sense in my head and because of this I cannot find the error anywhere. I have spent ages trying to find the error but cannot find it anywhere. The only thing that might be causing an error is that the precompiled headers didn't work when I started so i removed them from my project. The error only occurrs when i try to use the BST.Lookup and choose a key that is on the right subtree.
This is my main cpp file:
// BinarySearchTrees.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "BST.h"
#include <iostream>
#include <fstream>
#include <string>
void ReadFile(BST &Bst)
{
int iKey;
std::string Key;
std::string Data;
std::ifstream testFile("Test.txt");
if (testFile.is_open())
{
while (!testFile.eof())
{
getline(testFile, Key, ' ');
getline(testFile, Data);
iKey = stoi(Key);
Bst.Insert(iKey, Data);
}
}
}
int main()
{
std::string Option;
int Choice;
BST BST;
//ReadFile(BST);
BST.Insert(6, "Oscar");
BST.Insert(20, "Ben");
BST.Insert(99, "James");
BST.Insert(1, "Alex");
while (Option != "exit")
{
std::cout << "If you wish to Lookup a Node, Insert value to find. Enter 'exit' to close" << std::endl;
getline(std::cin, Option);
if (Option == "exit")
break;
else
{
Choice = stoi(Option);
BST.Lookup(Choice);
}
}
return 0;
}
I believe that the readfile code may be incorrect but am unsure.
My Binary Search Tree Class:
#include "BST.h"
struct BST::Node {
Key key;
Item item;
Node* leftChild;
Node* rightChild;
Node(Key, Item);
};
void BST::Insert(Key inputKey, Item inputItem)
{
Node* previousNode = nullptr;
if (root == nullptr)
{
root = new Node(inputKey, inputItem);
}
else
{
InsertRec(inputKey, inputItem, root, previousNode);
}
}
void BST::InsertRec(Key inputKey, Item inputItem, Node* & Current, Node* & previousNode)
{
if (Current != nullptr)
{
previousNode = Current;
}
bool isLeft = false;
if (!isLeaf(Current))
{
if (inputKey > Current->key)
{
isLeft = false;
InsertRec(inputKey, inputItem, Current->rightChild, previousNode);
}
else if (inputKey < Current->key)
{
isLeft = true;
InsertRec(inputKey, inputItem, Current->leftChild, previousNode);
}
else
{
Current->item = inputItem;
}
}
else
{
Current = new Node(inputKey, inputItem);
if (isLeft)
{
previousNode->leftChild = Current;
}
else
{
previousNode->rightChild = Current;
}
}
}
BST::Item* BST::Lookup(Key soughtKey)
{
Item* Item = LookupRec(soughtKey, root);
std::string Display = /*std::to_string(soughtKey) + ": " + */ *Item;
std::cout << Display << std::endl;
return Item;
}
BST::Item* BST::LookupRec(Key soughtKey, Node* currentNode)
{
if (!isLeaf(currentNode))
{
if ((currentNode->key > soughtKey))
{
LookupRec(soughtKey, currentNode->leftChild);
}
else if ((currentNode->key < soughtKey))
{
LookupRec(soughtKey, currentNode->rightChild);
}
else
{
return &currentNode->item;
}
}
else
{
return nullptr;
}
}
bool BST::isLeaf(Node* n)
{
if (nullptr == n)
{
return true;
}
else
{
return false;
}
}
BST::BST()
{
}
BST::Node::Node(Key K, Item I)
{
key = K;
item = I;
leftChild = nullptr;
rightChild = nullptr;
}
And finally my header file for the binary search tree:
#pragma once
#include "iostream"
#include "string"
class BST
{
public:
using Key = int;
using Item = std::string;
void Insert(Key, Item);
Item* Lookup(Key);
BST();
private:
struct Node;
Node* root = nullptr;
static bool isLeaf(Node*);
static Item* LookupRec(Key, Node*);
static void InsertRec(Key, Item, Node* &, Node* &);
};
Any help would be greatly appreciated. I've been stuck on this for too long and I cannot progress without fixing this first.
The Test.txt file is filled with keys and items that are read and inputted like how I do it manually at the start of my main function, so I dont think the error is the file data.
Thanks in advance
UPDATE: Have finally found the error. The problem was with the bool isLeft in my InsertRec function. The bool was always false due to the recursion so have changed the code to compare previousNode->Key with Current->Key to determine if the child goes left or right

Find the Error or memory leak

I was writing LinkedList Program in C++
Here is the Code. Find the Error and Memory Leak or anything else.
I want to write a perfect code.
#ifndef LEARN_REVERSELINKLIST_H
#define LEARN_REVERSELINKLIST_H
#include <string>
#include <utility>
#include <iostream>
#include <boost/assert.hpp>
class ReverseLinkList {
private:
struct Node {
std::string string;
Node *Previous{};
Node *Next{};
};
Node *head;
Node *tail;
int count;
public:
ReverseLinkList();
~ReverseLinkList();
void insert(std::string string);
void insertAt(std::string string, int position);
void iterator() const;
void reverseIterator() const;
void remove(const std::string &string);
void removeAt(int position);
int size() const;
Node *getHead() const;
Node *getTail() const;
};
#endif //LEARN_REVERSELINKLIST_H
and Here is the CC File
#include "ReverseLinkList.h"
ReverseLinkList::~ReverseLinkList() {
if (count != 0) {
Node *temp = head;
while (temp != tail->Next) {
delete temp->Previous;
temp = temp->Next;
}
tail = nullptr;
head = nullptr;
}
}
ReverseLinkList::ReverseLinkList()
: head(nullptr), tail(nullptr), count(0) {
}
void ReverseLinkList::iterator() const {
BOOST_ASSERT_MSG(count != 0, "List is empty");
Node *temp;
temp = head;
while (temp != tail->Next) {
std::cout << temp->string << std::endl;
temp = temp->Next;
}
}
void ReverseLinkList::reverseIterator() const {
BOOST_ASSERT_MSG(count != 0, "List is empty");
Node *temp;
temp = tail;
while (temp != head->Previous) {
std::cout << temp->string << std::endl;
temp = temp->Previous;
}
}
void ReverseLinkList::insert(std::string string) {
auto *newNode = new Node;
newNode->string = std::move(string);
count++;
if (head == nullptr && tail == nullptr) {
newNode->Previous = nullptr;
newNode->Next = nullptr;
head = newNode;
tail = newNode;
} else {
tail->Next = newNode;
newNode->Next = nullptr;
newNode->Previous = tail;
tail = newNode;
}
}
int ReverseLinkList::size() const {
return count;
}
void ReverseLinkList::remove(const std::string &string) {
BOOST_ASSERT_MSG(count != 0, "List is empty");
if (count == 1) {
if (head->string == string) {
delete head;
head = nullptr;
tail = nullptr;
}
count--;
return;
}
if (head->string == string) {
Node *temp;
temp = head->Next;
delete head;
head = temp;
head->Previous = nullptr;
count--;
return;
}
if (tail->string == string) {
Node *temp;
temp = tail->Previous;
delete tail;
tail = temp;
temp->Next = nullptr;
count--;
return;
}
Node *temp;
temp = head;
bool verbose = true;
while (temp != tail->Next) {
if (temp->string == string) {
verbose = false;
break;
}
temp = temp->Next;
}
if (verbose) {
std::cerr << "Node not present" << std::endl;
return;
}
Node *previous, *next;
previous = temp->Previous;
next = temp->Next;
delete temp;
previous->Next = next;
next->Previous = previous;
count--;
}
void ReverseLinkList::insertAt(std::string string, int position) {
if (position > count)
return;
if (position == 0) {
if (head == nullptr) {
insert(string);
return;
}
auto *newNode = new Node;
newNode->string = string;
newNode->Previous = nullptr;
head->Previous = newNode;
newNode->Next = head;
head = newNode;
count++;
return;
}
if (position == count - 1) {
insert(string);
return;
}
Node *temp;
temp = head;
while (position != 0) {
temp = temp->Next;
position--;
}
Node *next;
next = temp->Next;
auto *newNode = new Node;
newNode->string = string;
temp->Next = newNode;
newNode->Previous = temp;
newNode->Next = next;
next->Previous = newNode;
count++;
}
ReverseLinkList::Node *ReverseLinkList::getHead() const {
return head;
}
ReverseLinkList::Node *ReverseLinkList::getTail() const {
return tail;
}
void ReverseLinkList::removeAt(int position) {
BOOST_ASSERT_MSG(count != 0, "List is empty");
if (position > count)
return;
Node *temp;
if (count == 1) {
delete head;
head = nullptr;
tail = nullptr;
count--;
return;
}
if (position == 0) {
temp = head->Next;
delete head;
head = temp;
head->Previous = nullptr;
count--;
return;
}
if (position == count) {
temp = tail->Previous;
delete tail;
tail = temp;
tail->Next = nullptr;
count--;
return;
}
temp = head;
while (position != 0) {
temp = temp->Next;
position--;
}
Node *previous;
Node *next;
previous = temp->Previous;
next = temp->Next;
delete temp;
previous->Next = next;
next->Previous = previous;
count--;
}
I would like to have some suggestion how can I write better code and Implement it. If you have some functions you would like to add please do post.
My next move is to make this code using templates

Binary Search Tree Traversals

I have just started learning Binary Trees and went ahead and tried to implement my own in C. I am kinda lost as to why only InOrder Traversal is displaying correctly while the other two are wrong. I really can't figure this out. I even directly tried inserting nodes, and the result is the same.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node
{
int val;
struct Node *left;
struct Node *right;
};
//Allocate Memory for New Node
struct Node* getNewNode(int val)
{
struct Node * ptr = (struct Node*)malloc(sizeof(struct Node));
ptr->val = val;
ptr->left = NULL;
ptr->right = NULL;
return ptr;
}
//Insert Node in Binary Search Tree
struct Node* insertNode(struct Node* root,int val)
{
if(root == NULL)
{
root = getNewNode(val);
}
else if(val <= root->val)
{
root->left = insertNode(root->left,val);
}
else
{
root->right = insertNode(root->right,val);
}
return root;
}
void printInorder(struct Node* root)
{
if(root == NULL) return;
printInorder(root->left);
printf("%d ",root->val);
printInorder(root->right);
}
void printPostOrder(struct Node* root)
{
if(root == NULL) return;
printInorder(root->left);
printInorder(root->right);
printf("%d ",root->val);
}
void printPreOrder(struct Node*root)
{
if(root == NULL) return;
printf("%d ",root->val);
printInorder(root->left);
printInorder(root->right);
}
bool search(struct Node* root,int val)
{
if(root == NULL)
{
return false;
}
else if(val == root->val)
{
return true;
}
else if(val < root->val)
{
return search(root->left,val);
}
else
{
return search(root->right,val);
}
}
int main(void)
{
struct Node * root = NULL; //Tree is Empty
root = insertNode(root,15);
root = insertNode(root,10);
root = insertNode(root,8);
root = insertNode(root,12);
root = insertNode(root,20);
root = insertNode(root,17);
root = insertNode(root,25);
printf("Printing In-Order: \n");
printInorder(root);
printf("\nPrinting Post-Order: \n");
printPostOrder(root);
printf("\nPrinting Pre-Order: \n");
printPreOrder(root);
// if(search(root,11))
// {
// printf("\nValue Found\n");
// }
// else
// {
// printf("\nValue Not Found\n");
// }
return 0;
}
Please help me understand if I am doing this wrong or my understanding of traversals is faulty.
The output is as follows:
output terminal
You have copy-paste errors in printPostOrder and printPreOrder - they both call printInorder where they should be calling themselves.

double linke list

i am deleting a (3 or 4 th) node using double linked list...every time only first element is deleting ?what is the problem with the following code?
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *pre;
struct node *link;
};
//void addafter(struct node*,int,int);
main()
{
struct node *p=NULL;
add(&p,2);
add(&p,3);
add(&p,4);
add(&p,5);
add(&p,6);
add(&p,27);
add(&p,8);
add(&p,9);
add(&p,10);
//addafter(&p,7,20);
delete(&p ,6);
display(&p);
}
add(struct node **q,int n)
{
struct node *temp,*r;
temp=*q;
if(temp==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->pre=NULL;
temp->data=n;
temp->link=NULL;
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *pre;
struct node *link;
};
//void addafter(struct node*,int,int);
main()
{
struct node *p=NULL;
add(&p,2);
add(&p,3);
add(&p,4);
add(&p,5);
add(&p,6);
add(&p,27);
add(&p,8);
add(&p,9);
add(&p,10);
//addafter(&p,7,20);
delete(&p ,6);
display(&p);
}
add(struct node **q,int n)
{
struct node *temp,*r;
temp=*q;
if(temp==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->pre=NULL;
temp->data=n;
temp->link=NULL;
*q=temp;
}
else
{
while(temp->link!=NULL)
temp=temp->link;
r=(struct node*)malloc(sizeof(struct node));
r->pre=temp;
r->data=n;
r->link=NULL;
temp->link=r;
}
}
/*
addafter(struct node **q,int m,int n)
{
struct node *temp,*r,*s;
int i;
temp=*q;
for(i=1;i<m;i++)
temp=temp->link;
s=temp->link;
r=(struct node*)malloc(sizeof(struct node));
r->data=n;
r->link=temp->link;
r->pre=temp;
temp->link=r;
s->pre=r;
}
*/
/*display(struct node **q)
{
struct node *temp;
temp=*q;
while(temp!=NULL)
{
printf("\n %d\n",temp->data);
temp=temp->link;
}
}
*/
delete(struct node **q,int n)
{
struct node *temp,*old,*s;
temp=*q;
while(temp!=NULL)
{
if(temp->data=n)
{
if(temp == *q)
{
*q=temp->link;
free(temp);
return;
}
else
{
// s=temp->link;
old->link=temp->link;
// s->pre=old;
free(temp);
return;
}
}
else
{
old=temp;
temp=temp->link;
}
}
printf("data not found");
}
display(struct node **q)
{
struct node *temp;
temp=*q;
while(temp!=NULL)
{
printf("\n %d\n",temp->data);
temp=temp->link;
}
}
At first glance I see this mistake:
old->link=temp->link;
temp->link->pre=temp->pre; // or old.
free(temp);
You should set the pointers for next and previous nodes too. You can make this change in your add method which is also wrong.

Resources