huffman tree in c prints memory address instead of values? - c

Here is a program to create huffman tree,
written by my junior.
what's error? perhaps it is printing memory addresses.
#include <stdio.h>
#include <stdlib.h>
struct link
{
int data;
struct link *next, *left, *right;
};
typedef struct link node;
node *curr = NULL, *head = NULL, *nnode = NULL;
void createheap();
void main()
{
node* i;
int k1, k2;
int ch;
int x;
printf("create node\nadd nodes\ntraverse node\nmake heap\n");
do
{
printf("enter the choice\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
nnode = (node*)malloc(sizeof(node));
printf("enter number please\n");
scanf("%d", &x);
nnode->data = x;
nnode->next = NULL;
nnode->left = NULL;
nnode->right = NULL;
head = nnode;
curr = nnode;
break;
case 2:
nnode = (node*)malloc(sizeof(node));
printf("enter number please\n");
scanf("%d", &x);
nnode->data = x;
nnode->next = NULL;
nnode->left = NULL;
nnode->right = NULL;
curr->next = nnode;
curr = nnode;
break;
case 3:
printf("traverse\n");
for (i = head; i != NULL; i = i->next)
{
printf("%d\n", i->data);
}
break;
case 4:
createheap();
break;
default:
printf("this is wrong number\n");
}
} while (ch <= 5);
}
void createheap()
{
node* l;
node *t1, *t2, *t;
node *np, *r1;
int k1, k2, k3;
node* i;
// void traverse(node*);
l = head;
while (head->next != NULL)
{
l = head;
t = l;
nnode = (node*)malloc(sizeof(node));
t1 = t->next;
t2 = t->next->next;
k1 = t->data;
k2 = t1->data;
k3 = k1 + k2;
if (t2 == NULL)
{
nnode->left = t;
nnode->right = t1;
nnode->data = k3;
nnode->next = NULL;
curr = nnode;
head = nnode;
}
else if (k3 <= t2->data)
{
nnode->next = t2;
nnode->left = t;
nnode->right = t1;
nnode->data = k3;
head = nnode;
}
else if (k3 > t2->data)
{
nnode->left = t;
nnode->right = t1;
nnode->data = k3;
if (k3 <= curr->data)
{
for (i = t2; i != NULL; i = i->next)
{
if (i->data >= k3)
{
break;
}
else
{
np = i;
}
}
r1 = np->next;
nnode->next = r1;
np->next = nnode;
}
else if (k3 > curr->data)
{
nnode->next = NULL;
curr->next = nnode;
curr = nnode;
}
head = t2;
}
free(t);
free(t1);
printf("l %d\n", nnode->left);
printf("r %d\n", nnode->right);
printf("d %d\n", nnode->data);
}
// traverse(l);
// printf("\n%d",l->left->data);
// printf("\n%d",l->right->left->data);*/
}
void traverse(node* t)
{
if (t != NULL)
{
traverse(t->left);
printf("\n%d\n", t->data);
traverse(t->right);
}
}
"create node" means initialise the list and enter first value.
"add nodes" means add second, third and so on.. values. (please provided sorted values only for now)
"make heap" means create heap out of provided list, AND print left(l), right(r) and root values(d), from bottom of tree (as it gets made).
So, problem is root is printing correct value. but left and right are printing like.. 31298384, 31298323.

Related

I was trying to make a simple doubly linked list with operations Insert, Delete and Display and

