Title explains it all, something weird is going on with my delete function that i cant seem to figure out. I think its getting stuck on the recursion part of the program based off of standard print statements. If anyone can help that would be greatly appreciated
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct btree
{
char name[10];
//int data;
struct btree *rlink;
struct btree *llink;
}node;
void insert(node**, char[]);
void inorder(node*);
void preorder(node*);
void postorder(node*);
int charToNum(node*);
node* findmin(node*);
node* findmax(node*);
node* del(node*, char[]);
int main(void)
{
node *root;
root = (node*)malloc(sizeof(node));
root = NULL;
int ch, x;
x = 0;
char num[10];
while (1)
{
printf("\n 1.insert2.inorder3.preorder4.postorder5.findmin6.findmax7.Delete");
printf("\n Enter your choice>");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter node to insert>");
scanf("%s", &num);
insert(&root, num);
break;
case 2:
printf("\n inorder display");
inorder(root);
break;
case 3:
printf("\n pre-order display");
preorder(root);
break;
case 4:
printf("\n post-order display");
postorder(root);
break;
case 5:
printf("\n Min in the tree is %s", findmin(root)->name);
break;
case 6:
printf("\n Max in the tree is %s", findmax(root)->name);
break;
case 7:
printf("\n enter the node to delete>");
scanf("%s", &num);
root = del(root, num);
break;
case 8:
exit(0);
}
}
}
void insert(node** h, char info[10])
{
if (*h == NULL)
{
node *temp;
temp = (node*)malloc(sizeof(node));
strcpy((temp->name), info);
//temp->name = info;
temp->llink = NULL;
temp->rlink = NULL;
*h = temp;
printf("successfully added %s\n", temp->name);
}
else if (info > (*h)->name) {
insert(&(*h)->rlink, info);
}
else if (info <(*h)->name)
insert(&(*h)->llink, info);
}
void inorder(node* h)
{
if (h != NULL)
{
inorder(h->llink);
printf("%s->", h->name);
inorder(h->rlink);
}
}
void preorder(node* h)
{
if (h != NULL)
{
printf("%s->", h->name);
preorder(h->llink);
preorder(h->rlink);
}
}
void postorder(node* h)
{
if (h != NULL)
{
postorder(h->llink);
postorder(h->rlink);
printf("%s->", h->name);
}
}
node* findmin(node* h)
{
if (h->llink == NULL)
return h;
else
findmin(h->llink);
}
node* findmax(node* h)
{
if (h->rlink == NULL)
return h;
else
findmax(h->rlink);
}
node* del(node *h, char info[])
{
if (h == NULL)
return h;
else if (info > h->name) {
printf("info > h->name\n");
h->rlink = del(h->rlink, info);
}
else if (info < h->name) {
h->llink = del(h->llink, info);
printf("info < h->name\n");
}
else
{
if (h->llink == NULL && h->rlink == NULL)
{
printf("first if excuting");
/*node *tmp;
tmp = (node*)malloc(sizeof(node));
tmp = h;*/
h = NULL;
return h;
}
else if (h->llink == NULL)
{
node *tmp;
tmp = (node*)malloc(sizeof(node));
tmp = h;
h = h->rlink;
free(tmp);
printf("else if finished\n");
}
else if (h->rlink == NULL)
{
printf("second else if is ecuting \n");
node *tmp;
tmp = (node*)malloc(sizeof(node));
tmp = h;
h = h->llink;
free(tmp);
printf("second else if finished\n");
}
else
{
printf("final else is excuting\n");
node *tmp;
tmp = (node*)malloc(sizeof(node));
tmp = findmin(h->rlink);
strcpy((h->name),tmp->name);
//h->name = tmp->name;
h->rlink = del(h->rlink, tmp->name);
free(tmp);
printf("final else finished\n");
}
}
return h;
}
Related
I am working on building circular doubly linked list code.
In my code, there are four function- add node, delete node, print clockwise, print counterclockwise. All my code works fine, besides the delete function. The if(recycle->name == x) line seems not working properly, and free(recycle) also doesn't successfully free the recycle node.
My original code are. as follows
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define nameLen 20
struct Node
{
char name[nameLen];
struct Node *left; //next
struct Node *right; //previous
};
struct Node* current;
struct Node* head;
int count = 0;
struct Node* GetNewNode(char *x)
{
struct Node* newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
strncpy(newNode->name, x, nameLen);
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void add_name(char *x)
{
struct Node* temp = current;
struct Node* newNode = GetNewNode(x);
count++;
if(current == NULL)
{
current = newNode;
head = current;
}
else
{
current->left = newNode;
newNode->right = temp;
current = newNode;
current->left = head;
head->right = current;
}
printf("Add %s into database.\n\n", current->name);
}
void delete_name(char *x)
{
int i, j;
struct Node* recycle = current;
if(current == NULL)
{
printf("No data input.");
}
else
{
for (i = 0; i < count; i++)
{
if(recycle->name == x)
{
free(recycle);
j++;
printf("Delete %s from database.\n", x);
}
recycle = recycle->left;
}
if(j == 0)
{
printf("There is no %s in data", x);
}
current = recycle;
}
}
void print_clock(int number)
{
int i;
struct Node* temp = current;
if(temp == NULL)
{
printf("No data input.");
}
else
{
printf("Clockwise: \n");
for(i = 0; i < number; i++)
{
printf("%s ",temp->name);
temp = temp->left;
}
}
printf("\n\n");
}
void print_counter(int number)
{
int i;
struct Node* temp = current;
if(temp == NULL)
{
printf("No data input.");
}
else
{
printf("Counterclockwise: \n");
for(i = 0; i < number; i++)
{
printf("%s ",temp->name);
temp = temp->right;
}
}
printf("\n\n");
}
int main()
{
char s1;
char s2[nameLen];
char name[nameLen];
int number;
while(1)
{
printf("Enter the instruction: ");
scanf("%s %s", &s1, s2);
if (s1 == '+' && sscanf(s2, "%d", &number) == 1)
{
printf("Print out %d name(s) clockwise.\n", number);
print_clock(number);
}
else if (s1 == '-' && sscanf(s2, "%d", &number) == 1)
{
printf("Print out %d name(s) counterclockwise.\n", number);
print_counter(number);
}
else if (s1 == '+' && sscanf(s2, "%s", name) == 1)
{
add_name(s2);
}
else if (s1 == '-' && sscanf(s2, "%s", name) == 1)
{
delete_name(s2);
}
else if (s1 == 'e')
{
printf("Bye.\n");
break;
}
else // No match.
printf("Wrong Input. %s %s\n", &s1, s2);
}
system("pause");
return 0;
}
Statement recycle->name == x checks if two pointers point to the same object in memory. It does not check if two (different) objects in memory have equal content.
Use
if (strcmp(recycle->name, x) == 0) { ...
to check for equal string contents.
recently i just learning about c and c++, and now i get assignment about creating simple music playlist in C using queue linked list, what i want ask why my song list not appear on the screen ?
is there something wrong with my code, pleas enlight me
i am sorry newbie on stackoverflow too, still not getting use to stackoverflow
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
int songSlot;
char* song[50]= {
"IDGAF - Dua Lipa",
"FRIENDS - Marshmello, Anne-Marie",
"The Middle - Zedd, Maren Morris, Grey",
"Best Part - H.E.R., Daniel Caesar",
"All The Stars (with SZA) - Kendrick Lamar, LZA",
"Wolves - Selena Gomez, Marshmello",
"God's Plan - Drake",
"Rewrite The Stars - Zac Efron, Zendaya",
"Havana - Camila Cabello, Young Thug",
"Perfect - Ed Sheeran"
};
void ShowSong(int _val){
switch (_val) {
case 1: printf(song[0]);
break;
}
}
void Insert() {
int val;
printf("What song number you want to add : \n");
scanf("%d",val);
ShowSong(val);
printf("Added to playlist\n");
system("pause");
//ShowSong(val);
if (rear == NULL) {
rear = (struct node*) malloc(sizeof(struct node));
rear->next = NULL;
rear->data = val;
front = rear;
}
else {
temp = (struct node*) malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp;
}
}
void NextSong() {
temp = front;
if (front == NULL) {
printf("Underflow");
system("pause");
return;
}
else
if (temp->next != NULL) {
temp = temp->next;
printf("\n");
printf("Skipping ",front->data );
int skipSongVal = front->data;
ShowSong(skipSongVal);
printf("\n");
free(front);
front = temp;
printf("\n");
printf("Now Playing ",front->data );
int nextSongVal = front->data;
ShowSong(nextSongVal);
printf("\n");
printf("\n");
system("pause");
}
else {
printf("Skipping ",front->data );
int nextSongVal2 = front->data;
ShowSong(nextSongVal2);
printf("\n");
free(front);
front = NULL;
rear = NULL;
system("pause");
}
}
void ClearPlaylist()
{
temp = front;
if (front == NULL) {
printf("Playlist is Already Empty\n");
system("pause");
return;
}
else
{
printf("Clearing Playlist");
free(front);
front = NULL;
rear = NULL;
system("pause");
}
}
void Display() {
printf("PLAYLIST ");
temp = front;
if ((front == NULL) && (rear == NULL)) {
printf("Playlist is empty\n");
return;
}
printf("Next Song is : \n");
while (temp != NULL) {
int valDisplay = temp->data;
printf("%d",valDisplay);
ShowSong(valDisplay);
printf("\n");
temp = temp->next;
}
printf("\n");
}
int main() {
int ch;
do {
system("CLS");
printf("MUSICS\n");
printf("1)\n",song[0]);
printf("2)\n",song[1]);
printf("3)\n",song[2]);
printf("4)\n",song[3]);
printf("5)\n",song[4]);
printf("6)\n",song[5]);
printf("7)\n",song[6]);
printf("8)\n",song[7]);
printf("9)\n",song[8]);
printf("10)\n",song[9]);
printf("\n");
Display();
printf("\n");
printf("1) Add Song to playlist\n");
printf("2) Skip to next song\n");
printf("3) Clear playlist\n");
printf("4) Exit\n");
printf("Enter your choice : \n");
scanf("%d",&ch);
switch (ch) {
case 1: Insert();
break;
case 2: NextSong();
break;
case 3: ClearPlaylist();
break;
case 4: printf("exit\n");
break;
default: printf("Invalid Choice");
}
} while (ch != 4);
return 0;
}
my code is supposed to find all the prime numbers between 1879 and 9987. everything seems to be working as intended except for when i go to display the linked list. it will display the first 6 elements roughly (it is random how far it will display each time the program is run). as you can see by running the code that it is printing all of the prime numbers required, it just doesn't store them all. any help is appreciated. I am using Dev C for my compiler.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *link;
}n;
n *head = NULL;
void insert(int);
void display();
void erase();
void enqueue(int);
void pop();
void po$p();
void search(int);
void isprime();
int main(void)
{
int num,count, choice;
isprime();
while(1)
{
printf("\n 1. To display>");
printf("\n 2. To Delete the list>");
printf("\n 3. To insert at the end>");
printf("\n 4. Pop out of the stack>");
printf("\n 5. To delete at the back of the list>");
printf("\n 6. To search a particular node>");
printf("\n 7. To exit>");
printf("\n Enter your choice>");
scanf("%d",&choice);
switch(choice)
{
case 1:
display();
break;
case 2:
erase();
break;
case 3:
printf("Enter the data you want to insert at the end>");
scanf("%d",&num);
enqueue(num);
break;
case 4:
pop();
break;
case 5:
po$p();
break;
case 6:
printf("Enter the node you want to search>");
scanf("%d",&num);
search(num);
break;
case 7:
exit(0);
}
}
return 0;
}
void insert(int X)
{
n *temp;
temp = (n*)malloc(sizeof(n*));
temp->data = X;
temp->link = NULL;
if(head == NULL)
head = temp;
else
{
temp->link = head;
head = temp;
}
}
void display()
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
printf("\n There is no list");
else
{
temp = head;
printf("\n head->");
while(temp != NULL)
{
printf("%d->",temp->data);
temp = temp->link;
}
printf("NULL");
}
}
void erase()
{
head = NULL;
}
void enqueue(int X)
{
n *temp,*newnode;
newnode = (n*)malloc(sizeof(n*));
newnode->data = X;
newnode->link = NULL;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
head = newnode;
else
{
temp = head;
while(temp->link != NULL)
temp = temp->link;
temp->link = newnode;
}
}
void pop()
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
{
printf("\n nothing to pop");
}
else
{
temp = head;
printf("\n element popped is %d",temp->data);
head = head->link;
free(temp);
}
}
void po$p()
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
{
printf("\n nothing to po$p");
}
else
{
temp = head;
while(temp->link->link != NULL)
temp = temp->link;
printf("\n Element po$ped is %d",temp->link->data);
temp->link = NULL;
}
}
void search(int X)
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
{
printf("\n nothing to search");
}
else
{
temp = head;
while(temp->data != X && temp->link != NULL)
temp = temp->link;
if(temp->data == X)
printf("\n item in the list");
else if(temp->link == NULL)
printf("\n item is not in the list");
}
}
void isprime(){
int count,i,x;
for(x = 1879;x<=9987;x++){
count = 0;
for(i=2;i<=x/2;i++){
if(x%i==0){
count++;
break;
}
}
if(count==0 && x!= 1){
insert(x);
printf("%d",x);
}
}
}
So I'm having issue with my code with the structure I'm using. I would like my structure to be able add,retrieve or sort but I'm getting a lot of problem with the structure. It work if I use only number but I need to user 3 string. One for firstname, lastname and phonenumber but I can't figure.
This is the code I'm having right now:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
char first[15];
char last[15];
char phone[12];
struct node *next;
}*head;
void append(int num, char f[15], char l[15],char p[12])
{
struct node *temp, *right;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
strcpy(temp->first, f);
strcpy(temp->last, l);
strcpy(temp->phone, p);
right = (struct node *)head;
while (right->next != NULL)
right = right->next;
right->next = temp;
right = temp;
right->next = NULL;
}
void add(int num, char f[15], char l[15],char p[12])
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
strcpy(temp->first, f);
strcpy(temp->last, l);
strcpy(temp->phone, p);
if (head == NULL)
{
head = temp;
head->next = NULL;
}
else
{
temp->next = head;
head = temp;
}
}
void addafter(int num, char f[15], char l[15],char p[12],int loc)
{
int i;
struct node *temp, *left, *right;
right = head;
for (i = 1; i<loc; i++)
{
left = right;
right = right->next;
}
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
strcpy(temp->first, f);
strcpy(temp->last, l);
strcpy(temp->phone, p);
left->next = temp;
left = temp;
left->next = right;
return;
}
void insert(int num, char f[15], char l[15],char p[12])
{
int c = 0;
struct node *temp;
temp = head;
if (temp == NULL)
{
add(num,f,l,p);
}
else
{
while (temp != NULL)
{
if (temp->data<num)
c++;
temp = temp->next;
}
if (c == 0)
add(num,f,l,p);
else if (c<count())
addafter(num,f,l,p, ++c);
else
append(num,f,l,p);
}
}
int delete(int num)
{
struct node *temp, *prev;
temp = head;
while (temp != NULL)
{
if (temp->data == num)
{
if (temp == head)
{
head = temp->next;
free(temp);
return 1;
}
else
{
prev->next = temp->next;
free(temp);
return 1;
}
}
else
{
prev = temp;
temp = temp->next;
}
}
return 0;
}
void display(struct node *r)
{
r = head;
if (r == NULL)
{
return;
}
while (r != NULL)
{
printf("%d ", r->data);
r = r->next;
}
printf("\n");
}
int count()
{
struct node *n;
int c = 0;
n = head;
while (n != NULL)
{
n = n->next;
c++;
}
return c;
}
int main()
{
int i, num;
char fname[15], lname[15], phone[12];
struct node *n;
head = NULL;
while (1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Retrieve\n");
printf("4.Delete\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if (scanf("%d", &i) <= 0){
printf("Enter only an Integer\n");
exit(0);
}
else {
switch (i)
{
case 1:
printf("Enter the id, first, last and phone (Separte with space) : ");
scanf("%d %s %s %s", &num,fname,lname,phone);
insert(num,fname,lname,phone);
break;
case 2:
if (head == NULL){
printf("List is Empty\n");
}else{
printf("Element(s) in the list are : ");
}
display(n);
break;
case 3:
//To be made
//scanf("Retrieve this : %d\n", count());
break;
case 4:
if (head == NULL){
printf("List is Empty\n");
}else{
printf("Enter the number to delete : ");
scanf("%d", &num);
if (delete(num))
printf("%d deleted successfully\n", num);
else
printf("%d not found in the list\n", num);
}
break;
case 5:
return 0;
default:
printf("Invalid option\n");
}
}
}
return 0;
}
Thanks for anyone that could explain me the issue and or fix it.
Everywhere you have:
temp->data = num;
add the lines
strcpy(temp->first, f);
strcpy(temp->last, l);
strcpy(temp->phone, p);
I have made a working binary tree.
Now I want when I press option # 4, it'll show binary tree is sorted according to the array (that means the middle should be root, the left part of the left subtree, then the right part of the field to the right subtree).
I have created functions required to do what it should be done but still it does not work as it should.
I have created NodSize(), it controls how many nodes are in the tree.
I have created balanceTree(), it takes all that is in the root.
I have created ArrayInorder(),
And Balance(), it makes balancing the tree according to array.
Where did I do wrong and how can I solve it?
..
You can see down here how I made it:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MAX 100
struct tree
{
int data;
struct tree *left;
struct tree *right;
};
struct tree *CreateNode(int data)
{
struct tree *node = (struct tree*) malloc(sizeof(struct tree));
if (node != NULL)
{
node->data = data;
node->left = NULL;
node->right = NULL;
}
return node;
}
struct tree *insert(struct tree *root, int data)
{
if (root == NULL)
{
root = CreateNode(data);
}
if (root->data > data)
{
if (root->left == NULL)
{
root->left = CreateNode(data);
}
else
{
insert(root->left, data);
}
}
else if (root->data < data)
{
if (root->right == NULL)
{
root->right = CreateNode(data);
}
else
{
insert(root->right, data);
}
}
return root;
}
struct tree *delet(struct tree *ptr, int x)
{
struct tree *p1, *p2;
if (!ptr)
{
printf("\n NOTHING ");
return(ptr);
}
else
{
if (ptr->data < x)
{
ptr->right = delet(ptr->right, x);
}
else if (ptr->data > x)
{
ptr->left = delet(ptr->left, x);
return ptr;
}
else
{
if (ptr->data == x)
{
if (ptr->left == ptr->right)
{
free(ptr);
return(NULL);
}
else if (ptr->left == NULL)
{
p1 = ptr->right;
free(ptr);
return p1;
}
else if (ptr->right == NULL)
{
p1 = ptr->left;
free(ptr);
return p1;
}
else
{
p1 = ptr->right;
p2 = ptr->right;
while (p1->left != NULL)
p1 = p1->left;
p1->left = ptr->left;
free(ptr);
return p2;
}
}
}
}
return(ptr);
}
void Findroot(struct tree *root)
{
if (root != NULL)
{
printf("\nRoot is %d\n", root->data);
}
else
{
printf("\nNOTHING\n");
}
}
void inorder(struct tree *root)
{
if (root != NULL)
{
inorder(root->left);
printf(" %d", root->data);
inorder(root->right);
}
return;
}
int NodSize(struct tree *root)
{
if (root == NULL)
return 0;
else
return (NodSize(root->left) + 1 + NodSize(root->right));
}
void DestoryTree(struct tree * root)
{
if (root == NULL)
return;
DestoryTree(root->left);
DestoryTree(root->right);
printf("\n Destory node: %d", root->data);
free(root);
}
struct tree *Balance(int arr[], int start, int end)
{
if (start > end)
return NULL;
int mid = (start + end) / 2;
struct tree *root = CreateNode(arr[mid]);
root->left = Balance(arr, start, mid - 1);
root->right = Balance(arr, mid + 1, end);
return root;
}
int ArrayInorder(struct tree *root, int *arr, int helper)
{
if (root != NULL)
{
helper = ArrayInorder(root->left, arr, helper);
arr[helper] = root->data;
helper++;
helper = ArrayInorder(root->right, arr, helper);
}
return helper;
}
void balanceTree(struct tree *root)
{
int *arr, size;
size = NodSize(root);
arr = (int*)malloc(sizeof(int)*size);
ArrayInorder(root, arr, 0);
//DestoryTree(root);
root = Balance(arr, 0, size - 1);
}
void CreateBalancedTreeFromArray(struct tree *root, int *arr, int size)
{
DestoryTree(root);
root = Balance(arr, 0, size - 1);
}
int main(void)
{
struct tree *root;
int valja, item, dele;
root = NULL;
do
{
do
{
printf("\n1. Add a node to BINARY TREE ");
printf("\n2. Delete a node from BINARY TREE ");
printf("\n3. Show ROOT");
printf("\n4. Show Balanced Tree IN (Array)");
printf("\n5. Stang");
printf("\nYour choice? : ");
scanf(" %d", &valja);
if (valja<1 || valja>5)
printf("\n Fel - Try again");
} while (valja<1 || valja>5);
switch (valja)
{
case 1:
system("cls");
printf("\n Write a new element: ");
scanf("%d", &item);
root = insert(root, item);
printf("\n root is %d \n", root->data);
printf("INORDER: ");
inorder(root);
printf("\n");
break;
case 2:
system("cls");
printf("\n Write an element to delete: ");
scanf(" %d", &dele);
root = delet(root, dele);
printf("\n");
break;
case 3:
system("cls");
Findroot(root);
printf("\n");
break;
case 4:
balanceTree(root);
Findroot(root);
inorder(root);
break;
default:
printf("\n bye ");
}
} while (valja != 5);
return(0);
}
The problem is here:
void balanceTree(struct tree *root)
Although this function works, the new root that is created is NOT returned to the caller because root has been passed by value. You need to change the function to:
struct tree *balanceTree(struct tree *root)
and add this at the end:
return root;
Call the function with:
root = balanceTree(root);