Error in simple linked list code - c

I try to create a simple linked list out of a integer vector. Why is this not working? The errors I get are:
'=' : incompatible types - from 'talstrul *' to 'node *'
'=' : cannot convert from 'talstrul*' to talstrul'
This is the .h file:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int num;
struct node *next;
} talstrul;
This is the .c file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "clabb2head.h"
int main()
{ int vek[5] = {1,2,3,4,5};
int langd = sizeof(vek)/sizeof(vek[0]);
printf("%d",langd);
talstrul *temp = malloc(sizeof(talstrul));
talstrul *pek1 = NULL;
int i;
for(i=0; i<langd; i++)
{
pek1 -> num = vek[i];
pek1 -> next = *temp;
*temp = pek1;
}
}

temp is of type talstrul *
talstrul *temp = malloc(sizeof(talstrul));
You are trying to assign to next, which is of type node *
struct node *next;
Further
pek1 -> next = *temp;
dereferences temp, yielding a talstrul. You should not dereference the pointer.
The compiler is giving you a pretty good explanation of what is going wrong.

Another problem with the code :
pek1 is assigned to NULL. Yet you try to assign pek1->num and pek1->next. You should do memory allocation for pek1 first.

typedef struct node {
int num;
struct node *next;
} talstrul;
...
int vek[5] = {1,2,3,4,5};
int langd = sizeof(vek)/sizeof(vek[0]);
talstrul *pek1 = NULL;
int i;
for(i=0; i<langd; i++){
talstrul *temp = malloc(sizeof(talstrul));
temp -> num = vek[i];
temp -> next = pek1;
pek1 = temp;
}

Related

Inserting nodes in a linked list in decreasing order - C

I have to make a list that arrange the people in decreasing order of their number('no' for my program). I tryed to make it by modifying the addNode function but I got no result(peoples do not arrange by their number). This is my code:
Header code:
#ifndef __EX__
#define __EX__
typedef struct Person{
char name[10];
float no;
struct Person *pNext;
} NODE, *pNODE, **ppNODE;
void addNode(ppNODE, pNODE);
void travers(pNODE, unsigned int*);
#endif
Functions folder:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include "EX.h"
void addNode (ppNODE ppPrim, pNODE p){
pNODE q = (pNODE)malloc(sizeof(NODE));
assert(q!=NULL);
printf("Add name: \n");
scanf("%s", &q->name);
printf("\nAdd no: ");
scanf("%f", &q->no);
if (p == NULL || q->no < p->no) {
q->pNext = *ppPrim;
*ppPrim = q;
} else {
q->pNext = p->pNext;
p->pNext = q;
}
return;
}
void travers(pNODE pPrim, unsigned int *pLen){
*pLen = 0;
pNODE tmp = pPrim;
while (tmp != NULL){
puts (tmp->name);
fprintf(stdout, " no %.2f\n", tmp->no);
tmp = tmp->pNext;
(*pLen)++;
}
return;
}
Main folder:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include "EX.h"
int main(){
unsigned int len;
pNODE prim = NULL;
int i;
for (i=0; i<=1; i++){
addNode(&prim, prim);
addNode(&prim, prim->pNext);
}
travers(prim, &len);
return 0;
}
When you insert a new node to the list, you must traverse the list until you find a suitable place to insert it. Your code takes a second argument, which isn't really needed and causes confusion, and only looks at that.
The code to insert a code q at the end of a list that is defined by its head is:
Node *prev = NULL;
Node *p = *head;
while (p) {
prev = p;
p = p->pNext;
}
q->pNext = p;
if (prev == NULL) {
*head = q;
} else {
prev->pNext = q;
}
You can get rid of keeping track of the previous node and the distinction between inserting at the head and inserting after that by traversing the list with a pointer to node pointer:
Node **p = &head;
while (*p && (*p)->no < q->no) {
p = &(*p)->pNext;
}
q->pNext = *p;
*p = q;
In this concise code, p holds the address of the head at first and the address of the pNext pointer of the previous node. Both can be updated via *p.
You can now use this code to traverse only as far as the numbers associated with each node are smaller than the one of the node to insert. here's a complete program:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct Node Node;
void addNode(Node **p, const char *name, float no);
void travers(Node *pPrim, unsigned int *pLen);
struct Node {
char name[10];
float no;
Node *pNext;
};
void addNode(Node **p, const char *name, float no)
{
Node *q = malloc(sizeof(*q));
assert(q != NULL);
snprintf(q->name, sizeof(q->name), "%s", name);
q->no = no;
while (*p && (*p)->no < q->no) {
p = &(*p)->pNext;
}
q->pNext = *p;
*p = q;
}
void traverse(const Node *pPrim, unsigned int *pLen)
{
*pLen = 0;
while (pPrim != NULL) {
fprintf(stdout, "%-12s%.2f\n", pPrim->name, pPrim->no);
pPrim = pPrim->pNext;
(*pLen)++;
}
}
int main()
{
unsigned int len;
Node *prim = NULL;
addNode(&prim, "Alice", 0.23);
addNode(&prim, "Bob", 0.08);
addNode(&prim, "Charlie", 0.64);
addNode(&prim, "Dora", 0.82);
traverse(prim, &len);
printf("\n%u entries.\n", len);
return 0;
}
Things to node:
I've used Node * and Node ** instead of the typedeffed pNODE and ppNODE. In my opinion using the C pointer syntax is clearer.
You should separate taking user input from adding a node.
In your code you shouldn't pass the address of the char array when scanning a string, just the char array. (It happens to work, but it isn't correct. The compiler should warn you about that.)