The insert seems to go smoothly but the display only shows the head element only. I wanted to do this on my own and tried to use the logic. I am confused whether the fault lies in the Insert function or the Display.
I am not really that great at programming and just started learning C++.Thank you for your help.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node *head = NULL;
void insert(struct node **head)
{
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;
if ((*head) == NULL)
{
int x;
printf("\nEnter the value of the starting node :");
scanf("%d", &x);
newnode->data = x;
(*head) = newnode;
}
else
{
int pos, x;
printf("\nEnter the pos ");
scanf("%d", &pos);
if (pos == 0)
{
newnode->next = (*head);
newnode->prev = NULL;
(*head)->prev = newnode;
(*head) = newnode;
printf("\nEnter data in %d pos : ", pos);
scanf("%d", &x);
newnode->data = x;
}
else
{
struct node *temp;
struct node *ptr = (*head);
while(ptr->next!=NULL)
{
for (int i = 0; i < pos - 1; i++)
{ ptr = ptr->next;}
}
if (ptr->next == NULL)
{
newnode->prev = ptr;
newnode->next = NULL;
printf("\nEnter data in %d pos : ", pos);
scanf("%d", &x);
newnode->data = x;
}
else
{
printf("\nEnter data in %d pos : ", pos);
scanf("%d", &x);
newnode->data = x;
temp = ptr->next;
newnode->prev = ptr;
newnode->next = temp;
ptr->next = newnode;
temp->prev = newnode;
}
}
}
}
void delete (struct node **head)
{
struct node *ptr;
ptr = (*head);
if ((*head) == NULL)
{
printf("\nUnderflow\n");
}
else
{
int pos;
printf("\nEnter the pos ");
scanf("%d", &pos);
struct node *temp;
for (int i = 0; i < pos; i++)
{
ptr = ptr->next;
}
temp = ptr->next;
temp->prev = ptr->prev;
ptr->next = NULL;
ptr->prev = NULL;
}
}
void display(struct node **head)
{
struct node *ptr = (*head);
if (ptr != NULL)
{
printf(" %d ",ptr->data);
}
else
{
printf("\nUnderflow OR empty\n");
}
}
int main()
{
while (1)
{
int x;
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\n\nChoose option :\n");
scanf("%d", &x);
switch (x)
{
case 1:
{
insert(&head);
break;
}
case 2:
{
delete (&head);
break;
}
case 3:
{
display(&head);
break;
}
default:
{
printf("\nWrong operation.Select again :");
continue;
}
}
}
return 0;
}
The display was supposed to show the list like
a-> b -> c......
In the display function you should take the next node and print that too
// head->next1->next2->NULL
struct node *ptr = (*head);
while (ptr != NULL) {
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL");

Selection sort of singly Linked Lists by swapping nodes(iterative approach)?

I am trying to perform selection sort on singly linked list by swapping the node itself but after all the inputs it looks like my sort() function in not working properly.
What am I missing or did wrong please someone help me with that.
NOTE: Name of all the functions and pointers pretty much tells their task so I thought not to add comments But if anyone wants comment then please let me know.
NOTE: In swap_node(int i, int j),i and j are the positions of the two nodes which needs to be swapped.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
} node;
node *head = NULL, *prev = NULL, *next = NULL;
static int k = 0;//Global variable K which will store the no of nodes created so far.
node *make_node()
{
k++;
return ((node *)malloc(sizeof(node)));
}
void push()
{
if (k == 0)
{
next = make_node();
printf("Enter Data:");
scanf("%d", &next->data);
next->next = NULL;
head = next;
prev = next;
}
else
{
next = make_node();
printf("Enter Data:");
scanf("%d", &next->data);
next->next = NULL;
prev->next = next;
prev = next;
}
}
node *x = NULL, *y = NULL;
void swap_node(int i, int j)
{
node *prevX = NULL, *currX = head, *prevY = NULL, *currY = head;
if (i == 1)
{
for (int l = 1; l < j; l++)
{
prevY = currY;
currY = currY->next;
}
head = currY;
if (j - i == 1)
{
currX->next = currY->next;
currY->next = currX;
}
else
{
node *temp = currX->next;
currX->next = currY->next;
currY->next = temp;
prevY->next = currX;
}
}
else
{
for (int l = 1; l < i; l++)
{
prevX = currX;
currX = currX->next;
}
for (int l = 1; l < j; l++)
{
prevY = currY;
currY = currY->next;
}
if (j - i == 1)
{
prevX->next = currY;
currX->next = currY->next;
currY->next = currX;
}
else
{
node *temp = currX->next;
prevX->next = currY;
prevY->next = currX;
currX->next = currY->next;
currY->next = temp;
}
}
x = currX;
y = currY;
}
void sort()
{
x = head;
int i = 0, j = 0;
for (i = 1; i < k; i++)
{
y = x->next;
for (j = i + 1; j <= k; j++)
{
if (x->data > y->data)
{
swap_node(i, j);
}
y = y->next;
}
x = x->next;
}
}
void print_node()
{
printf("------------Printing Node--------------\n");
node *temp = head;
do
{
printf("%d\n", temp->data);
temp = temp->next;
} while (temp != NULL);
}
void main(void)
{
int choice;
printf("MENU\n1-PUSH\n2-Print node\n");
do
{
printf("Enter Your Choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
sort();
print_node();
break;
default:
printf("Wrong Choice!");
}
} while (choice == 1);
}
why not just swap the content :
void swap_content (node *first, node *second) {
int med = first->data;
first->data = second->data;
second->data = med;
}

