I'm writing this code to store data records of students' first name, last name, scores and zip codes. I have almost everything done but my Print() function doesn't print the first element of node 2(first name of 2nd student) and turns into an infinite loop when I input ore than 2 nodes, what am i doing wrong?
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
void Insert(char first[20], char last[20], float data, char zip[50]);
void delete(int e);
void Print();
double median();
struct node
{
char first_name[20];
char last_name[20];
float score;
char zip_code[50];
struct node *ptr;
};
int n;
struct node* head =NULL;
struct node* tail=NULL ;
void Insert(char first[20], char last[20], float data, char zip[50])
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->ptr=NULL;
strcpy(temp->first_name,first);
strcpy(temp->last_name,last);
temp->score=data;
strcpy(temp->zip_code,zip);
if(head==NULL)
{
head=temp;
tail=temp;
return;
}
tail->ptr=temp;
tail=temp;
free(temp);
temp=NULL;
}
void delete(int e)
{
int i;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=head;
if(e==1)
{
head=temp->ptr;
free(temp);
return;
}
else if(e==n)
{
while(temp->ptr->ptr!=NULL)
{
temp=temp->ptr;
}
temp->ptr=NULL;
free(temp->ptr);
return;
}
for(i=0; i<(e-2); ++i)
temp=temp->ptr;
struct node *temp1=temp->ptr;
temp->ptr=temp1->ptr;
free(temp1);
}
void Print()
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=head;
printf("Data entered is below: \n");
while(temp!=NULL)
{
printf("First Name: %s, Last Name: %s, Score: %.2f, Zip Code: %s",temp->first_name,temp->last_name,temp->score,temp->zip_code);
temp=temp->ptr;
printf("\n");
}
printf("\n\n\n");
}
double median()
{
double median,tmp;;
double *ex=(double*)malloc(sizeof(double));/*head*/
double *exe=(double*)malloc(sizeof(double));/*dynamic*/
ex=exe;
int i=1,term,j;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=head;
while(i<=n)
{
temp->ptr;
*exe=temp->score;
exe++;
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if( *(ex+i) > *(ex+j))
{
tmp = *(ex+i);
*(ex+i) = *(ex+j);
*(ex+j) = tmp;
}
}
}
if(n%2==0)
{
/*even;median=n/2-1*/
term=(n/2)-1;
median= (*(ex+(term-1)));
return median;
}
/*odd; median=n-1/2*/
term=(n-1)/2;
median= (*(ex+(term-1)));
return median;
}
int main()
{
char name1[20],name2[20], code[50];
float x;
int i,option,index;
printf("Enter the number of nodes: ");
scanf("%d",&n);
printf("Please enter the records of students in the following format(click enter for new students)\n");
printf("First_Name Last_Name Score Zip_Code\n");
for(i=1; i<=n; ++i)
{
scanf(" %s",name1);
scanf(" %s",name2);
scanf(" %f",&x);
scanf(" %s",code);
Insert(name1,name2,x,code);
}
printf("\n");
while(1)
{
printf("Choose one of the following options: \n");
printf("Print records (press 1)\nAdd a new record (press 2)\nDelete record(s) (press 3)\nSearch by zip code (press 4)\nSearch by score range (press 5)\nFind median score (press 6)\nExit the program (press 0)\n");
scanf("%d",&option);
switch(option)
{
case 0:
{
/*Exit Program*/
exit(0);
break;
}
case 1:
{
/*print*/
Print();
break;
}
case 2:
{
/*insert*/
getchar();
printf("Enter the new record in the following format: \nFirst_Name Last_Name Score Zip_Code\n");
scanf("%s",name1);
scanf("%s",name2);
scanf("%f",&x);
scanf("%s",code);
getchar();
Insert(name1,name2,x,code);
break;
}
case 3:
{
/*delete*/
printf("Enter the node/record to be deleted: ");
scanf("%d",&index);
delete(index);
printf("The deletion of record %d has been succesfully completed!\n\n",index);
break;
}
case 4:
{
/*search by zip*/
break;
}
case 5:
{
/*search by score*/
break;
}
case 6:
{
/*find median*/
printf("Median score for the entered records is: %f",median());
break;
}
}/*switch*/
}/*while*/
return 0;
}
The Insert function is holding a reference to freed data:
tail=temp;
free(temp);
Attempting to use the storage referenced by tail after it's been freed is an error.
Related
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct Node
{
char firstname[100];
char lastname[100];
char number[100];
char mail[100];
struct Node *next;
}*head;
void insert(char* firstname,char* lastname,char* number,char* mail)
{
struct Node * node=(struct Node *)malloc(sizeof(struct Node));
strcpy(node->firstname, firstname);
strcpy(node->lastname, lastname);
strcpy(node->number, number);
strcpy(node->mail, mail);
node->next=NULL;
if(head==NULL)
{
head=node;
}
else{
node->next=head;
head=node;
}
}
void search(char* firstname)
{
struct Node * temp = head;
while(temp!=NULL){
if(temp->firstname==firstname){
printf("Contact Found");
printf("Firstname:%s\n",temp->firstname);
printf("Lastname:%s\n",temp->lastname);
printf("PhoneNumber:%s\n",temp->number);
printf("Mail Id:%s\n",temp->mail);
return;
}
temp = temp->next;
}
printf("%s is not found in the contact \n",firstname);
}
void update(char* firstname)
{
struct Node * temp=head;
while(temp!=NULL){
if(temp->firstname==firstname){
printf("Contact Found");
printf("Enter the new Phone number for %s\n",temp->firstname);
scanf("%s",temp->number);
printf("Contact Updated Successfully\n");
return;
}
temp=temp->next;
}
printf("%s is not found in the contact \n",firstname);
}
void delete(char* firstname)
{
struct Node * temp1 = head;
struct Node * temp2 = head;
while(temp1!=NULL){
if(temp1->firstname==firstname){
printf("Contact Found for deleting\n");
if(temp1==temp2){
head = head->next;
free(temp1);
}
else{
temp2->next = temp1->next;
free(temp1);
}
printf("Contact deleted Successfully\n");
return;
}
temp2=temp1;
temp1=temp1->next;
}
printf("%s is not found in the contact \n",firstname);
}
void display()
{
struct Node * temp=head;
while(temp!=NULL){
printf("Firstname:%s\n",temp->firstname);
printf("Lastname:%s\n",temp->lastname);
printf("PhoneNumber:%s\n",temp->number);
printf("Mail Id:%s\n",temp->mail);
temp = temp->next;
}
}
int main()
{
head = NULL;
int choice;
char firstname[100];
char lastname[100];
char number[100];
char mail[100];
printf("-------Welcome--------\n ");
printf("1.Insert a Contact\n2.Search a Contact\n3.Delete a Contact\n4.Update a Contact\n5.Display all the Contacts");
do
{
printf("\nEnter Choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter Firstname:");
scanf("%s",firstname);
printf("Enter Lastname:");
scanf("%s",lastname);
printf("Enter PhoneNumber:");
scanf("%s",number);
printf("Enter Mail Id:");
scanf("%s",mail);
insert(firstname,lastname,number,mail);
break;
case 2:
printf("Enter Firstname to Search:");
scanf("%s",firstname);
search(firstname);
break;
case 3:
printf("Enter Firstname to Delete:");
scanf("%s",firstname);
delete(firstname);
break;
case 4:
printf("Enter Firstname to Update:");
scanf("%s",firstname);
update(firstname);
break;
case 5:
display();
break;
}
}while (choice != 0);
}
this c program runs without error but search,delete,update funtion is not working...you can refer the img for more details.
tommorrow i have to submit my mini project..so if anyone knows c program please help me
Enter Choice: 2
Enter Firstname to Search:durai
durai is not found in the contact
Enter Choice: 3
Enter Firstname to Delete:durai
durai is not found in the contact
Enter Choice: 4
Enter Firstname to Update:durai
durai is not found in the contact
these are the errors which i'm getting
For example within the function search you are trying to compare two pointers that point to different extents of memory
if(temp->firstname==firstname){
So the condition evaluates to false even if the pointed strings are equal each other. You have to use the standard string function strcmp
For example
if( strcmp( temp->firstname, firstname ) == 0 ){
Pay attention to that all function parameters that have the type char * should be declared as having the type const char *.
Also it is a bad idea when the functions depend on the global variable head. For example in this case you can not create more than one list in a program.
I was asked to make a hash table program which input name and age. I declared name and age as char. While doing the search function, I need to print the name and the age but the age do not print. What is wrong with my code?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int n;
int count;
struct data
{
char name [100];
char age[5];
struct data *next;
};
struct data *chain[100000];
struct data* insert(char name[], char age[]){
struct data *curr = (struct data*) malloc (sizeof(data));
strcpy(curr->name, name);
strcpy(curr->age, age);
curr->next = NULL;
return curr;
};
//hash function
int hashing(char name[]){
int first = name[0];
return first % n;
}
//buat tau di index ke x dia data ke brp
void add(struct data *newNode){
int hash = hashing(newNode->name);
printf("index->%d\n", hash);
if(chain[hash] == NULL){
chain[hash] = newNode;
}
else
{
struct data *temp = chain[hash];
while(temp->next != NULL){
temp = temp->next;
//count++;
}
count++;
temp->next = newNode;
//count++;
}
printf("data-> %d\n", count);
}
void view(){
for(int i = 0 ; i < n ; i ++)
{
int count = 0;
if(chain[i]!=NULL){
printf("=== index %d data ===\n", i );
struct data *temp = chain[i];
while(temp!=NULL){
printf("data %d\n", count);
printf("name=%s age=%s\n", temp->name, temp->age);
temp = temp->next;
count ++;
}
}
}
printf("\n");
}
void search(int hash){
char age[5];
char search[100];
printf("Search name:");
scanf(" %[^\n]s", search);
int key = hashing(search);
printf("Search data %s from index %d data....\n", search, key);
if(strcmp(chain[key]->name, search)==0){
printf("data found at node %d\n", key);
printf("name = %s age = %s\n", search, age);
}
else
{
struct data *temp = chain[key];
while(temp->next!=NULL){
if(strcmp(chain[key]->name, search)==0){
printf("data found at node %d\n", key);
printf("name = %s age = %s\n", search, age);
}
temp = temp->next;
}
if(strcmp(temp->name, search)==0 && temp->next==NULL){
printf("data found at node %d\n", key);
printf("name = %s age = %s\n", search, age);
}
else{
printf("no data found!\n");
}
}
}
//void remove(int hash){
// //int key = item->key;
// int hash = hashing(key);
// int option;
// printf("input option:");
// scanf("%d", &option);
// char search[100];
// printf("delete name:");
// scanf(" %[^\n]s", search);
// int first = search[0];
// int key = first % n;
// printf("Search data %s from index %d data....\n", search, key);
// if(chain[key]==hash){
// chain[key]=-1;
// }
// else
// {
// printf("try");
// }
//}
int main(){
char name[100];
char age[5];
printf("input the number of hash table: ");
scanf("%d", &n); getchar();
struct data *chain[n];
int chainSize[n]={0};
printf("\n");
int option;
do{
printf("=== Option Menu ===\n");
printf("1. insert data\n");
printf("2. delete data\n");
printf("3. search data\n");
printf("4. view data\n");
printf("5. exit\n");
printf("input option: ");
scanf("%d", &option); getchar();
switch(option){
case 1:{
printf("input name: ");
scanf(" %[^\n]s", name);
printf("input age: ");
scanf(" %[^\n]s", age);
add(insert(name,age));
printf("\n");
break;
}
case 2:
{
//remove();
break;
}
case 3:
{
search(n);
break;
}
case 4:
{
view();
break;
}
}
}while(option!=5);
}
the output is like this
name = hansol
age =
when it is supposed to be like this:
name = hansol
age = 23
so the age variable does not print while doing the search function
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 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.
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.