C - Deleting a node in a linked list

I'm working on a delete function in my code. I want to delete the key, value pair within the node and free the space allocated to it. I'm not sure how to approach this so that the following nodes shift into the right place (not sure how to word it, hope you know what I mean). Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "symTable.h"
#define DEFAULT_TABLE_SIZE 61
#define HASH_MULTIPLIER 65599
/*Structures*/
typedef struct Node
{
char *key;
void *value;
struct Node *next;
} Node_T;
typedef struct SymTable
{
Node_T **Table;
int tablesize;
int counter;
} *SymTable_T;
/*Global Variables*/
int tablesize = DEFAULT_TABLE_SIZE;
int counter = 0;
/*Create function to show how memory is allocated*/
SymTable_T SymTable_create(void)
{
SymTable_T S_Table;
S_Table = malloc(sizeof(SymTable_T *) * DEFAULT_TABLE_SIZE);
S_Table->Table = (Node_T **) calloc(DEFAULT_TABLE_SIZE, sizeof(Node_T *));
return S_Table;
}
/*Hash Function*/
static unsigned int hash(const char *key, const int tablesize)
{
int i;
unsigned int h = 0U;
for (i = 0; key[i] != '\0'; i++)
h = h * tablesize + (unsigned char) key[i];
return h % tablesize;
}
/*Delete Function*/
int symTable_delete(SymTable_T symTable, const char *key)
{
Node_T *new_list;
unsigned int hashval = hash(key, DEFAULT_TABLE_SIZE);
free(new_list->key);
free(new_list->value);
//here is where I am stuck, how can I make it so the nodes following the one deleted go to the right space?
}
With a singly linked list you have
A -> B -> C
if you want to remove B then you need to make
A -> C
The only way to do this is to get the parent of B and update its pointer which means either
You need to add a pointer from a node to its parent ( aka use a doubly linked list )
You iterate over the list until you find a node where the child pointer is set to the node you are removing and then you update the pointer to instead point to child.child

How to access an element of an array just given a pointer

struct node
{
int a;
node * link;
}
i have an array A with each element of type 'pointer to node' and hence each element of A can have variable size.Example
A[0]=NULL
A[1]=2->3->4
A[2]=3->4
and so on..
so to dynamically allocate an array if I use
u = (struct node*) malloc( m * sizeof(struct node*) )
then
u+i = NULL
(i is any integer) gives error as Lvalue required.
If I use array pointer as
struct node(*p)[];
and then use
(*p)+i = NULL
it gives error as L value required.
*(p+i) = NULL
gives error as
invalid use of array with unspecified bounds
What is the solution?
#include <stdio.h>
#include <stdlib.h>
typedef struct node node;
struct node{
int a;
node * link;
};
void print(node *np){
while(np){
printf("%d->", np->a);
np = np->link;
}
printf("NULL\n");
}
int main(){
struct node four = {4, NULL};
struct node three = {3, &four};
struct node two = {2, &three};
struct node **u;
int m = 3;
u = malloc(m * sizeof(struct node*));
u[0] = NULL;
u[1] = &two;
u[2] = &three;
for(int i=0;i<m;++i)
print(u[i]);
free(u);
return 0;
}
I think what you want is:
(*p) += i;
(*p) = NULL;
or
p[i] = NULL;
Here is a working example:
#include <stdio.h>
#include <string.h>
typedef struct s_node {
int x;
struct s_node *next;
} node ;
main()
{
node n[5];
n[2].x = 42;
printf("%d\n", n[2].x);
node *p = n;
printf("%d\n", p[2]);
p += 2;
printf("%d\n", p->x);
}
Output:
42
42
42
Consider to take a look at a tutorial for pointer arithmetic. Just google for it or click the provided link.