why is the below code not scanning all inputs properly?

This code is a solution to the given problem :
https://www.hackerearth.com/international-women-hackathon-2016/algorithm/jp-and-rotations/
Issue is after I give m operations as R 1 ,L 2 ,L 1 one below other as specified in the sample input , it doesn't take any further input and directly prints output as 2 , I am unable to get the mistake I am making here ,please help me out .
#include <stdio.h>
#include<malloc.h>
struct node
{
unsigned long int data ;
struct node *next,*prev;
};
int main()
{
int n ,m,a,count=0,i,j;
char rot;
scanf("%d %d ", &n ,&m);
struct node *head1=NULL,*rear1,*ptr,*temp,*head2=NULL,*rear2;
if(head1==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);
temp->next=NULL;
head1=temp;
ptr=head1;
head1->prev=NULL;
}
for(i=1;i<=n-1;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);
ptr->next=temp;
temp->prev=ptr;
ptr=temp;
temp->next=NULL;
}
rear1=ptr;
temp=NULL;
ptr=NULL;
fflush(stdout);
if(head2==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);
temp->next=NULL;
head2=temp;
ptr=head2;
}
for(i=1;i<=n-1;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);
ptr->next=temp;
ptr=temp;
ptr->next=NULL;
}
rear2=ptr;
ptr=NULL;temp=NULL;
fflush(stdout);
for(i=0;i<m;i++)
{
scanf("%c %d",&rot,&a);
fflush(stdout);
if(rot=='L')
{
ptr=head1;
for(j=0;j<a;j++)
{
temp=ptr->next;
rear1->next=ptr;
ptr->prev=rear1;
rear1=ptr;
head1=temp;
ptr=head1;
}
count++;
ptr=NULL;
temp=NULL;
if(head1->data==head2->data && rear1->data==rear2->data)
{
break;
}
}
else if(rot=='R')
{
temp=head1;
ptr=rear1->prev;
for(j=0;j<a;j++)
{
temp->prev=rear1;
rear1->prev->next=NULL;
rear1->next=temp;
head1=rear1;
temp=head1;
rear1=ptr;
ptr=rear1->prev;
}
count++;
temp=NULL;
ptr=NULL;
if(head1->data==head2->data && rear1->data==rear2->data)
{
break;
}
}
}
printf("%d",count);
return 0;
}
Try removing space after the format specifier in the scanf statement.
It will not give you correct final output.But,I think thats what you needed to know.
Try this code:
#include <stdio.h>
#include<malloc.h>
struct node
{
unsigned long int data;
struct node *next, *prev;
};
int main()
{
int n, m, a, count = 0, i, j;
char rot;
scanf("%d %d", &n, &m);
struct node *head1 = NULL, *rear1 = NULL, *ptr = NULL, *temp = NULL, *head2 = NULL, *rear2 = NULL;
if (head1 == NULL)
{
temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);
temp->next = NULL;
head1 = temp;
ptr = head1;
head1->prev = NULL;
}
for (i = 1; i <= n - 1; i++)
{
temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);
ptr->next = temp;
temp->prev = ptr;
ptr = temp;
temp->next = NULL;
}
ptr->next = head1;
head1->prev = ptr;
rear1 = ptr;
temp = NULL;
ptr = NULL;
fflush(stdout);
fflush(stdin);
if (head2 == NULL)
{
temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);
temp->next = NULL;
head2 = temp;
ptr = head2;
head2->prev = NULL;
}
for (i = 1; i <= n - 1; i++)
{
temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);
ptr->next = temp;
temp->prev = ptr;
ptr = temp;
ptr->next = NULL;
}
ptr->next = head2;
head2->prev = ptr;
rear2 = ptr;
ptr = NULL; temp = NULL;
fflush(stdout);
fflush(stdin);
for (i = 0; i<m; i++)
{
scanf("%c %d", &rot, &a);
fflush(stdout);
fflush(stdin);
if (rot == 'L')
{
ptr = head1;
for (j = 0; j<a; j++)
{
temp = ptr->next;
rear1->next = ptr;
ptr->prev = rear1;
rear1 = ptr;
head1 = temp;
ptr = head1;
}
count++;
ptr = NULL;
temp = NULL;
if (head1->data == head2->data && rear1->data == rear2->data)
{
break;
}
}
else if (rot == 'R')
{
temp = head1;
ptr = rear1->prev;
for (j = 0; j<a; j++)
{
temp->prev = rear1;
rear1->prev->next = NULL;
rear1->next = temp;
head1 = rear1;
temp = head1;
rear1 = ptr;
ptr = rear1->prev;
}
count++;
temp = NULL;
ptr = NULL;
if (head1->data == head2->data && rear1->data == rear2->data)
{
break;
}
}
}
printf("%d", count);
return 0;
}

