I have writen a simple code in C. What I am trying to accomplish is given a string [similar to html] I am creating node pointer object. May be its called serialization or something but not sure.
For example, I have string abccbccacca in which a tag has two immediate children ccacc and bccb going further recursively deep bccb tag b also has two children so two = c and each c are siblings together.
But this lines are in my thinking not doing what they suppose to.
struct node *temp=malloc(sizeof(struct node));
struct node *t=n+i;
t=temp;
parse(arr,l,r,(struct node *)n+i);
What I am doing above is simply allocate n+i-th sibling and passing recursively.
I also tried like this
(n+i)=malloc(sizeof(struct node));
parse(arr,l,r,(struct node *)n+i);
But the above does not compile and give error that lvalue should be ...
So I believe algorithm makes sense but I may need pointer to pointer for allocation if I want this to output something
struct node *no=n->ch;
struct node *no1=no->ch;
printf("\n%c\n",no1[2].c);
This is full code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <string.h>
#include <malloc.h>
struct node {
char c;
struct node *ch;
};
int chcmp(char a,char b)
{
if(a==b)
return 1;
return 0;
}
void parse(char *arr,int l,int r,struct node *n)
{
if(arr[l]==arr[r])
{
n->c=arr[r];
//printf(" &%c %c ",*(arr+r),*(arr+l));
n->ch=malloc(sizeof(struct node));
r--;
l++;
printf("%c",n->c);
parse(arr,l,r,n->ch);
}
//printf("\n");
int i=0;
while(r>l)
{
if(arr[r]!=arr[l])
{
//printf("[%c %c] %d\n",arr[r],arr[l],l);
r--;
}
else
{
struct node *temp=malloc(sizeof(struct node));
struct node *t=n+i;
t=temp;
parse(arr,l,r,(struct node *)n+i);
l=r+1;
r=strlen(arr);
i++;
}
}
}
int main()
{
struct node *n=malloc(sizeof(struct node));
char *arr="abccbccacca";
parse(arr,0,strlen(arr)-1,n);
struct node *no=n->ch;
struct node *no1=no->ch;
printf("\n%c\n",no1[2].c);
}
Related
I'm trying to implement a linked list traversal, however my code is not working and I don't udnerstand why I've reassigned my pointer but it doesn't stick?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void inserter(Node *n1, int num){
if(n1==NULL){
Node *temp =(Node*)malloc(sizeof(Node));
temp->data=num;
temp->next=NULL;
n1=temp;
}
else{
Node *trav=n1;
while(trav!=NULL){
trav=trav->next;
}
}
}
int main(int argc, char **argv)
{
Node *l1 = NULL;
inserter(l1,l2,5);
inserter(l1,l2,11);
//It goes back into the NULL bracket despite me assigning it a node?
}
Here is the accompanying struct
typedef anode {
struct Node *next;
int data;
}Node;
Thank you for any help!I'm still new to all of this.
You're passing inserter() a pointer and an int. If you modify that int, no modifications would be visible from the calling function (I think you know that already). Same goes for the pointer: Node * is just a numeric type that holds an address, which has been passed by value.
If you want to modify where n1 points to, you'd need to dereference it. But because you don't want to change the contents of the memory that n1 points to, but change the value of n1 itself, your function would need to be passed a Node **.
Here's a quick example:
void inserter(Node **n, int num) {
if (*n == NULL) {
*n = malloc(...);
....;
}
....
}
That way you would change the actual value of n globally instead of just locally.
I have a char pointer in the structure to store names.When I inserted the values and printed the last value of name is getting printed for all nodes.
typedef struct tests{
int id;
char *p;
struct tests *next;
}test;'
'void add(test **root,int id,char *name){
test *newnode=(test* )malloc(sizeof(test));
newnode->id=id;
newnode->p=name;
newnode->next=NULL;
test *curr;
curr=(*root);
if((*root)==NULL){
(*root)=newnode;
}
else{
while(curr->next!=NULL){
curr=curr->next;
}
curr->next=newnode;
}
}
Most likely what you intend to achieve is something along these lines:
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
typedef struct test_s {
int id;
char *p;
struct test_s *next;
} test_t;
test_t** add(test_t **root, int id, char const *name){
size_t len = strlen(name);
test_t *newnode=(test_t*)malloc(sizeof(test_t)+len+1); // 1 goes for \0 terminator
newnode->id=id;
strcpy(newnode->p=(void*)(newnode+1), name);
newnode->next=NULL;
test_t **tail = root;
while (*tail) {
tail = &(*tail)->next;
}
*tail = newnode;
return &(newnode->next);
}
Problem with your code is, you never copied your string assigning just a single pointer instead.
Take a look at pointers mechanics once again, additionally I would advise you to check string.h reference.
I am trying to read characters into a linked list (I made this simple test code just to try to read in the characters) for some reason I cannot get it to read in a character value.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char name[50];
struct node *next;
}*head;
void add(char AddName);
int main()
{
head = NULL;
char TempName[50];
printf("What Name");
scanf(" %s", TempName);
add(TempName);
printf("%s",head->name);
return 0;
}
void add(char AddName)
{
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
strcpy(temp->name,AddName);
head = temp;
head->next = NULL;
}
I understand this is not how a linked list works I just made this to try to get to be able to run a single character name into the struct and print it back out. (I should be able to enter in the name Bob and it prints bob)
I think your function parameter definition is wrong. Try this:
void add(char *AddName)
{
....
}
I am new to C. I have implemented a simple stack with some structs and what not. I have posted the entire code below. The problem section is commented.
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *next;
} Node;
typedef struct Stack{
Node *top;
int size;
} Stack;
/* Function Prototypes */
void push(Stack *sPtr, int data);
int pop(Stack *sPtr);
void create(Stack *sPtr);
int main(void)
{
static Stack first;
create(&first);
push(&first,4);
push(&first,3);
push(&first,2);
printf("%d\n",pop(&first));
printf("%d\n",pop(&first));
printf("%d\n",pop(&first));
exit(1);
}
void push(Stack *sPtr, int data)
{
struct Node newNode;
newNode.data = data;
newNode.next = sPtr->top;
sPtr->top = &newNode;
sPtr->size++;
printf("%d\n",sPtr->top->data);
}
int pop(Stack *sPtr)
{
struct Node *returnNode = sPtr->top;
struct Node *topNode = sPtr->top;
if(sPtr->size != 0){
sPtr->top = topNode->next; /* =============PROBLEM?=============== */
return returnNode->data;
}
else{
printf("Error: Stack is Empty!\n");
return -1;
}
}
void create(Stack *sPtr)
{
sPtr->size = 0;
sPtr->top = NULL;
}
The output of this code is
4
3
2
2
8103136
680997
So obviously, it is pulling off the top node, and then printing the addresses of the next two nodes, instead of their data.
But why is it doing this? As far as I know (which is little) preforming this operation
sPtr->top = topNode->next;
should tell the program to make top now point to to topNode.next. But instead, it seems to be returning the address. What's going on here?
In your push() function, you're creating a new struct Node and adding it to your stack. However, the node is a local variable within the scope of push()--allocated on the stack (not your stack, the call stack), and will be gone when push() returns.
What you want to do is create the node on the heap, which means it will still be there after push() returns.
Since you're coding in C, you'll want to do something like:
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
Since you're now dealing with heap-allocated memory, you'll need to make sure that at some point it gets freed (somewhere) using free().
You're also not decrementing size as Jonathan has pointed out.
One trouble is that pop() never decrements size, so size is really 'number of elements ever pushed onto stack', not 'the number of elements in the current stack'.
int pop(Stack *sPtr)
{
struct Node *returnNode = sPtr->top;
struct Node *topNode = sPtr->top;
if (sPtr->size != 0)
{
sPtr->top = topNode->next;
sPtr->size--;
return returnNode->data;
}
else
{
fprintf(stderr, "Error: Stack is Empty!\n");
return -1;
}
}
Another trouble, as pointed out by unluddite in his answer is that you are not pushing data correctly. You need both fixes to be safe. There might still be other problems (such as not freeing memory correctly — or at all), but these two will get you a long way.
I am trying to create a linked list in my program and I am not able to allocate memory to the structure pointer using malloc(). How do I allocate memory to variables in GCC? The sample program is given below. How to make it work in gcc?
#include<stdio.h>
#include <alloc.h>
struct node
{
int data;
struct node * link;
};
void insert (struct node *p, int d)
{
struct node *temp;
temp = malloc(sizeof(struct node));
temp->data=d;
temp->link=NULL;
if(p==NULL)
{
p=temp;
}
else{
while(p->link!=NULL)
p=p->link;
p->link=temp;
}
}
void disp(struct node *p)
{
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->link;
}
}
int main()
{
struct node *p;
p=NULL;
insert(p,7);
insert(p,9);
disp(p);
}
The error I'm encountering is:
Line 18: error: alloc.h: No such file or directory
In function 'insert':
Line 13: warning: incompatible implicit declaration of built-in function 'malloc'
malloc is in <stdlib.h>. Include that.
Reading the man page for that function would have given you that information. It's not compiler-dependent.
malloc is declared in <stdlib.h>, so that's what you want to #include.
The definition of malloc is in the stdlib.h file:
#include <stdlib.h>
instead of alloc.h.
Like the others say: your error occurs because you have to include stdlib.h instead of alloc.h
To get your list printed, you have to modify p in insert. Currently, you're passing NULL every time you call insert. Change your code that way (pass a pointer-to-pointer to insert):
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node * link;
};
/* note **p instead of *p */
void insert (struct node **p, int d)
{
struct node *temp;
temp = malloc(sizeof(struct node));
temp->data=d;
temp->link=NULL;
if(*p==NULL)
{
*p=temp;
}
else{
while((*p)->link!=NULL)
*p=(*p)->link;
(*p)->link=temp;
}
}
void disp(struct node *p)
{
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->link;
}
}
int main()
{
struct node *p;
p=NULL;
insert(&p,7);
insert(&p,9);
disp(p);
}
and it will print
7
9