I'm currently trying to understand fifo linked list and found example here Example , and I'm trying to input char instead of int
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct Node
{
char Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
void push(char *value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}
int main()
{
int i=0;
char ch;
front=NULL;
printf(" \n1. Push to Queue");
printf(" \n2. Pop from Queue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
//scanf("%d",&i);
ch = getchar();
switch(ch)
{
case '+':
{
char value[20];
printf("\nEnter a valueber to push into Queue : ");
scanf("%s", value);
push(value);
printf("%s",value);
display();
break;
}
case '-':
{
delQueue();
display();
break;
}
case '*':
{
display();
break;
}
case '$':
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
I can't understand this warning on line 26: warning: assignment makes integer from pointer without a cast
I can input text, for example: "Hello world", but when I want to display it, it is showed as "-9".
I am really confused.
Data is defined as char, but your assigning a char * (char pointer) to it. That creates the warning, and surely won't work as you expect.
In your Node struct, the value is of type char, but you're assigning a char* to it instead. This is why you get the warning, and why printing things doesn't come out right.
Related
so i have created a program which uses double linked list and
performs some operations on it . The problem is that it displays garbage
values
at the end every time i try to create a linked list and then display it.
whats wrong in my code?(sorry! for the bad indentations)
if the linked list i created has elements say 15 and 16, it displays it as
15 16 25710 0 0
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct dll
{
int data;
struct dll *llink;
struct dll *rlink;
};
typedef struct dll *node;
node head=NULL;
void create();
int search(int u);
void insert(int num1);
void Delete(int num2);
void display();
node getnode();
int main()
{
int i,num,o;
while(1)
{
printf("\n1.create a list\n 2. insert before a search node\n 3. delete a node\n 4.display\n 5.exit\n");
scanf("%d",&num);
switch(num)
{
case 1 :
create();
break;
case 2 :
printf("enter the value before which you want to enter the node\n");
scanf("%d",&i);
insert(i);
break;
case 3 :
printf("enter the value to be deleted\n");
scanf("%d",&o);
Delete(o);
break;
case 4 :
printf("the linked list has :\n");
display();
break;
case 5 :
getch();
exit(1);
default :
printf("enter the correct option\n");
break;
}
}
}
node getnode()
{
node temp1;
temp1=(node)malloc(sizeof(node));
temp1->llink=NULL;
temp1->rlink=NULL;
return temp1;
}
void create()
{
node nn;
int num,y;
if(head==NULL)
head=getnode();
while(1)
{
printf("enter the data for the node");
scanf("%d",&num);
head->data=num;
printf("do you want to create another node(1/0):\n");
scanf("%d",&y);
if(y==1)
{
nn=getnode();
nn->rlink=head;
head->llink=nn;
head=nn;
nn=NULL;
}
else
break;
}
}
void insert (int num1)
{
int i,n,k;
node temp=head,nn;
n=search(num1);
if(n==0)
{
printf("element not present in the linked list");
}
if(n==1)
{
nn=getnode();
printf("enter the data for the node");
scanf("%d",&k);
nn->data=k;
nn->rlink=head;
head->llink=nn;
head=nn;
nn=NULL;
}
else
{
for(i=2; i<=n; i++)
temp=temp->rlink;
nn=getnode();
temp->llink->rlink=nn;
nn->llink=temp->llink;
nn->rlink=temp;
temp->llink=nn;
}
}
void Delete(int num2)
{
node temp=head;
int p,i;
p=search(num2);
if(p==0)
{
printf("no element is found");
}
if(p==1)
{
printf("the deleted element is %d",head->data);
head=head->rlink;
head->llink=NULL;
free(temp);
}
else
{
for(i=2; i<=p; i++)
{
temp=temp->rlink;
}
temp->llink->rlink=temp->rlink;
temp->rlink->llink=temp->llink;
free(temp);
temp=temp->rlink;
}
}
int search(int u)
{
node temp=head;
int pos=0;
if(u==head->data)
return 1;
else
{
while(temp!=NULL)
{
pos++;
if(temp->data==u)
{
printf("element found\n");
return(pos);
}
temp=temp->rlink;
}
}
if(temp==NULL)
{
return 0;
}
return -1;
}
void display()
{
node temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->rlink;
}
}
This:
temp1=(node)malloc(sizeof(node));
is a major error. Since you're "hiding a star", and node is a typedef for a pointer type, you're not allocating enough memory. It should be:
node temp1 = malloc(sizeof *temp1);
But I really recommend against typedefing a pointer away, it just makes things confusing.
I have this program that does not throw error, but the viewl procedure only shows the first item in the list. How do I for the viewl procedure that printing but in the reverse order to what does the procedure viewr?
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
struct list
{
int info;
struct list *nxt,*prv;
}*HEAD=NULL,*AUX=NULL,*P=NULL,*F=NULL,*QD=NULL,*FD=NULL;
main function (ok)
int main()
{
void insertr(void);
void extractr(void);
void viewr(void);
void viewl(void);
void deleten();
void deletep();
char opc;
do
{
system("cls");
printf("___________________________________________________\n");
printf("_______¡¡¡DOUBLE-LINKED CIRCULAR LIST MENU!!!______\n");
printf("___________________________________________________\n");
printf("_________________SELECT AN OPTION__________________\n");
printf("___________________________________________________\n");
printf("___________________________________________________\n");
printf("__________1) INSERT________________________________\n");
printf("__________2) VIEW ASCENDING________________________\n");
printf("__________3) VIEW DESCENDING_______________________\n");
printf("__________4) ENTERING AND ELIMINATE NEXT___________\n");
printf("__________5) ENTERING AND ELIMINATE PREVIOUS_______\n");
printf("__________6) EXIT__________________________________\n");
printf("___________________________________________________\n");
opc=getch();
switch(opc)
{
case '1':
insertr();
break;
case '2':
viewr();
break;
case '3':
viewl();
break;
case '4':
deleten();
break;
case '5':
deletep();
break;
}
}
while(opc!='6');
getch();
return 0;
}
insert right process
void insertr(void)
{
P=HEAD;/* very first execution of this method P=NULL */
AUX=(struct list *)malloc(sizeof(struct list));
system("cls");
printf("ENTER AN ENTIRE NUMBER: ");
scanf("%d",&AUX->info);
AUX->nxt=HEAD;
AUX->prv=HEAD;
F=AUX;
if(HEAD==NULL)
{
HEAD=AUX;
P = AUX;/*first execution of this method P is no longer NULL but P is pointing to AUX */
}
else
{
while (P->nxt!=HEAD)
{
P=P->nxt;
}
}
P->nxt=AUX;
AUX->prv=P;
HEAD->prv=AUX;
}
void deleten()
{
int x;
system("cls");
printf("ENTER A NUMBER TO ELIMINATE THE FOLLOWING: ");
scanf("%d",&x);
FD=HEAD;
QD=HEAD;
while(FD->info!=x&&FD->nxt!=HEAD)
{
FD=FD->nxt;
}
QD=FD->nxt;
if(FD->nxt==HEAD&&FD->info!=x)
{
printf("\nENTERED NUMBER IS NOT LISTED");
}
else
{
if(FD->info==x)
{
FD->nxt=QD->nxt;
(QD->nxt)->prv=FD;
printf("\nDELETED %d",QD->info);
free(QD);
}
}
getch();
}
deletea process (ok)
void deletep()
{
int x;
system("cls");
printf("ENTER A NUMBER TO REMOVE THE PREVIOUS ");
scanf("%d",&x);
FD=HEAD;
QD=HEAD;
while (FD->info!=x&&FD->nxt!=HEAD)
{
FD=FD->nxt;
}
QD=FD->prv;
if(FD->nxt==HEAD&&FD->info!=x)
{
printf("\nENTERED NUMBER IS NOT LISTED");
}
else
{
if(FD->info==x)
{
FD->prv=QD->prv;
(QD->prv)->nxt=FD;
printf("\nDELETED %d",QD->info);
free(QD);
}
}
getch();
}
viewr process (ok):
void viewr(void)
{
system("cls");
if(HEAD==NULL)
{
printf("EMPTY LIST");
getchar();
return;
}
AUX=HEAD;
printf("LIST:\n\n");
while(AUX->nxt!=HEAD)
{
printf("-> %d\n",AUX->info);
AUX=AUX->nxt;
}
if(AUX->nxt==HEAD)
{
printf("-> %d\n",AUX->info);
}
getch();
}
this function fails:
void viewl(void)
{
system("cls");
if(HEAD==NULL)
{
printf("EMPTY LIST");
getchar();
return;
}
AUX=F;
printf("LIST:\n\n");
do
{
printf("-> %d\n",AUX->info);
AUX=AUX->prv;
}
while(AUX->nxt!=HEAD);
getch();
}
The code had several logical errors, other variables, etc. Here it is working without problems
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
//THIS IS TO CREATE THE LIST
struct list{
int info;
struct list *nxt,*prv;
}*HEAD=NULL,*AUX=NULL,*P=NULL,*FD=NULL,*QD=NULL,*Fd=NULL,*Qd=NULL;
int main(){
void insertr(void);
void viewr(void);
void viewl(void);
void deleten();
void deletep();
char opt;
do{
system("cls");
printf("___________________________________________________\n");
printf("_______¡¡¡DOUBLE-LINKED CIRCULAR LIST MENU!!!______\n");
printf("___________________________________________________\n");
printf("_________________SELECT AN OPTION__________________\n");
printf("___________________________________________________\n");
printf("___________________________________________________\n");
printf("__________1) INSERT________________________________\n");
printf("__________2) VIEW ASCENDING________________________\n");
printf("__________3) VIEW DESCENDING_______________________\n");
printf("__________4) ENTERING AND ELIMINATE NEXT___________\n");
printf("__________5) ENTERING AND ELIMINATE PREVIOUS_______\n");
printf("__________6) EXIT__________________________________\n");
printf("___________________________________________________\n");
opt=getch();
switch(opt) {
case '1':
insertr();
break;
case '2':
viewr();
break;
case '3':
viewl();
break;
case '4':
deleten();
break;
case'5':
deletep();
break;
}
}while(opt!='6');
return 0;
}
void insertr(void){
P=HEAD; /* very first execution of this method P=NULL */
AUX=(struct list *)malloc(sizeof(struct list));
system("cls");
printf("ENTER AN ENTIRE NUMBER: ");
scanf("%d",&AUX->info);
AUX->nxt=HEAD;
AUX->prv=HEAD; // The pointer to the previous node is null (HEAD is null)
if(HEAD==NULL){ //I ask if HEAD is equal to Null
HEAD=AUX;
P=AUX; /* The first execution of this method P is no longer NULL but P is pointing to AUX */
}else{ //THIS YES
while(P->nxt!=HEAD){
P=P->nxt;
}
}
P->nxt=AUX; // The next pointer of P takes the value of AUX
AUX->prv=P;
HEAD->prv=AUX;
}
void deleten(){
int x;
system("cls");
printf("ENTER A NUMBER TO ELIMINATE THE FOLLOWING: ");
scanf("%d",&x);
FD=HEAD;
QD=HEAD;
while(FD->info!=x&&FD->nxt!=HEAD){ // if what I am going to eliminate is different from the number that was entered and if there is another
FD=FD->nxt; //step to the next node
}
QD=FD->nxt;
if(FD->nxt==HEAD&&FD->info==x){
printf("\nIT IS THE HEADBOARD! CAN NOT BE ELIMINATED");
}else
if(FD->nxt==HEAD&&FD->info!=x){
printf("\nTHE NUMBER ENTERED IS NOT IN THE LIST");
}else{
FD->nxt=QD->nxt;
(QD->nxt)->prv=FD;
printf("\nDELETED %d",QD->info);
free(QD); //free space in memory occupied by QD
}
getch();
}
void deletep()
{
int xd;
system("cls");
printf("IENTER A NUMBER TO REMOVE THE PREVIOUS: ");
scanf("%d",&xd);
Fd=HEAD;
Qd=HEAD;
while(Fd->info!=xd){
Fd=Fd->prv;
} Qd=Fd->prv;
if(Fd==HEAD){
printf("\nIT'S THE HEAD! CAN NOT BE ELIMINATED");
}else{
if(Qd==HEAD){
HEAD=HEAD->nxt;
Fd->prv=Qd->prv;
(Fd->prv)->nxt=Fd;
printf("\nDELETED %d",Qd->info);
}else{
Fd->prv=Qd->prv;
(Qd->prv)->nxt=Fd;
printf("\nDELETED %d",Qd->info);
}
free(Qd);
}
getch();
}
void viewr(void)
{
system("cls");
if(HEAD==NULL){
printf("LISTA VACIA");
getchar();
return;
}
AUX=HEAD;
printf("LISTA:\n\n");
while(AUX->nxt!=HEAD){
printf("->%d\n",AUX->info);
AUX=AUX->nxt;
}
if(AUX->nxt==HEAD){
printf("->%d\n",AUX->info);
}
getch();
}
void viewl(void){
system("cls");
if(HEAD==NULL){
printf("EMPTY LIST");
getchar();
return;
} AUX=HEAD->prv;
printf("LIST:\n\n");
do{
printf("->%d\n",AUX->info);
AUX=AUX->prv;
}while(AUX->nxt!=HEAD);
getch();
}
in your code:
do
{
printf("-> %d\n",AUX->info);
AUX=AUX->prv;
}
while(AUX->nxt!=HEAD);
It can be happening something like this.
As you are going to the previous node and checking if next is head, next will be head if you built your list to be double-linked and circular.
try like this:
do
{
AUX=AUX->prv;
printf("-> %d\n",AUX->info);
}
while(AUX!=HEAD);
and you should avoid using all variables as global.
So I'm creating a program that will display a student's ID as well as their name in a queue. I've finally got it to work displaying the users name, but I also need the ID displayed as well. I'm afraid to start messing around to much and mess everything up. Someone show me the way to the light ;).
EDIT: Thanks to Paul R for pointing this out. I didn't realize until now that I'm only going to be able to display one letter at a time for my students name, what did I do wrong here?
#include <stdio.h>
#include <stdlib.h>
struct queueNode
{
int data1;
char data;
struct queueNode *nextPtr;
};
typedef struct queueNode QueueNode;
typedef QueueNode *QueueNodePtr;
void printQueue (QueueNodePtr currentPtr);
int isEmpty (QueueNodePtr headPtr);
char dequeue (QueueNodePtr *headPtr, QueueNodePtr *tailPtr);
void enqueue (QueueNodePtr *headPtr, QueueNodePtr *tailPtr, char value);
void instructions (void);
int main (void)
{
QueueNodePtr headPtr=NULL;
QueueNodePtr tailPtr=NULL;
int choice;
char name;
int ID;
instructions();
printf("?");
scanf("%d",&choice);
while(choice !=3)
{
switch(choice)
{
case 1:
printf("Enter ID: ");
scanf("\n%d", &ID);
printf("Enter Name: ");
scanf("\n%c", &name);
//RETURN HERE TO ENTER LAST NAME
enqueue(&headPtr, &tailPtr, ID);
enqueue(&headPtr, &tailPtr, name);
printQueue(headPtr);
break;
case 2:
if (!isEmpty(headPtr))
{
ID=dequeue(&headPtr, &tailPtr);
name=dequeue(&headPtr, &tailPtr);
printf("%d %c has been dequeued.\n", ID, name);
}
printQueue(headPtr);
break;
default:
printf("Invalid choice.\n\n");
instructions();
break;
}
printf("?");
scanf("%d", &choice);
}
printf("End of Run\n");
return 0;
}
void instructions(void)
{
printf("Enter your choice: \n"
"1 to add to queue\n"
"2 to remove from queue\n"
"3 to exit\n");
}
void enqueue (QueueNodePtr *headPtr, QueueNodePtr *tailPtr, char value)
{
QueueNodePtr newPtr;
newPtr=malloc(sizeof(QueueNode));
if(newPtr!=NULL)
{
newPtr->data = value;
newPtr->nextPtr = NULL;
if(isEmpty (*headPtr))
{
*headPtr=newPtr;
}
else
{
(*tailPtr)->nextPtr=newPtr;
}
*tailPtr=newPtr;
}
else
{
printf("%c not inserted. No memory available.\n", value);
}
}
char dequeue (QueueNodePtr *headPtr, QueueNodePtr *tailPtr)
{
char value;
QueueNodePtr tempPtr;
value = (*headPtr)->data;
tempPtr = *headPtr;
*headPtr = (*headPtr)->nextPtr;
if(*headPtr==NULL)
{
*tailPtr = NULL;
}
free(tempPtr);
return value;
}
int isEmpty(QueueNodePtr headPtr)
{
return headPtr==NULL;
}
void printQueue (QueueNodePtr currentPtr)
{
if(currentPtr==NULL)
{
printf("Queue is empty. \n\n");
}
else
{
printf("The Queue is: \n");
while(currentPtr !=NULL)
{
printf("%c --> ", currentPtr ->data);
currentPtr = currentPtr->nextPtr;
}
printf("NULL\n\n");
}
}
Instead of char data; (a single character) you want e.g. char data[80]; (a char array, aka string). Ditto for char name;. You will need to use a string function such as strcpy to copy strings between your struct data field and your temporary variable name.
I have the below code which I am using to implement BinarySearchTree. Somehow it doesn't build the binarytree.Can anybody help what is the issue ?
typedef struct BinarySearchTree
{
struct BinarySearchTree *left;
int nodeval;
struct BinarySearchTree *right;
}
BST;
BST *root=NULL;
void addrootnode(BST *,int );
void addnode(BST *,int );
void deletenode(int );
void printnodes();
main()
{
int nodeval;
char choice;
printf("\n r->rootnode \n a->add \n d->delete \n p->print \n e->exit\n");
while (1)
{
printf("\nEnter your choice:");
scanf("%c",&choice);
switch (choice)
{
case 'r':
printf("\nEnter the root node value to add: ");
scanf("%d",&nodeval);
addrootnode(root,nodeval);
break;
case 'a':
printf("\nEnter the node value to add: ");
scanf("%d",&nodeval);
addnode(root,nodeval);
break;
case 'd':
printf("\nEnter the node value to delete: ");
scanf("%d",&nodeval);
break;
case 'p':
//printf("\n");
printnodes(root);
break;
case 'e':
exit(0);
default:
break;
}
}
getchar();
}//end of main
//addrootnode
void addrootnode(BST *ptr,int num)
{
if(ptr==NULL)
{
ptr=(BST *) malloc(sizeof(BST));
ptr->left=NULL;
ptr->nodeval=num;
ptr->right=NULL;
root=ptr;
}
}
//add node
void addnode(BST *ptr,int num)
{
if(ptr==NULL)
{
ptr=(BST *) malloc(sizeof(BST));
ptr->left=NULL;
ptr->nodeval=num;
ptr->right=NULL;
}
else if(num < ptr->nodeval )
{
addnode(ptr->left,num);
}
else
{
addnode(ptr->right,num);
}
}
//delete functionality
void deletenode(int nodeval)
{
}
//print all nodes
void printnodes(BST *ptr)
{
if (ptr)
{
printnodes(ptr->left);
printf("%d\t",ptr->nodeval);
printnodes(ptr->right);
}
}
You should not use BST *ptr as argument to addnode. You try to assign a value to it but at this time ptr is a local variable to the function addnode an does not alter the intended left or right pointer of the parent node.
Try to think about using BST **ptr instead.
I am trying to write a program in C language(code::blocks in windows).
I have added below header files, it compiles with no error, but when it comes to run the code it throws an error undefined reference to gotoxy.
Find the full code.
Error comes where ever I have gotoxy statements.
# include<stdio.h>
# include<conio.h>
# include<malloc.h>
# include<stdlib.h>
# include<windows.h>
#include<dos.h>
struct node
{ int data;
struct node *link;
};
void append(struct node **,int);
void in_begin(struct node **,int);
void del(struct node **,int);
void in_middle(struct node **,int,int);
int count(struct node *);
void display(struct node *);
char ans;
int main()
{ struct node *p; /* p can be said as the head or a start ptr */
p=NULL;
/* Printing the menu */
int num,loc;
char choice;
do
{ //clrscr();
printf("PROGRAM TO IMPLEMENT SINGLY LINKED LIST ");
printf("\n=====================================");
printf("\n\n1.Create \\ Appending The List");
printf("\n2.Insert Node At Begining");
printf("\n3.Insert Node In Middle");
printf("\n4.Deleting a Node");
printf("\n5.Counting The No Of Nodes");
printf("\n6.Displaying the list");
printf("\n7.Exit");
oper:
gotoxy(1,15);printf(" ");
gotoxy(1,11);printf("\n\nEnter ur Choice : ");
choice=getch();
switch(choice)
{
case '1':
// char ans;
do
{ printf("Enter any number : ");
scanf("%d",&num);
append(&p,num);
printf("Enter more (y/n) :");
fflush(stdin);
ans=getchar();
}while(ans !='n');
break;
case '2':
printf("Enter The Data : ");
scanf("%d",&num);
in_begin(&p,num);
break;
case '3':
printf("\nEnter The Position :");
scanf("%d",&loc);
printf("\nEnter The Data : ");
scanf("%d",&num);
in_middle(&p,loc,num);
break;
case '4':
printf("\nEnter The Data u Want To Delete : ");
scanf("%d",&num);
del(&p,num);
break;
case '5':
printf("\nThe No Of Nodes Are %d",count(p));
getch();
break;
case '6':
display(p);
getch();
break;
case '7':
printf("\n\nQuiting.......");
getch();
exit(0);
break;
default:
gotoxy(1,15);printf("Invalid choice.Please Enter Correct Choice");
getch();
goto oper;
}
}while(choice !=7);
return 0;
}
void append(struct node **q,int num)
{ struct node *temp,*r;
temp = *q;
if(*q==NULL)
{ temp = (struct node *)malloc(sizeof(struct node));
temp->data=num;
temp->link=NULL;
*q=temp;
}
else
{ temp = *q;
while(temp->link !=NULL)
{ temp=temp->link;
}
r = (struct node *)malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;
}
}
void display(struct node *q)
{ if(q==NULL)
{ printf("\n\nEmpty Link List.Can't Display The Data");
getch();
goto last;
}
while(q!=NULL)
{ printf("\n%d",q->data);
q=q->link;
}
last:
;
}
int count(struct node *q)
{ int c=0;
if(q==NULL)
{ printf("Empty Link List.\n");
getch();
goto last;
}
while(q!=NULL)
{ c++;
q=q->link;
}
last:
return c;
}
void in_begin(struct node **q,int num)
{ struct node *temp;
if(*q==NULL)
{ printf("Link List Is Empty.Can't Insert.");
getch();
goto last;
}
else
{ temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
temp->link=*q;
*q=temp; /* pointing to the first node */
}
last:
getch();
}
void in_middle(struct node **q,int loc,int num)
{ struct node *temp,*n;
int c=1,flag=0;
temp=*q;
if(*q==NULL)
{ printf("\n\nLink List Is Empty.Can't Insert.");
getch();
goto last;
}
else
while(temp!=NULL)
{ if(c==loc)
{ n = (struct node *)malloc(sizeof(struct node));
n->data=num;
n->link=temp->link;
temp->link=n;
flag=1;
}
c++;
temp=temp->link;
}
if(flag==0)
{ printf("\n\nNode Specified Doesn't Exist.Cant Enter The Data");
getch();
}
else
{ printf("Data Inserted");
getch();
}
last:
getch();
}
void del(struct node**q,int num)
{ if(*q==NULL)
{ printf("\n\nEmpty Linked List.Cant Delete The Data.");
getch();
goto last;
}
else
{
struct node *old,*temp;
int flag=0;
temp=*q;
while(temp!=NULL)
{ if(temp->data==num)
{ if(temp==*q) /* First Node case */
*q=temp->link; /* shifted the header node */
else
old->link=temp->link;
free(temp);
flag=1;
}
else
{ old=temp;
temp=temp->link;
}
}
if(flag==0)
printf("\nData Not Found...");
else
printf("\nData Deleted...Tap a key to continue");
getch();
}
last:
getch();
}
Please help me.
The problem you're facing isn't related to Code::Blocks, it's related to the compiler it's using (MinGW by default), and it's because that function isn't standard and wasn't implemented in that compiler. I'm not sure if Borland still provides conio.h, but you could try this one for MinGW.
Have a look at this http://projectsofashok.blogspot.in/2010/05/gotoxy-in-codeblocks.html
You can also try the below snippet, this will work in GCC
#include<stdio.h>
//gotoxy function
void gotoxy(int x,int y)
{
printf("%c[%d;%df",0x1B,y,x);
}
main ()
{
gotoxy(25,50); //reposition cursor
printf("hello world"); //display text
}
You can also have a look at NCURSES.
Code::Blocks(MinGW) doesnt have conio.h header file. So you cant use gotoxy()
#include<stdio.h>
#include<conio.h>
main()
{
gotoxy(10, 10);
printf("C program to change cursor position.");
getch();
return 0;
}
this works fine in C. but not in code blocks.
The problem you're facing isn't related to Code::Blocks, it's related to the compiler it's using (MinGW by default), and it's because that function isn't standard and wasn't implemented in that compiler.
try using these:
SetConsoleCursorPosition(hConsole, TL);
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
or use Borland C which supports conio.h. For getting it Visit the LINK
# include<stdio.h>
# include<conio.h> // you cant use conio.h
# include<malloc.h>
# include<stdlib.h>
# include<windows.h>
For what it is worth, here are some Windows versions of the old Borland functions I wrote maybe a decade ago.
/* console_functions.h */
#ifndef __WINAPI_CONSOLE_WRAPPER_H
#define __WINAPI_CONSOLE_WRAPPER_H
void init_console_functions (void);
void gotoxy (int x, int y);
void clrscr (void);
char getch (void);
#endif /* __WINAPI_CONSOLE_WRAPPER_H */
/* console_functions.c */
#include "ConsoleFunctions.h"
#include <windows.h>
static HANDLE hStdout;
static HANDLE hStdin;
static CONSOLE_SCREEN_BUFFER_INFO csbi;
static const COORD startCoords = {0,0};
void init_console_functions (void)
{
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
hStdin = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdout, &csbi);
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(hStdout,coord);
}
void clrscr(void)
{
DWORD dummy;
FillConsoleOutputCharacter(hStdout,
' ',
csbi.dwSize.X * csbi.dwSize.Y,
startCoords,
&dummy);
gotoxy(0,0);
}
char getch(void)
{
INPUT_RECORD inp_rec;
DWORD bytes_read;
BOOL success;
do
{
success = PeekConsoleInput(hStdin, &inp_rec, 1, &bytes_read);
FlushConsoleInputBuffer(hStdin);
} while(!success || inp_rec.EventType != KEY_EVENT || bytes_read==0);
return (char)inp_rec.Event.KeyEvent.uChar.AsciiChar;
}
There are two things for such kinds of questions in C
1) you have to have the proper header with the proper prototp (this i conio.h)
2) you have to have the proper library to link against in this case probably something along libconio.lib
You then have to include the proper header and you have to inform the linker against which libraries you like to link.
The link commands in gcc e.g are -L and -l and both are needed to use conio.
See also: g++ conio.h: no such file or directory
Regards
it is error because there is something missing that can handle the gotoxy try this code
int gotoxy(int x,int y)
{
COORD coord = {x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
Code blocks GUI doesn't work gotoxy()
so you make function it here....
#include<stdio.h>
#include<windows.h>
void gotoxy(int a, int b)
{
COORD c;
c.X=a;
c.Y=b;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int main()
{
gotoxy(5, 66);
printf("hello");
getch();
}