Linked List in C

Although not requirement of my course, I am trying to implement single and double linked list to ensure I understand the concepts. I have worked out most of the issues I think except for one that I have tried to debug and printf my way out of. Although I have read different posts I cannot find out why they are not working similarly.
One my insert function when the while(h != NULL) kicks out I have to check the last item one more time because it would skip it. I thought about a do/while loop but that would not work.
I have the exact same loop for the search function and it finds the last item in the list using while(h != NULL) instead of kicking me before processing it.
Here is the loop that works from the search function,
while(h != NULL) //check list
if(h->val == s)
return true;
else
h = h->next;
Here is the loop that does not from the insert function, this one does not end up using the final value in the linked list before exiting the loop. If you read in the full code before I had to check if if (t->next == NULL && t->val > n) to get it to search the last item.
while(t->next != NULL) //assigns it in the middle
{
if(t->val < n)
t = t->next;
else
{
node* tt = t->prev;
tt->next = a;
a->next = t;
a->prev = t->prev;
t->prev = a;
return h; //returns original
}
}
Thanks in advance and hopefully I am explaining it. Difficult as I am attempting to learn what I do not know.
#include <stdio.h>
#include <stdlib.h> //for rand();
#include <stdbool.h> //bool function
typedef struct _node
{
int val;
struct _node* next;
struct _node* prev;
} node;
node* create(int n, node* h);
node* insert(int n, node* h);
bool search(int s, node* h);
void print(node* h);
void del(node* h);
int main()
{
node* h = NULL;
for(int i = 0; i < 15; i++)
{
int n = rand() % 15;
h = create(n, h);
}
print(h);
int s = 10; //number to search for
if(search(s, h))
printf("Found Item\n");
else
printf("Item not found\n");
del(h);
}
bool search(int s, node* h)
{
if(h == NULL)
printf("No nodes created to search.\n");
while(h != NULL) //check list
if(h->val == s)
return true;
else
h = h->next;
return false;
}
void del(node* h)
{
node* t = h;
while (h->next != NULL)
{
t = h->next;
h->next = NULL;
free(h);
h = t;
}
free(h);
}
node* insert(int n, node* h)
{
node* a = malloc(sizeof(node));
node* t = h;
if(a == NULL)
{
printf("Failed to create new node\n");
return NULL;
}
a->val = n; //sets new node to have value of n
if(h->val >= n) //assigns the integer to the beginning
{
a->next = t;
t->prev = a;
a->prev = NULL;
h = a; //assigns a new head since it appended the item to the beginning
return h;
}
while(t->next != NULL) //assigns it in the middle
{
if(t->val < n)
t = t->next;
else
{
node* tt = t->prev;
tt->next = a;
a->next = t;
a->prev = t->prev;
t->prev = a;
return h; //returns original
}
}
if (t->next == NULL && t->val > n) //check to see if second to last
{
node* tt = t->prev;
tt->next = a;
a->next = t;
a->prev = t->prev;
t->prev = a;
}
else
{
a->next = NULL;
a->prev = t;
t->next = a;
}
return h;
}
node* create(int n, node* h)
{
if(h == NULL) //create first node in the list
{
h = malloc(sizeof(node));
if(h == NULL)
{
printf("Failed to create first node\n");
return NULL;
}
h->val = n;
h->next = NULL;
h->prev = NULL;
}
else //create additional nodes in the list
h = insert(n, h);
return h;
}
void print(node* h)
{
if(h == NULL)
printf("List is empty\n");
else
while(h != NULL)
{
printf("%i\n", h->val);
h = h->next;
}
}
If I understand your question correctly, you are asking about these lines:
while(t->next != NULL) //assigns it in the middle
{
if(t->val < n)
t = t->next;
else
{
node* tt = t->prev;
tt->next = a;
a->next = t;
a->prev = t->prev;
t->prev = a;
return h; //returns original
}
}
if (t->next == NULL && t->val > n) //check to see if second to last
{
node* tt = t->prev;
tt->next = a;
a->next = t;
a->prev = t->prev;
t->prev = a;
}
else
{
a->next = NULL;
a->prev = t;
t->next = a;
}
and the question is: "How can to avoid repeating the code block from inside the while after the while"
Maybe something like this:
while(1)
{
if(t->val < n)
{
if (t->next == NULL) break;
t = t->next;
}
else
{
//assigns it in the middle
node* tt = t->prev;
tt->next = a;
a->next = t;
a->prev = t->prev;
t->prev = a;
return h; //returns original
}
}
// Add new node at the end
a->next = NULL;
a->prev = t;
t->next = a;

