I tried writing a tree program in C, starting with the insert function as shown below:
void insert(int val)
{
struct node * nn = (struct node *)malloc(sizeof(struct node));
nn->data = val;
nn->left = NULL;
nn->right = NULL;
if(root == NULL)
{
root = nn;
}
else
{
struct node *ptr = root;
while(ptr->left != NULL && ptr->right != NULL)
{
if(nn->data < ptr->data)
{
ptr = ptr->left;
}
else
{
ptr = ptr->right;
}
}
if(ptr->data < nn->data)
{
ptr->right = nn;
}
else
{
ptr->left = nn;
}
}
Then I wrote the code for displaying the nodes of the tree so formed:
void display()
{
struct node *n;
if(root == NULL)
{
printf("Tree is Empty\n");
}
else
{
n = root;
if(n!=NULL)
{
printf("%d",n->data);
display(n->left);
display(n->right);
}
}
}
This is the main function:
int main()
{
int ch,val;
while(ch!=3)
{
printf("1. Insert\n2. Display\n3. Exit\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter value to be inserted: ");
scanf("%d",&val);
insert(val);
break;
case 2:
display();
break;
case 3:
ch=3;
printf("Exiting....\n");
break;
default:
printf("Invalid choice, please try again\n");
break;
}
}
return 0;
}
But when I tried executing the display function after inserting a few nodes, it only printed the first node in an infinite loop.
Could someone please help me get around this problem?
With each iteration (recursion) you start with root again. That will continue forever, of course.
Your function signature should be
void display(struct node *n)
and then call it in main as:
display(root);
Fixing the display function itself with this feed back I leave to you.
EDIT: the function should be:
void display(struct node *n)
{
if (n) {
printf("%d\n",n->data);
display(n->left);
display(n->right);
}
}
Related
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;
}
Im working on a program in C that reads a binary tree from a file and opperates functions on it:insert elements,delete elements,search elements,display elements
With my old debugger it seems that i have 4 warnings and i dont know kow to fix them
Here is the code
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct bst
{
double number;
struct bst *leftChild;
struct bst *rightChild;
} node;
node *root = NULL;
void insertNode(double value);
void preOrderPrint(node *root);
node * findMinimum(node *root);
node * deleteNode(node *root, double value);
node* search(node ** tree, double val);
int main()
{
char line[255];
char* endptr;
FILE* f = fopen("tree.txt", "r");
while (fgets(line, sizeof(line), f))
{
if (*line!='*')
{
if(strtod (line, &endptr))
insertNode(strtod (line, &endptr));
else if (strtoimax(line,&endptr,0))
insertNode((strtoimax(line,&endptr,0)));
else
insertNode( strtol (line, &endptr,0));
}
}
fclose(f);
int flag = 0, key;
int choice = 0;
do
{
printf("\nEnter your choice:\n1. Display tree\n2. Insert node\n3. Delete node\n4. Exit\nChoice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("In Order Display\n");
preOrderPrint(root);
break;
case 2:
printf("Enter key to insert: ");
scanf("%d",&key);
flag = search(&root,key);
if (flag)
{
printf("Key found in tree,cannot insert\n");
}
else
{
printf("Node %d was inserted \n",key);
insertNode(key);
preOrderPrint(root);
}
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", (int*)&key);
flag = search(&root, key);
if (!flag)
{
printf("Key not found in tree,cannot delete\n");
}
else
{
printf("Node %d was deleted \n",key);
deleteNode(root,key);
preOrderPrint(root);
}
break;
case 4:
printf("Memory Cleared\nPROGRAM TERMINATED\n");
break;
default: printf("Not a valid input, try again\n");
}
}
while (choice != 5);
return 0;
}
node * deleteNode(node *currentNode, double value)
{
if(currentNode==NULL) // empty tree
return NULL;
else if(value < currentNode->number) // value is less than node's number. so go to left subtree
currentNode->leftChild = deleteNode(currentNode->leftChild, value);
else if(value > currentNode->number) // value is greater than node's number. so go to right subtree
currentNode->rightChild = deleteNode(currentNode->rightChild, value);
else // node found. Let's delete it!
{
//node has no child
if(currentNode->leftChild == NULL && currentNode->rightChild == NULL)
{
currentNode = NULL;
}
else if(currentNode->leftChild == NULL) // node has only right child
{
currentNode = currentNode->rightChild;
}
else if(currentNode->rightChild == NULL) // node has only left child
{
currentNode = currentNode->leftChild;
}
else // node has two children
{
node *tempNode = findMinimum(currentNode->rightChild);
currentNode->number = tempNode->number;
currentNode->rightChild = deleteNode(currentNode->rightChild, tempNode->number);
}
}
return currentNode;
}
node * findMinimum(node *currentNode)
{
if(currentNode->leftChild==NULL)
return currentNode;
return findMinimum(currentNode->leftChild);
}
void insertNode(double value)
{
node *tempNode;
node *currentNode;
node *parentNode;
tempNode = (node *) malloc(sizeof(node));
tempNode->number = value;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
//For the very first call
if(root==NULL)
{
root = tempNode;
}
else
{
currentNode = root;
parentNode = NULL;
while(1)
{
parentNode = currentNode;
if(value <= parentNode->number)
{
currentNode = currentNode->leftChild;
if(currentNode==NULL)
{
parentNode->leftChild = tempNode;
return;
}
}
else
{
currentNode = currentNode->rightChild;
if(currentNode==NULL)
{
parentNode->rightChild = tempNode;
return;
}
}
}
}
}
void preOrderPrint(node *root)
{
if(root==NULL)
return;
preOrderPrint(root->leftChild);
printf("%g ", root->number);
preOrderPrint(root->rightChild);
}
node* search(node ** tree, double val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->number)
{
search(&((*tree)->leftChild), val);
}
else if(val > (*tree)->number)
{
search(&((*tree)->rightChild), val);
}
else if(val == (*tree)->number)
{
return *tree;
}
//return true;
}
C:\UsersX\Desktop\39\main.c|32|warning: implicit declaration of
function 'strtoimax' [-Wimplicit-function-declaration]|
C:\UsersX\Desktop\39\main.c|56|warning: assignment makes integer from
pointer without a cast [-Wint-conversion]|
C:\UsersX\Desktop\39\main.c|72|warning: assignment makes integer from
pointer without a cast [-Wint-conversion]|
C:\UsersX\Desktop\39\main.c|224|warning: control reaches end of
non-void function [-Wreturn-type]| ||=== Build finished: 0 error(s), 4
warning(s) (0 minute(s), 0 second(s)) ===|
Also the main question would be why i cannot delete the root?
With my debugger it seems that the root can be deleted.
I'm trying to make a simple double linked list, I used (switch) in the first place:
int choice, data;
switch(choice)
{
case 1:
printf("Enter Your Data\n");
scanf("%d",&data);
InsetFirst(data);
data =0;
break;
case 2:
printf("Enter Your Data\n");
scanf("%d",&data);
InsertLast(data);
data =0;
break;
case 3:
printf("The list from the beginning to the End :\n");
PrintForward();
break;
case 4:
printf("The list from the end to the beginning\n");
PrintBackward();
break;
case 5:
printf("Enter the data you want search\n");
scanf("%d",data);
Search(data);
if(Search(data))
{
printf("%d\n",*(Search(data)));
}
else
{}
data =0;
break;
case 6:
printf("Enter The data you want to delete\n");
scanf("%d",&data);
DeleteNode(data);
break;
default :
printf("Not Valid Entry\n");
But it kept showing me this error in one of the functions
"expected declaration or statement at end of input"
knowing that I tested the functions individually and it worked properly,
After that I used (if,if-else) and then it worked`
int main()
{
int choice=1 , data;
while (1)
{
printf("Choose from the following options\n\n");
printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n");
scanf("%d",&choice);
if(choice==1)
{
printf("Enter Your Data\n");
scanf("%d",&data);
InsetFirst(data);
data =0;
}
else if (choice==2)
{
printf("Enter Your Data\n");
scanf("%d",&data);
InsertLast(data);
data =0;
}
else if(choice==3)
{
printf("The list from the beginning to the End :\n");
PrintForward();
}
else if(choice==4)
{
printf("The list from the end to the beginning\n");
PrintBackward();
}
else if(choice==5)
{
printf("Enter the data you want search\n");
scanf("%d",data);
Search(data);
data =0;
}
else if(choice==6)
{
printf("Enter The data you want to delete\n");
scanf("%d",&data);
DeleteNode(data);
}
else
{
printf("Enter a Valid Choice\n");
}
}`,
but there were error with search function in case the item doesn't exist.
hope anyone can help me, thanks in advance, peace :)
here is the full code with commented sections that don't work:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct Node
{
int data;
struct Node* pnext;
struct Node* pprev;
};
struct Node* pstart = NULL;
struct Node* plast = NULL;
/** Functions Prototype **/
struct Node* CreatNode (void);
void InsetFirst (int data);
void InsertLast (int data);
void PrintForward (void);
void PrintBackward (void);
struct Node* Search (int data);
void DeleteNode (int Node );
int main()
{
int choice=1 , data;
while (1)
{
printf("Choose from the following options\n\n");
printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n");
scanf("%d",&choice);
if(choice==1)
{
printf("Enter Your Data\n");
scanf("%d",&data);
InsetFirst(data);
data =0;
}
else if (choice==2)
{
printf("Enter Your Data\n");
scanf("%d",&data);
InsertLast(data);
data =0;
}
else if(choice==3)
{
printf("The list from the beginning to the End :\n");
PrintForward();
}
else if(choice==4)
{
printf("The list from the end to the beginning\n");
PrintBackward();
}
else if(choice==5)
{
printf("Enter the data you want search\n");
scanf("%d",data);
Search(data);
data =0;
}
else if(choice==6)
{
printf("Enter The data you want to delete\n");
scanf("%d",&data);
DeleteNode(data);
}
else
{
printf("Enter a Valid Choice\n");
}
}
/*
int choice,data;
switch(choice)
{
case 1:
printf("Enter Your Data\n");
scanf("%d",&data);
InsetFirst(data);
data =0;
break;
case 2:
printf("Enter Your Data\n");
scanf("%d",&data);
InsertLast(data);
data =0;
break;
case 3:
printf("The list from the beginning to the End :\n");
PrintForward();
break;
case 4:
printf("The list from the end to the beginning\n");
PrintBackward();
break;
case 5:
printf("Enter the data you want search\n");
scanf("%d",data);
Search(data);
if(Search(data))
{
printf("%d\n",*(Search(data)));
}
else
{}
data =0;
break;
case 6:
printf("Enter The data you want to delete\n");
scanf("%d",&data);
DeleteNode(data);
break;
default :
printf("Not Valid Entry\n");
*/
return 0;
}
/** Function to create Node in the list **/
struct Node* CreatNode (void)
{
struct Node* temp;
temp = (struct Node*) malloc(sizeof(struct Node));
if (!temp)
{
printf("\nNot Enough Memory");
}
else
{
return temp;
}
}
/**************************************************************************************/
/** Function to Insert Node at the Beginning of the list **/
void InsetFirst (int data)
{
struct Node* temp;
temp = CreatNode();
temp ->data = data;
temp ->pnext = NULL;
temp ->pprev = NULL;
if (pstart == NULL)
{
pstart = temp;
plast = temp;
}
else
{
temp ->pnext = pstart;
pstart ->pprev =temp;
pstart = temp;
}
}
/***********************************************************************************/
/** Function to Insert Node at the End of the List **/
void InsertLast (int data)
{
struct Node* temp;
temp = CreatNode();
temp ->data = data;
temp ->pnext = NULL;
temp ->pprev = NULL;
if (pstart == NULL)
{
pstart = temp;
plast = temp;
}
else
{
temp ->pprev = plast;
plast ->pnext = temp;
plast = temp;
}
}
/**********************************************************************************************/
/** Function to Print the list From the beginning to the End **/
void PrintForward (void)
{
struct Node* current;
current = pstart;
printf("\nThe list From the Beginning to the End :\n");
while (current)
{
printf("\n%d",current->data);
current = current->pnext;
}
printf("\n");
}
/*********************************************************************************************/
void PrintBackward (void)
{
struct Node* current;
current = plast;
printf("\nThe list From End to the Beginning :\n");
while (current)
{
printf("\n%d",current->data);
current = current->pprev;
}
printf("\n");
}
/*********************************************************************************************/
/** Function To Find a Given Data **/
struct Node* Search (int data)
{
struct Node* current;
current = pstart;
if (current)
{
while(current)
{
if (current->data == data)
{
return current;
}
current = current->pnext;
}
printf("\nIt's not found\n");
return NULL;
}
}
/**************************************************************************************/
/** Function to Delete a Given Node **/
void DeleteNode (int Node )
{
struct Node* state;
state = Search(Node);
if (state)
{
if ((state == pstart) && (state == plast))
{
pstart = NULL;
plast = NULL;
}
else if (pstart == state)
{
pstart = state->pnext;
state->pnext->pprev = NULL;
}
else if (plast == state)
{
plast = state->pprev;
state->pprev->pnext = NULL;
}
else
{
state->pprev->pnext = state->pnext;
state->pnext->pprev = state->pprev;
}
free(state);
}
else
{
printf("NOT Found\n");
}
}
There was a few problems in your code. I was working with the switch version, and there were also problems with the if else version.
case 5:
printf("Enter the data you want search\n");
scanf("%d",data);
Search(data);
if(Search(data))
{
printf("%d\n",*(Search(data)));
}
else
{}
data =0;
break;
When you use scanf you need to send a pointer to the location where you want to store something, so it will be scanf("%d", &data). Also printf's %d needs int value as argument but here:
printf("%d\n",*(Search(data)));
you are sending it a Node, and so it will not accept it. You need to send the data that is within the Node so you do this:
printf("%d\n",(*(Search(data))).data);
And you don't need to have else with an empty block, if you remove it it won't affect the program.
Now we have a problem in the CreatNode function which does not return anything in the case temp is null. So you need to return null in the if block in case there was not enough memory:
struct Node* CreatNode (void)
{
struct Node* temp;
temp = (struct Node*) malloc(sizeof(struct Node));
if (!temp)
{
printf("\nNot Enough Memory");
return NULL; //YOU NEED TO RETURN NULL HERE
}
else
{
return temp;
}
}
Function Search won't return anything if for example first current equals NULL, so you need to move return and printf line out of if block like this:
struct Node* Search (int data)
{
struct Node* current;
current = pstart;
if (current)
{
while(current)
{
if (current->data == data)
{
return current;
}
current = current->pnext;
}
}
printf("\nIt's not found\n");
return NULL;
}
And the last thing and the reason why your switch does not work is because it is not in a loop. Switch by itself is not a loop so you need to put it in a while loop that works until for example, user enters 0. So this will be a solution:
int main()
{
int choice=-1,data;
while(choice != 0)
{
printf("\n\nChoose from the following options\n\n");
printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
/*...*/
case 2:
/*...*/
case 3:
/*...*/
case 4:
/*...*/
case 5:
/*...*/
case 6:
/*...*/
case 0:
break;
default :
printf("Not Valid Entry\n");
}
}
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);
}
}
}
Question: Create a linked list containing values in the ascending values. Then write functions addNew() which will accept a value from the user and then call addBegin() and addafterValue() functions to add the input value in the appropriate place
e.g. consider the list is like this:
12,15,20,26 then if the user enters values 8, 16 & 30 the list will look like this: 8,12,15,16,20,26,30.
My program:
#include<stdio.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *start=NULL;
void append()
{
NODE *temp,*ptr;
temp=(NODE *)malloc(sizeof(NODE));
printf("Enter data:");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
}
}
void display()
{
NODE *ptr=start;
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void addBegin(int val)
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=start;
start=temp;
}
unsigned int addAfterValue(int val,NODE *ptr)
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=ptr->next;
return temp;
}
void addNew()
{
int val;
unsigned int loc;
NODE *ptr=start;
printf("Enter value to add:");
scanf("%d",&val);
if(val<ptr->data) {
addBegin(val);
ptr=NULL;
}
while(ptr!=NULL) {
if(ptr->next!=NULL)
{
if(val<ptr->next->data)
{
addAfterValue(val,ptr);
ptr->next=loc;
ptr=NULL;
}
else
{
ptr=ptr->next;
}
}
if(ptr->next==NULL)
{
loc=addAfterValue(val,ptr);
ptr=NULL;
}
}
}
int main()
{
int ans;
do
{
printf("Enter [1]To append\n[2]To add new node\n[3]To display\n[0]To exit\n");
printf("Enter your choice:");
scanf("%d",&ans);
switch(ans)
{
case 1:
append();
break;
case 2:
addNew();
break;
case 3:
display();
break;
case 0:
break;
default:
printf("Wrong Input.Try again.");
}
}while(ans);
}
My doubt: The addBegin() function works perfectly. I think there's something wrong with addafterValue(). Can anyone help me by finding out my mistake?
Instead of passing the current pointer address.
Use the previous node pointer and assign to its next node.
void addAfterValue(int val,NODE *ptr)
{
NODE *temp = (NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=ptr->next;
ptr->next = temp;
}
Change the addNew function
void addNew()
{
int val;
NODE *ptr=start;
NODE *prev= NULL;
printf("Enter value to add:");
scanf("%d",&val);
if( ptr == NULL || val < ptr->data)
{
addBegin(val);
return;
}
else
{
prev = ptr;
ptr=ptr->next;
}
while( ptr != NULL)
{
if( val <= ptr->data)
{
addAfterValue(val,prev);
return;
}
else
{
prev = ptr;
ptr=ptr->next;
}
}
/* Control comes here if the entire list is scanned.... Now append it to the end using prev pointer, as the new node is greater than all of the existing nodes */
}