i build this interface for List in c,i insert to the list pointer of array of struct,
now i want to print all the fields of the struct,how can i do that?
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct element {
void *item;
struct element *next;
} Element;
typedef Element* position;
typedef Element* List;
typedef struct strc
{
int row,col,value;
} Strc;
List initList()
{
Element *ls=malloc(sizeof(Element));
if(ls)
ls->next=NULL;
else
ls=NULL;
return ls;
}
position insertToList(position p, void *item)
{
position newp=malloc(sizeof(Element));
if(newp)
{
newp->next=p->next;
p->next=newp;
newp->item=item;
//p=p->next;
}
else newp=NULL;
return newp;
}
void *retriveFromList(position p) { return p->item; }
position Next(position p) { return (position)p->next; }
int isEmpty(List ls) { return ls->next==NULL; }
void freeList(List ls)
{
position pos=ls->next;
while(ls->next!=NULL)
{
pos=ls->next;
ls->next=pos->next;
free(pos);
}
}
void puts(List *ls)
{
Position p=*ls;
Strc *tmp;
int i;
for(i=0;i<10;i++)
{
tmp=(Strc *)malloc(sizeof(strc));
tmp->row=i;
tmp->col=i+1;
tmp->value=i+2;
p=insertToList(p,tmp);
}
}
void print_list(List ls)
{
position p=ls->next;
while(p!=NULL)
{
printf("%3d\n",*((int*) retriveFromList(p)));
p=Next(p);
}
}
void main()
{
List ls=initList();
puts(&ls);
print(ls);
}
So, my C isn't grand
But, it seems like there are a ton of little typos that are going to eat you as you get this to run. One of the biggest being that puts() is already defined in stdio.h.
First, this thing doesn't compile for me, so working through all the compiler errors got me pretty far. Your puts function also was missing a closing bracket. I renamed it to putv() and changed it to be the following.
void putv(List *ls)
{
position p=*ls;
Strc *tmp;
int i;
for(i=0;i<10;i++)
{
if(tmp=(Strc *)malloc(sizeof(Strc)))
{
tmp->row=i;
tmp->col=i+1;
tmp->value=i+2;
p=insertToList(p,tmp);
}
}
}
The second issue was that your main function was not calling print_list(), but instead calling plain print().
void main()
{
List ls=initList();
putv(&ls);
print_list(ls);
}
This doesn't solve all of your problems
Mostly because I don't think it's printing what you want, but I'm going to leave a little bit for you to figure out.
Related
I tried to search a tree node with a specific value in a binary search tree.
So I used recursion, but it didn't worked well.
I want to know what is wrong with the function.
Someone said I should return the function when I call it in itself.
It actually worked, but I don't understand why. Can someone explain to me how recursion really works and what if the return type is void?
Thank you!
typedef struct node
{
struct node* left;
struct node* right;
int val;
}treeNode;
int searchTree(treeNode *T, int key, treeNode *T1, treeNode** p)
{
if(!T)
{
*p=T1;
return 0;
}
else if(T->val==key)
{
*p=T;
return 1;
}
else if(T->val<key)
{
searchTree(T->right,key,T,p);
}
else
{
searchTree(T->left,key,T,p);
}
return 1;
}
you missed to return the value of the recursive call
if you need to do a recursive call this is not for nothing ;-)
int searchTree(treeNode *T, int key, treeNode *T1, treeNode** p)
{
if(!T)
{
*p=T1;
return 0;
}
else if(T->val==key)
{
*p=T;
return 1;
}
else if(T->val<key)
{
return searchTree(T->right,key,T,p);
}
else
{
return searchTree(T->left,key,T,p);
}
}
Note that because the recursion is terminal the compiler can remove it and produce a loop ...and you can do that by yourself too ;-)
I'm sure I'm making some silly mistake, hope somebody can help me out and clear some of my basic concepts.
Here's my code to create and print a basic BST in C:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct bst
{
int value;
struct bst *left;
struct bst *right;
}T;
T *temp=NULL, *newnode;
T *tree; // had globally declared as NULL previously
T* createnode(int val)
{
newnode=(T*)malloc(sizeof(T));
if(newnode==NULL)
{
printf("Memory not allocated! \n");
}
else
{
newnode->value=val;
newnode->right=NULL;
newnode->left=NULL;
}
return newnode;
}
T * insert_tree(T *tree, int val)
{
if(tree==NULL)
{
newnode=createnode(val);
tree=newnode;
return tree;
}
if (val < tree->value)
{
tree->left=insert_tree(tree->left,val);
}
else if(val > tree->value)
{
tree->right=insert_tree(tree->right, val);
}
return tree;
}
void display_preorder(T *tree) //changed to accept parameter
{
if(tree)
{
printf("%d \n", tree->value);
display_preorder(tree->left);
display_preorder(tree->right);
}
}
int main(void)
{
insert_tree(tree,34);
insert_tree(tree,45);
insert_tree(tree,88);
insert_tree(tree,87);
display_preorder();
return 0;
}
It runs and executes without error, but, the output screen is blank.
Can somebody please point out the errors and mistakes?
Thank You.
The problem is in the insert_tree function.
Because your function gets only a pointer of a tree and not a pointer to a pointer you returned the value.
So, in the main you should place the tree equal to the function.
Your function gets the tree pointer by value and not by reference.
like this:
int main(void)
{
tree = insert_tree(tree, 34);
tree = insert_tree(tree, 45);
tree = insert_tree(tree, 88);
tree = insert_tree(tree, 87);
display_preorder(tree);
getchar();
return 0;
}
There are lots of C examples by using an array to represent the tree.
However, I'm trying to use a tree(Nodes) for the Binary heap.
I was trying to add the child left and then right. In order to insert the elements in Width-first-Search, I've used a Queue. Is this necessary?
My Queue is using a Node* not a int type. Because, if I put a int in the Queue I must search the position of the Node(more time needed). Is this an appropriate way to built a Binary heap?
During the insertion, there are bubbling up and downs. However, since i have a binary heap and also a Queue. Do I have to match the orders in Queue also?
4.Do I need to search for inserting position like a BST? or do I approach to the address directly by using Queue?
I've learned the theory. However, the codes and structures are unfamiliar.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int left(int a,int b)
{
return (int)(a-floor((float)a/(float)b)*b);
}
typedef struct Node
{
int data;
struct Node* leftNext;
struct Node* rightNext;
}Node;
struct queue
{
Node** data;
int top;
int bottom;
int size;
}typedef Queue;
int push(Queue* Q,Node* input)
{
printf("PUSH : %d\n",input);
//한칸은 비울 것임.
if(left((*Q).top-(*Q).bottom,(*Q).size)==(*Q).size-1)
{
printf("큐가 다 찼습니다.\n");
return 0;
}
else
{
//큐의 크기를 넘으면 아래로 이동
if((*Q).top==(*Q).size)
(*Q).top=0;
printf("TOP : %d\n",(*Q).top);
(*Q).data[(*Q).top++]=input;
return 1;
}
}
Node* pop(Queue* Q)
{
if(left((*Q).top-(*Q).bottom,(*Q).size)==0)
printf("큐가 비었습니다.\n");
else
{
if((*Q).bottom==(*Q).size)
(*Q).bottom=0;
printf("BOTTOM : %d\n",(*Q).bottom);
return (*Q).data[(*Q).bottom];
}
}
typedef struct Heap
{
Node* head;
}Heap;
int insert(Node** head,int data,Queue* Q)
{
if((*head)!=NULL)
{
if((*head)->leftNext==NULL)
{
(*head)->leftNext=(Node*)malloc(sizeof(Node));
(*head)->leftNext->data=data;
(*head)->leftNext->leftNext=NULL;
(*head)->leftNext->rightNext=NULL;
push(Q,&(*head)->leftNext);
}
else if((*head)->rightNext==NULL)
{
(*head)->rightNext=(Node*)malloc(sizeof(Node));
(*head)->rightNext->data=data;
(*head)->rightNext->leftNext=NULL;
(*head)->rightNext->rightNext=NULL;
push(Q,&(*head)->rightNext);
(*Q).bottom++;
}
else
{
if(insert((pop(Q)),data,Q)==1)
return 1;
else
return 0;
}
}
else
{
printf("헤드가 비었습니다.");
return 0;
}
return 1;
}
int main()
{
//입력 15 6 12 7 10 17
Heap h;
//큐 초기화
Queue Q;
//큐 크기
int temp=100;
Q.data=(Node**)malloc(sizeof(Node*)*temp);
Q.size=temp;
Q.top=0;
Q.bottom=0;
h.head=(Node*)malloc(sizeof(Node));
h.head->data=15;
h.head->leftNext=NULL;
h.head->rightNext=NULL;
insert(&(h.head),6,&Q);
insert(&(h.head),12,&Q);
insert(&(h.head),7,&Q);
insert(&(h.head),10,&Q);
insert(&(h.head),17,&Q);
return 0;
}
As the title declares, I have a problem concerning an ordered list;
the line where the program crashes is signed in the code below; anyone have idea of where I'm wrong?
Here the job.h header:
typedef struct
{
char stringa[DIM];
unsigned int priority;
} Job;
Here the link declaration (I know it's weird, but my professor want this):
typedef struct QUEUEnode *link;
And here the incriminated module:
#include "ordinate_list.h"
#include <stdlib.h>
#include <stdio.h>
struct QUEUEnode
{
link next;
Job job;
};
void QUEUEinit(Job);
void QUEUEput_ordered(Job a);
void QUEUEfind_link(link b, Job a);
void QUEUElink(link upper, link bottom, Job a);
void print_list_(link, int);
link head=NULL;
int QUEUED_nodes=0;
void add_QUEUEnode(Job a)
{
if(QUEUED_nodes==0)
{
printf("Coda iniziallizata...\n\n");
QUEUEinit(a);
}
else
{
QUEUEput_ordered(a);
}
}
void QUEUEinit(Job a) //OK
{
head = malloc(sizeof(struct QUEUEnode));
head->next=NULL;
head->job=a;
QUEUED_nodes++;
}
void QUEUEput_ordered(Job a)
{
if(head->job.priority<=a.priority)
{
QUEUElink(NULL, head, a);
}
else
{
QUEUEfind_link(head, a);
}
}
void QUEUEfind_link(link b, Job a)
{
if(b->next==NULL)
{
QUEUElink(b, NULL, a);
}
else if((b->job.priority > a.priority) && (b->next->job.priority < a.priority))
{
QUEUElink(b, b->next, a);
}
else QUEUEfind_link(b->next, a);
return;
}
void QUEUElink(link upper, link bottom, Job a)
{
link tmp;
tmp = malloc(sizeof(struct QUEUEnode));
tmp->job=a;
if(upper==NULL)
{
tmp->next=head;
head=tmp;
}
else if(bottom==NULL)
{
bottom->next=tmp;
tmp->next=NULL;
}
else
{
upper->next=tmp; //HERE! DAMN BUG
tmp->next=bottom;
}
QUEUED_nodes++;
}
Job QUEUEget_top()
{
Job a;
link tmp=head;
a=head->job;
head=head->next;
free(tmp);
QUEUED_nodes--;
return a;
}
As you can see from the code, the program crashes at an unusual line. The only thing I haven't tried is to change the priority of the jobs, putting the second before the head and not after; Do it worth the try?
Well, you haven't provided ordinate_list.h, and your posted code doesn't have a main(), so we can't possibly tell you for certain what the problem is. I can, however, tell you for certain what a problem is... in your QUEUElink() function, you have a guaranteed failure right here:
else if(bottom==NULL)
{
bottom->next=tmp;
tmp->next=NULL;
}
If bottom is equal to NULL, I can assure your with absolute, utter certainty that bottom->next=tmp; is impossible, and will segfault.
So here's my code... My understanding is that I'm supposed to create a function "map" that takes a function as an argument. It's not going as planned. Any help would be completely amazing.
Here's a compilable (well not compilable, but scaled down) version of the code:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int main(int argc, const char *argv[])
{
//it should be apparent that I am quite new to C, I have some java experience.
struct linkedList {
int count;
int num;
struct linkedList *next;
};
struct linkedList *head, *tail, *curr;
int count1=0;
int i=0;
int square(int v) {return v=v*v;}
void map(int (*func)(int v), struct linkedList){
struct linkedList2 *head, *tail, *curr;
for(curr=head; curr!=NULL; curr=curr->next){
curr->num=func(curr->num);
printList();
}
}
void start(){
printf("This program will ONLY accept integer input.\n");
head=NULL;
for(i=1;i<=4;i++) {
count1++;
curr=malloc(sizeof(struct linkedList));
curr->count=count1;
printf("Enter a number: ");
scanf("%d", &curr->num);
if(head==NULL) { head=curr; }
else { tail->next=curr; }
tail=curr;
tail->next=NULL;
}
printf("A list has been created.\n");
}
void printList(){
printf("The list now contains these numbers: ");
for(curr=head;curr!=NULL;curr=curr->next){
printf("%d, ", curr->num);
}
printf("\n");
}
start();
printList();
map(square, linkedList);
printList();
system("PAUSE");
return 0;
}
Defining all of those structs and functions inside of main is not how you're supposed to write C. Move the int main(int argc, const char *argv[]) { to right after the definition of printList so that main only contains the actual code for main.
Moreover, your definition of map appears to have an unfinished prototype. Instead of void map(int (*func)(int v), struct linkedList), in which the second parameter is unused, you want void map(int (*func)(int v), struct linkedList* head) (and then get rid of the declaration of head on the next line). Moreover, linkedList2 here presumably should be changed to linkedList. In addition, your attempt to call map in main with map(square, linkedList) is nonsensical; you want to use map(square, head).