C two-way list deleting items

I have a problem. The code below creates a two-way looped list. When I want to delete an item of specyfic key value, everything goes right. The problem comes when I want to add an item after deleting another one. It just doesn't appear when I display the list. Do you have any ideas why?
#include <iostream>
#include <time.h>
#include <vector>
#include <stdlib.h>
struct List
{
int key;
double val1, val2, val3;
struct List *next, *prev;
}*element, *temp, *head;
bool ifrepeat = false,ifdelete;
void AddOne(struct List *&head1, int x)
{
List *tmp = head1;
if (head1 != NULL)
{
do
{
if (head1->key == x)
{
ifrepeat = true;
}
head1 = head1->next;
} while (head1 != tmp);
}
if (ifrepeat)
{
printf("Podana wartosc klucza juz istnieje!\n");
ifrepeat = false;
}
else
{
head1 = tmp;
if (head1 == NULL)
{
head1 = (struct List*)malloc(sizeof(struct List));
head1->key = x;
head1->val1 = 1 + (rand() % 1000);
head1->val2 = 1 + rand() % 1000;
head1->val3 = 1 + rand() % 1000;
head1->next = head1;
head1->prev = head1;
}
else
{
if (element == NULL)
{
element = (struct List*)malloc(sizeof(struct List));
element->key = x;
head->next = element;
element->prev = head1;
element->next = head1;
head1->prev = element;
temp = element;
}
else
{
element = (struct List*)malloc(sizeof(struct List));
element->key = x;
temp->next = element;
element->prev = temp;
element->next = head1;
head1->prev = element;
temp = element;
}
}
}
}
void ShowBegin(struct List *head)
{
if (head == NULL)
printf("Lista jest pusta!");
else
{
struct List *temphead;
temphead = head;
printf("%d ", head->key);
head = head->next;
while (head != temphead)
{
printf("%d ", head->key);
head = head->next;
}
printf("\n");
}
}
void AddMany(struct List *&head1, int x)
{
AddOne(head, 10+(rand()%1000)*(rand()%1000));
int tmpkey;
List *tmp = head1;
for (int i = 0; i < x; i++)
{
tmpkey = 10 + (rand() % 100)*(rand() % 1000);
while (head1 != tmp)
{
while (head->key == tmpkey)
{
tmpkey = 10 + (rand() % 100)*(rand() % 1000);
head1 = tmp;
}
head1 = head1->next;
}
AddOne(head, tmpkey);
head1 = tmp;
head1 = head1->next;
}
}
void Sort(struct List *&head1)
{
int max;
List *maxPos,*tmp;
maxPos = head1;
max = head1->key;
while (true)
{
head1 = head1->next;
if (head1->key > max)
{
tmp = head1;
head1 = maxPos;
maxPos = tmp;
max = maxPos->key;
head1 = head;
}
}
}
void CreateList()
{
head = NULL;
element = NULL;
temp = NULL;
}
void Delete(struct List *&head1, int x)
{
struct List *temphead;
temphead = head1;
if (head == NULL)
{
printf("Lista jest pusta!\n");
return;
}
else
{
head1 = head1->next;
if (head1 == temphead)
{
if (head1->key == x)
{
free(head1);
head1 = NULL;
ifdelete = true;
}
else
ifdelete = false;
}
else
{
while (head1 != temphead)
{
if (head1->key == x)
{
List *temp;
temp = head1;
head1->prev->next = head1->next;
head1->next->prev = head1->prev;
head1 = head1->next;
while (head1 != temphead)
{
}
free(temp);
ifdelete = true;
break;
}
else
ifdelete = false;
head1 = head1->next;
}
}
if (!ifdelete)
{
printf("Element o podanym kluczu nie istnieje!\n");
}
}
}
void ShowEnd(struct List *&head)
{
if (head == NULL)
printf("Lista jest pusta!");
else
{
struct List *temphead;
temphead = head;
head = head->prev;
while (head != temphead)
{
printf("%d ", head->key);
head = head->prev;
}
printf("%d", temphead->key);
printf("\n");
}
}
void DeleteAll(struct List *&head)
{
free(head);
head = NULL;
}
int main()
{
int X=1000, k1=5, k2=2, k3=10, k4=-1;
/*FILE* plik = fopen("inlab02.txt", "r");
if (plik == NULL)
return -257;
fscanf(plik, "%d %d %d %d %d", &X, &k1, &k2, &k3, &k4);
fclose(plik);*/
srand(time(NULL));
clock_t begin, end;
double time_spent;
begin = clock();
CreateList();
Delete(head, k1);
ShowBegin(head);
AddMany(head, 10);
AddOne(head, k2);
Delete(head, k2);
AddOne(head, k4);
//Delete(head, k4);
ShowBegin(head);
//ShowEnd(head);
//DeleteAll(head);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Program wykonal obliczenia w %f", time_spent);
getchar();
return 0;
}

Resources