#include <stdio.h>
struct list
{
int data;
struct list *next;
};
struct list *start, *end;
void add(struct list *head, struct list *list, int data);
void delete(struct list *head, struct list *tail);
int main(void)
{
start=end=NULL;
add(start, end, NULL);
add(start, end, NULL);
printf("First element: %d");
delete(start, end);
return 0;
}
void add(struct list *head, struct list *tail, int data)
{
if(tail==NULL)
{
head=tail=malloc(sizeof(struct list));
head->data=data; head->next=NULL;
} else {
tail->next=malloc(sizeof(struct list));
tail=tail->next;
tail->data=data;
tail->next=NULL;
}
}
void delete(struct list *head, struct list *tail)
{
struct list *temp;
if(head==tail)
{
free(head);
head=tail=NULL;
} else {
temp=head->next;
free(head);
head=temp;
}
}
I am aiming to return an output of 3 but keep getting random results. Any insight is greatly appreciated
As you want to modify head and tail you need to pass a pointer to them.
i.e.
void add(struct list **head, struct list **tail, int data)
{
if(*tail==NULL)
{
*head = *tail = malloc(sizeof(struct list));
(*head)->data = data;
(*head)->next = NULL;
} else {
(*tail)->next = malloc(sizeof(struct list));
*tail = (*tail)->next;
(*tail)->data = data;
(*tail)->next = NULL;
}
}
Do similarly for the other function. Then head and tail will also change outside the function.
The line
printf("First element: %d");
It requires an integer to print - supply it - see the manual page for printf
Avoid using keywords for C++ in C programs - such as delete
Parameters of C functions are passed by value. Thus, the changes that you make to the values of head and tail in the add() function will not be reflected in main() from which add() is called.
And, it doesn't seem that you've supplied a second parameter for printf(), so the %d format is not going to get the integer value that it will be looking for.
In your program start and end are global variables, so there is no need to pass it as arguments to other functions, because other methods can directly access it.
//Structure to store data
struct list
{
int data;
struct list *next;
};
// global variables
struct list *start, *end;
void add(int);
void delete();
// start of program
int main(void)
{
start=end=NULL;
add(5);
add(6);
printf("\nFirst element: %d",start->data);
delete();
printf("\nFirst element: %d",start->data);
return 0;
}
//add node to list
void add(int data)
{
if(end==NULL)
{
start=end=malloc(sizeof(struct list));
start->data=data; start->next=NULL;
} else {
end->next=malloc(sizeof(struct list));
end=end->next;
end->data=data;
end->next=NULL;
}
}
// delete node from list
void delete()
{
struct list *temp;
if(start==end)
{
free(start);
start=end=NULL;
} else {
temp=start->next;
free(start);
start=temp;
}
}
OUTPUT:
First element: 5
First element: 6
Note:
If you don't want your start and end to be global than it can be a local variable of main function. Here you have to either work on double pointer mechanism or return memory address to retain the modification.
There are a number of problems here.
First, when you pass a pointer to a function, the pointer is passed by value. Any changes to the pointer in the called function will not be reflected in the calling function. To change what the pointer in the calling function is pointing to, you need to pass a pointer-to-pointer. So your add()function needs to be:
void add(struct list **head, struct list **tail, int data) {
if(*tail == NULL) {
*head = *tail = malloc(sizeof(struct list));
(*head)->data = data;
(*head)->next = NULL;
}
else {
(*tail)->next = malloc(sizeof(struct list));
(*tail) = (*tail)->next;
(*tail)->data = data;
(*tail)->next = NULL;
}
return;
}
And your delete() function needs to be changed similarly.
Secondly, you are passing NULL as your data value to the add function. NULL is a pointer; it is typically defined as a macro, and could be (void*) 0 in the implementation. It should not be used as an integer. Pass 0 as the integer, not NULL.
Third, you have this statement:
printf("First element: %d");
Your format string has the conversion specifier %d but there is no argument that matches the %d. This is undefined behavior. What integer exactly are you trying to print?
Where do you expect the output 3, and for what reason?
Related
So I try to print a linked list, and it says that the argument head is incompatible argument type.
The list is already made correctly and works if not put in another function. I just can't understand why this specific function doesn't work.
struct data {
char foodName[FILENAME_MAX];
int rating;
float price;
};
typedef struct listElement {
struct listElement *next;
struct data food;
struct listElement *previous;
} listElement;
void printList(listElement *head);
void printElement(listElement *element);
int main()
{
struct data food;
listElement head;
printList(head); <-- this one gets an error
return 0;
}
void printList(listElement *head)
{
if (head == NULL) {
printf("Linked list is empty.\n");
} else {
printf("Printing linked list to result file...\n");
printElement(head);
printf("Linked list successfully printed to result file.\n");
}
}
void printElement(listElement *element)
{
if (element == NULL || file == NULL) {
return;
} else {
printf ("name = %s rating = %d price = %f\n", element->food.foodName, element->food.rating, element->food.price);
printElement(element->next);
}
In this function void printList(listElement *head) you are expecting a pointer to a listElement but in the main function you are calling the printList function with a listElement type, not a listElement* type.
To fix this you have to declare your head variable like this listElement* head; or you have to give the printList function a pointer to the head variable, like this printList(&head);.
Either way, it depends on what you want to do with it. But the first suggestion is more common.
I want to make a CreatNode() function in C to be called by other functions. I am playing around with code trying to reach great readability and functionality. The professor has a CreatEmptyList() function but not a CreatNode().She is negligent and not the capable of the concepts and C lagnguage and didn't give me an answer.
I don't need this exploration and trying ideas that come to my mind to pass the course, but my aim is to become a Dev not to graduate.
This is the code of the Prof:
typedef struct nodetype
{
int info;
struct nodetype *next;
} node;
node *head;
void createemptylist(node *head)
{
head=NULL;
}
void insertatbeginning(node *head, int item)
{
node *newNode;
/* allocate memory for the new node and initialize the data in it*/
newNode= malloc(sizeof(node));
newNode->info=item;
/* assign the value of head to the “next” of newNode*/
newNode->next=head;
/* assign the address of newNode to head */
head=newNode;
}
void insertatend(node *head, int item)
{
node *newNode;
newNode=malloc(sizeof(node));
newNode->info=item;
newNode->next=NULL;
if(head==NULL)
head=newNode;
else
{
node *prev=head;
while(prev->next!=NULL)
prev=prev->next;
prev->next=newNode;
}
}
All are the snippets from the PDF she provided not exactly a compilable code.
This is the code I am working on and it keeps giving errors:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int info;
struct Node *Next;
}ListNode;
ListNode CreatNode(ListNode *Head){///These steps not to be repeated using this function
printf("\n=================\nEntered CreatNode Function");
ListNode *NewNode;
NewNode = malloc(sizeof(ListNode));
return *NewNode;
}
void CreatList(ListNode *Head){
printf("\n=================\nEntered CreatList Function");
Head = NULL;
}
void InserBeg(ListNode *Head, int item){
///CreatNode() steps here
NewNode=CreatNode(&Head);
NewNode->info = item; ///Inesrt value
NewNode->Next = Head;///Insert Adress inside Head to the Next point
Head = NewNode;
printf("\nFinished InsertBeg Function");
printf("\nValue inserted is: %d\n=================\n", NewNode->info);
}
void Append(ListNode *Head, int item){
///CreatNode() steps here
///NewNode=CreatNode(Head);
NewNode ->info = item;
NewNode ->Next = NULL;
if (Head==NULL){
Head=ListNode
}
else{
ListNode *Prev=Head;
while(while->Prev!=NULL){
Prev = Prev->Next;
}
Prev->Next=NewNode;
}
}
int main(){
ListNode *Head;
CreatList(&Head);
InserBeg(&Head, 8);
return 0;
}
errors:
C:\Users\User\Desktop\all\C\Single Linked List test.c|27|error: incompatible types when assigning to type 'ListNode * {aka struct Node *}' from type 'ListNode {aka struct Node}'|
Undeclared NewNode struct errors since it can't see it
Any help on coding my idea in different ways or make my code work?
The provided by the professor code is very bad.
For starters she uses a global variable head. As the variable is declared in the file scope then it is already initialized as a null pointer. So this function
void createemptylist(node *head)
{
head=NULL;
}
does not make a great sense. And moreover it does nothing with the original pointer head because it accepts its argument by value. That is it deals with a copy of the value of the original pointer.
By this reason other functions insertatbeginning and insertatend are wrong because they do not change the original pointer head that they accept by value.
void insertatbeginning(node *head, int item)
{
//...
head=newNode;
}
void insertatend(node *head, int item)
{
//...
head=newNode;
//...
}
They change a copy of the value of the original pointer.
The same problem is present in your functions.
As for the function CreatNode then as is it does not make a sense.
ListNode CreatNode(ListNode *Head){///These steps not to be repeated using this function
printf("\n=================\nEntered CreatNode Function");
ListNode *NewNode;
NewNode = malloc(sizeof(ListNode));
return *NewNode;
}
For starters the parameter head is not used within the function. You need to pass an integer argument to the function that will be used as an initializer for the data member info.
Instead of an object of the type ListNode you should return a pointer to the dynamically allocated object. Otherwise the function returns a copy of the dynamically allocated object and as a result the function will produce a memory leak.
Within the functions InserBeg and Append the name NewNode is undefined as for example
void InserBeg(ListNode *Head, int item){
///CreatNode() steps here
NewNode=CreatNode(&Head);
//...
And you are calling the functions passing expressions of incompatible pointer type ListNode ** instead of ListNode *.
CreatList(&Head);
InserBeg(&Head, 8);
Here is a demonstration program that shows how the function CreateNode and for example InsertBeg can be defined.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int info;
struct Node *next;
} ListNode;
ListNode * CreateNode( int info )
{
ListNode *new_node = malloc( sizeof( *new_node ) );
if ( new_node != NULL )
{
new_node->info = info;
new_node->next = NULL;
}
return new_node;
}
int InsertBeg( ListNode **head, int info )
{
ListNode *new_node = CreateNode( info );
int success = new_node != NULL;
if ( success )
{
new_node->next = *head;
*head = new_node;
}
return success;
}
int main( void )
{
ListNode *head = NULL;
if ( InsertBeg( &head, 10 ) )
{
puts( "New node is added." );
}
else
{
puts( "Error: not enough memory." );
}
}
The program output is
New node is added.
I've got a LinkedList node struct defined as such:
struct intnode {
int item;
struct intnode *next;
};
typedef struct intnode IntNode;
Which I am using to do some simple sorting. However, in creating this linked list, I am having difficulty with scope. In my main function I've got a Null header IntNode object to serve as the first element in the list, however I can't modify it within my insert function, despite the fact that I'm passing a pointer to it. The code never reaches the print statement saying the list is no longer empty, which I am confused by. Does this have something to do with defining a new IntNode element to add within the insert function which is then thrown away after the function is done?
int main() {
IntNode *header = NULL;
printf("Enter some numbers, ending with -1: ");
int a;
while(a != -1) {
scanf("%d",&a);
if(a != -1) {
insert(header, a);
}
}
return 0;
}
IntNode *createNode(int val) {
IntNode *new_node;
new_node = malloc(sizeof(IntNode));
new_node->item = val;
return new_node;
}
void insert(IntNode *header, int val) {
IntNode *newNode = createNode(val);
if(header == NULL) { //list is empty, so insert at front
printf("list still empty\n");
newNode->next = header;
header = newNode;
printf("%d",header->item);
} else {
printf("the list is no longer empty...");
//do more stuff here
}
}
If you want to modify a pointer, you need to pass a pointer of that pointer. Therefore
insert(header, a);
should be
insert (&header, a);
and adjust your insert() function accordingly.
I almost figured out this code, but there are two details I can't figure out.
I found this code on YouTube.
source: https://www.youtube.com/watch?v=VOpjAHCee7c
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int vaule;
struct node *next;
}node_t;
void printlist(node_t *head)
{
node_t *temp = head;
while(temp != NULL)
{
printf("%d - ", temp->vaule);
temp = temp->next;
}
printf("\n");
}
node_t *create_node(int var)
{
node_t *res = malloc(sizeof(node_t));
res->vaule = var;
res->next = NULL;
return res;
}
node_t *insert_at_head(node_t **head, node_t *node_to_insert)
{
node_to_insert->next = *head;
*head = node_to_insert;
return node_to_insert;
}
void find_node()
int main()
{
node_t *tmp;
node_t *head = NULL;
for(int i = 0; i < 15; i++)
{
tmp = create_node(i);
head = insert_at_head(&head, tmp);
}
printlist(head);
return 0;
}
1) Why do we use the nested struct?
typedef struct node{
int vaule;
struct node *next;
}node_t;
I know about nested structures but I didn't understand why we use it here.
2) Why do we use double pointer?
node_t *insert_at_head(node_t **head, node_t *node_to_insert)
{
node_to_insert->next = *head;
*head = node_to_insert;
return node_to_insert;
}
if I change this code like this:
node_t *insert_at_head(node_t *head, node_t *node_to_insert)
{
node_to_insert->next = head;
return node_to_insert;
}
then nothing will change
Why do we use the nested struct?It's not a nested struct. struct node *next is a pointer, and as its name indidcates, it points to the next element.
Why do we use double pointer? Read this: How do I modify a pointer that has been passed into a function in C?
1)Why do we use the nested struct?
It is not a nested struct, but a linked list. Each node has a pointer to the next node (or to NULL for the last node of a list
2)Why do we use double pointer?
C only passes parameters by value. The idiomatic ways to change a variable from the caller are:
assign the return value to that variable. It is the best way, but you can only return one single value that way
pass a pointer to the variable and use the pointer to change the value. As we want to change the value of head which is already a pointer, we have to pass a pointer to pointer.
Here the write of the code has decided to change the passed header to clearly show that it is an input/output parameter, and also returns it because it had no better value to return.
I am expecting the below linked list program to print 1
but its not.can anyone figure out why?
#include<stdio.h>
#include <stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node * link;
};
typedef struct node NODE;
void display(NODE *);
void add(NODE *,int );
int main()
{
NODE *head=NULL;
add(head,1);
display(head);
printf("\n");
getch();
return 0;
}
void display(NODE *pt)
{
while(pt!=NULL)
{
printf("element is");
printf("%d",pt->data);
pt=pt->link;
}
}
void add(NODE *q,int num)
{
NODE *temp;
temp = q;
if(q==NULL)
{
q=(NODE *)malloc(sizeof(struct node));
temp = q;
}
else
{
while((temp=temp->link)!=NULL);
temp->link = (NODE *)malloc(sizeof(struct node));
temp=temp->link;
}
temp->data = num;
temp->link = NULL;
}
Your local variable head in main() is not modified by your add() function. This means you are calling display() with a parameter of NULL.
You'll need to pass a NODE **q into add, then update it in add().
Your add method when called first time (when head == NULL) should add the first node to the list thus changing head to point to the newly allocated node.
But this does not happen as add does not communicate back the changed head to the caller.
To fix this you can either return the modified head from the function:
// call as before but assign the return value to head.
head = add(head,1);
.....
// return type changed from void to NODE *
NODE* add(NODE *q,int num)
{
// make the changes as before
// return the head pointer.
return q;
}
or you can pass the pointer head by address to the function add as:
// pass the address of head.
add(&head,1);
.....
// change type of q from NODE * to NODE **
void add(NODE **q,int num)
{
// same as before but change q to *q.
// any changes made to the list here via q will be visible in caller.
}
The add() function is modifying the q argument, but it is passing it by value. Then head remains NULL after the add() call.
The problem is in signature of add method, to make your program work you should pass pointer to pointer of NODE, like this
void add(NODE **,int );
and work with him.
Then in case
if(*q==NULL)
you can allocate memory and replace NULL pointer to new HEAD
by it
*q=(NODE*)malloc(sizeof(struct node));
So it will work.
The problem is when you allocate memory you just replace local copy of null pointer to NODE but it doesn't affect head in main function.
int main()
{
NODE *head=NULL;
add(head,1);
display(head);
NODE *head is local to main. It's value is NULL. You pass the value NULL to add, which then creates a NODE and sets its data to 1. You then return to main... ...where head is STILL NULL. You need to pass the address of head, so that it's actual value is changed in add(). You also need to change add() to work with a pointer.
Main should return EXIT_SUCCESS or EXIT_FAILURE. Don't typedef struct node; it's harmful to readability and you get no abstraction here from using it.
When you call Add the new head pointer never gets returned. So it still points to NULL.
Ah... you're getting tripped up by the pointers...
Essentially, if you want to modify "head", you need to send a reference to THAT... otherwise you are just modifying the pointer... Change your code to this:
#include<stdio.h>
#include <stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node * link;
};
typedef struct node NODE;
void display(NODE *);
void add(NODE **,int );
int main()
{
NODE *head=NULL;
add(&head,1);
display(head);
printf("\n");
getch();
return 0;
}
void display(NODE *pt)
{
while(pt!=NULL)
{
printf("element is ");
printf("%d",pt->data);
pt=pt->link;
}
}
void add(NODE **q,int num)
{
NODE *temp;
temp = *q;
if(*q==NULL)
{
*q=(NODE *)malloc(sizeof(struct node));
temp = *q;
}
else
{
while((temp=temp->link)!=NULL);
temp->link = (NODE *)malloc(sizeof(struct node));
temp=temp->link;
}
temp->data = num;
temp->link = NULL;
}