Assignment makes pointer from Integer without a Cast in Structure

I'm making a program that dynamically creates a list of integers.
int ins_dlist(int data, struct dlist **p){
struct dlist *q;
if((*p)->sz == (*p)->maxsz){
q = realloc(*p, DLISTSZ((*p)->maxsz + INCRSZ));
if(q == NULL)
return (-1);
q->maxsz += INCRSZ;
*p = q;
}
//(*p)->item[(*p)->sz++] = data; <-Gives me pointer from integer without cast
*((*p)->item + (*p)->sz++) = data;
return(0);
}
My problem is on *((*p)->item + (*p)->sz++) = data; I tried declaring it in different ways but I still can't get access to the sz variable in my struct.
Heres my struct declaration, its inside a file named dlist.h:
#include <stdlib.h>
struct dlist{
int sz;
int maxsz;
int *item[1];
};
#define INITSZ 5
#define INCRSZ 5
#define DLISTSZ(n) ((size_t)(sizeof(struct dlist)) + ((n-1)*sizeof(int)))
struct dlist *init_dlist(int num);
int ins_dlist(int data, struct dlist **p);
You probably wanted to define dllist as:
struct dlist{
int sz;
int maxsz;
int item[1];
};

LinkedList Struct Typedef in C

I am try to build a basic linked list in C. I seem to get an error for this code:
typedef struct
{
char letter;
int number;
list_t *next;
}list_t;
char letters[] = {"ABCDEFGH"};
list_t openGame, ruyLopez;
openGame.letter = letters[4];
openGame.number = 4;
openGame.next = &ruyLopez;
ruyLopez.letter = letters[5];
ruyLopez.number = 4;
ruyLopez.next = NULL;
It won't accept my definition in the struct:
list_t *next;
And for the same reason it won't accept:
openGame.next = &ruyLopez;
When you are using list_t *next in your code, the compiler doesn't know what to do with list_t, as you haven't declared it yet. Try this:
typedef struct list {
char letter;
int number;
struct list *next;
} list;
As H2CO3 pointed out in the comments, using _t as an identifier suffix is not a great idea, so don't use list_t.
why did you make it hard on yourself just set openGame and ruzeLopez as pointers and you wont have to use the & (this is the usual way to use linked lists , just don't forget to use -> to access members)
try this code instead :
#include <stdlib.h>
#include <malloc.h>
typedef struct list
{
char letter;
int number;
struct list *next;
}list;
main(void)
{
char letters[] = "ABCDEFGH"; //what were the braces for ?
list *openGame, *ruyLopez;
openGame = ruyLopez = NULL;
openGame = malloc(sizeof(list));
ruyLopez = malloc(sizeof(list));
openGame->letter = letters[4];
openGame->number = 4;
openGame->next = ruyLopez;
ruyLopez->letter = letters[5];
ruyLopez->number = 5;
ruyLopez->next = NULL;
}
Here is a working example without the risk of memory leaks from using malloc to create your structures.
#include <stdlib.h>
typedef struct _list
{
char letter;
int number;
struct _list *next;
} list_type;
int main(void)
{
char letters[] = "ABCDEFGH"; //what were the braces for ?
list_type openGame, ruyLopez;
openGame.letter = letters[4];
openGame.number = 4;
openGame.next = &ruyLopez;
ruyLopez.letter = letters[5];
ruyLopez.number = 5;
ruyLopez.next = NULL;
}

Resources