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();
}
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'm new to C language and it's been harder for me to work with pointers after working in Java😥
I was trying to write a code of finding a path (not necessary minimum) between two nodes in a graph using breadth-first-search.
Here is my code :
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 200
void push(int a);
int pop(void);
void bfs(int a,int b,int len);
int nextnode(int a);
typedef struct node{
int data;
struct node* next;
}node;
int res[MAXSIZE];
int visited[MAXSIZE];
int rear,front;
node* graph[MAXSIZE];
int len;
int path[MAXSIZE];
int nextnode(int a)
{
if(graph[a]==NULL)
return -1;
else
{
struct node* c=graph[a];
while(visited[c->data]!=1 && c!=NULL)
{
c=c->next;
}
if(c==NULL)
return -1;
else
return c->data;
}
}
void push(int a)
{
path[rear]=a;
rear++;
}
int pop()
{
if(front==rear)
return -1;
int num=path[front];
front++;
return num;
}
int main()
{
rear=0;
len=0;
front=0;
int n,e;
int i,a,b;
printf("%s\n%s", "Inputting Graph... ","Enter number of nodes and edges: ");
scanf("%d %d",&n,&e);
printf("%s %d %s\n", "Graph Created with",n,"nodes without any edge.");
printf("%s\n","Enter the edges in 1 2 format if an edge exist from Node 1 to Node 2" );
for(i=1;i<=n;i++)
{
graph[i]=NULL;
visited[i]=0;
}
struct node* new = (struct node*)malloc(sizeof(struct node));
for(i=0;i<e;i++)
{
scanf("%d %d",&a,&b);
new->data=b;
new->next=NULL;
struct node* curr=graph[a];
if(curr==NULL)
{
graph[a]=new;
}
else
{
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=new;
}
}
printf("%s\n", "Graph Created Successfully.");
printf("%s", "Enter the node numbers between which the path is to be found between: ");
scanf("%d %d",&a,&b);
bfs(a,b,0);
printf("Length is %d\n",len);
for(i=1;i<=len;i++)
{
printf("%d\n",res[len]);
}
}
void bfs(int a,int b,int len)
{
int c;
visited[a]=1;
int flag=0;
while(a!=-1)
{
c=nextnode(a);
while(c!=-1)
{
c=nextnode(a);
if(c==b)
{
flag=1;
break;
}
push(c);
visited[c]=1;
}
len++;
res[len]=a;
if(flag==1)
{
res[len]=b;
break;
}
a=pop();
}
}
I know it's huge, but please mind going through it once. The problem I'm getting is Segmentation Fault after I input all the values, and before dfs() function call! Please Help.
For understanding: I have used array of Lists. Each array index denotes a node and the list denotes all the edges it is connected to. eg: if my Graph has 1->2, 1->3, 2-3 edges;
graph[1] will have a list 2->3->NULL. And graph[2] will have 3->NULL.
Thank you.
EDIT
As pointed out by Aditi, the error was in the line where nextnode function ran the while loop. After changing the code to
while(c != NULL && visited[c->data] == 1 )
the program ran flawlessly.
Thanks!
I think what you are trying to do is not graph[i] = NULL but graph[i]->next = NULL
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.
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.
#include<stdio.h>
#include<stdlib.h>
struct node {
char str[200];
int nn;
struct node* next;
};
int number;
struct node* start=NULL;
struct node* current;
//function to insert into the list
void insert() {
struct node* n;
n=(struct node*)malloc(sizeof(struct node));
n->str=malloc(sizeof(char) * 1000);
printf("please enter the data that you would like to insert: ");
gets(n->str);
printf("asdasdasdasd");
n->next=NULL;
if( start==NULL ) {
start->next=n;
current=n;
}
else {
current->next=n;
current=n;
}
printf("done\n");
}
void display() {
current=start;
int i=0;
while( current->next!=NULL ) {
printf("node%d= %s\n",++i,current->str);
current=current->next;
}
printf("this the end");
}
int main() {
char c;
int input;
do {
printf("Select from the following options:\n"
"1.Display list\n"
"2.Add to list\n"
"3.delete from list\n");
scanf("%d",&input);
switch (input) {
case 1: display(); break;
case 2: insert(); break;
// case 3: delete(); break;
default : printf("Please select 1 , 2 or 3\n");
}
printf( "would you like to continue?(y/n)\n");
scanf("%s",&c);
} while(c=='y');
return 0;
}
This is giving me an error in the insert function, A SEGMENTATION FAULT !
I Tried things but I just don't get the clear picture. I am a bit weak with pointers, actually confused !
Please help me out by telling me what I am doing wrong. Forget about my linked list logic, let it be wrong. I just wanna know why a segmentation fault is occurring !
you have many mistakes, but your crash is because you don't allocate start
n->next=NULL;
if( start==NULL ) {
start->next=n;
current=n;
}
else {
current->next=n;
current=n;
you ask if start is null, and if it is you try to read it by calling start->next it. you need to allocate it first.
Since you did not tell us the line in which your segfault happened, I can only guess.
At least these lines sounds bogus to me:
if( start==NULL ) {
start->next=n;
current=n;
}
Are you sure that you want to dereference start when it is NULL? That is what you are doing with start->next. Dereferencing NULL causes a segfault.
Maybe this will work the way you expect it:
if( start==NULL ) {
start=n;
current=n;
}
So basically, if start was not defined yet, define it and it also becomes your current item.
You should use debugger (not us) in order to find more information about the problem.
For example gdb (GNU Debuger).
Compilation Error
n->str=malloc(sizeof(char) * 1000);
and this is address reassign on a constant!!!!!You are changing the address of array???
Dangerous Function with 25% probability to execute.
gets(n->str);
Setting value at null pointer!!!
struct node* start=NULL;....
start->next=n;
Example of working Code
#include<stdio.h>
#include<stdlib.h>
struct node {
char *str;
int nn;
struct node* next;
};
int number;
struct node* start=NULL;
struct node* current;
//function to insert into the list
void insert() {
struct node* n;
n=(struct node*)malloc(sizeof(struct node));
n->str=(char*)malloc(sizeof(char) * 1000);
printf("please enter the data that you would like to insert: ");
scanf("%s",n->str);
printf("asdasdasdasd");
n->next=NULL;
if( start==NULL ) {
//start->next=n;
current=n;
}
else {
current->next=n;
current=n;
}
printf("done\n");
}
void display() {
current=start;
int i=0;
while( current->next!=NULL ) {
printf("node%d= %s\n",++i,current->str);
current=current->next;
}
printf("this the end");
}
int main() {
char c;
int input;
do {
printf("Select from the following options:\n"
"1.Display list\n"
"2.Add to list\n"
"3.delete from list\n"
);
scanf("%d",&input);
switch (input) {
case 1: display(); break;
case 2: insert(); break;
// case 3: delete(); break;
default : printf("Please select 1 , 2 or 3\n");
}
printf( "would you like to continue?(y/n)\n");
scanf("%s",&c);
}while(c=='y');
return 0;
}
if( start==NULL ) {
start->next=n;
current=n;
}
I think you have a little problem here ;-)
the next line is not safe:
scanf("%s",&c);
scanf adds terminating null character '\0' that corrupts stack in your case.