I have created a program which should read the number of vertices to be there in the graph and I have a problem in creating the links between the vertices using link list. I get it to create the vertices and it creates links between some of the nodes but for some reason, it crashes when I try to enter certain vertex as the link.
for example
if I give the number of vertices as 4
and enter inputs as 1 2 3 4 then next the vertex to link I enter 1
and the vertices to link with as -1 2 3
It crashes after I enter 3, why?
when I enter 2 as the vertex I could link with any vertices.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct BFS_node
{
int data,vis;
struct BFS_node *linknodes;
};
typedef struct BFS_node node;
node *head,*N=NULL;
int num;
void create_node(node **Node)
{
if(*Node==NULL)
{
*Node=(node*)malloc(sizeof(node));
head=*Node;
}
else
*Node=*Node+1;
printf("%d enter the value \n",*Node);
scanf("%d",&(*Node)->data);
(*Node)->linknodes=(node *)malloc(num*sizeof(node));
printf(" %d \n",(*Node)->linknodes);
}
node * search_node(int num,node *head2)
{
while(head2)
{
if(head2->data==num)
return head2;
head2++;
}
}
void linking()
{
node *Dnode,**Lnode;
int num,i=0;
char Snum[10];
printf("enter the number you want to link ");
scanf("%d",&num);
Dnode=search_node(num,head);
printf("%d",Dnode);
while(getchar() != '\n' && getchar()!=EOF);
Lnode=Dnode->linknodes;
printf("enter the linked numbers");
while(fgets(Snum,sizeof(Snum),stdin))
{
if(sscanf(Snum,"%d",&num)!=1)
break;
*Lnode=search_node(num,head);
printf("%d %d",Lnode,*Lnode);
Lnode++;
}
}
BFStraversal()
{
int num,i=0;
node *queue[10],*link;
printf("enter the starting number");
scanf("%d",&num);
queue[i]=search_node(num,head);
link=queue[i]->linknodes;
printf("%d",link->data);
queue[i]->vis=1;
while(queue[i]!=NULL)
{
int j=1;
link=queue[i]->linknodes;
printf("%d",queue[i]->linknodes->data);
while(link->data !=NULL)
{
if(link->vis!=1)
{
queue[i+j]=link;
link->vis=1;
j++;
}
link++;
}
printf("%d",queue[i]->data);
i++;
}
}
int main()
{
printf("enter the number of vertices \n ");
scanf("%d",&num);
for(int i=0;i<num;i++)
create_node(&N);
for(int i=0;i<num;i++)
linking();
BFStraversal();
return 0;
}
here is the complete working code .feels so relieved ,this was starting to annoy.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct BFS_node
{
int data,vis;
struct BFS_node *linknodes;
};
typedef struct BFS_node node;
node *head,*N=NULL;
int num,i=0,count=-1;
int queue[20];
void create_node(node **Node)
{
if(*Node==NULL)
{
*Node=(node*)malloc(num*sizeof(node));
head=*Node;
}
else
*Node=*Node+1;
printf(" enter the value \n");
scanf("%d",&(*Node)->data);
(*Node)->vis=1;
(*Node)->linknodes=(node *)malloc(num*sizeof(node));
printf("address- %d link- %d \n",*Node,(*Node)->linknodes);
}
node * search_node(int num,node *head2)
{
while(head2)
{
if(head2->data==num)
return head2;
head2++;
}
}
void linking()
{
node *Dnode,**Lnode;
int num2,i=0;
char Snum[10];
printf("enter the number you want to link ");
scanf("%d",&num2);
Dnode=search_node(num2,head);
while(getchar() != '\n' && getchar()!=EOF);
Lnode=Dnode->linknodes;
printf("enter the linked numbers\n");
while(fgets(Snum,sizeof(Snum),stdin))
{
node *head2=head;
if(sscanf(Snum,"%d",&num2)!=1)
break;
*Lnode=search_node(num2,head);
printf(" linknode-%d link-%d\n ",Lnode,*Lnode);
Lnode++;
}
*Lnode=0;
}
BFStraversal(int vertex)
{
node *node,*head2=head,**links;
node=search_node(vertex,head);
links=node->linknodes;
node->vis=0;
printf(" %d ",vertex);
while(*links!=0)
{
if((*links)->vis)
{
queue[i]=(*links)->data;
(*links)->vis=0;
i++;
}
links++;
}
if(++count<i)
BFStraversal(queue[count]);
}
int main()
{
printf("enter the number of vertices \n ");
scanf("%d",&num);
for(int i=0;i<num;i++)
create_node(&N);
for(int i=0;i<num;i++)
linking();
printf("enter the source vertex");
int source;
scanf("%d",&source);
BFStraversal(source);
return 0;
}
enter code here
Related
I'm doing self-study on C Programming, and I have been recommended the following C Program by my colleagues to study further, where you can enter the Name and Age and it displays and uses Insert, Delete, Display, and Exit menu options.
I'm trying to convert it to my current study stream logic scenario where I need to enter the Name, Exercise Mark 1 (up to 3), and then it computes the Average and gets displayed while employing the Insert, Delete, Display, Update (updating the scores only, not the names), Delete and Exit.
Any guidance please on how to learn this code and understand the logic, and apply it to the 2nd scenario will be much appreciated.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50
//using parallel arrays as fields in the list
typedef struct list{
char name[MAX][31];
int age[MAX];
int last;
}LIST;
LIST L;//L structure is global
void save();
void retrieve();
void makenull();
void insert(char n[31],int a);
void del(char n[31]);
void display();
int locate(char n[31]);
int isfull();
int isempty();
int menu();
int main(){
char nm[31];
int ag;
makenull();
retrieve();
while(1){
switch(menu()){
case 1: system("cls");printf("Insert Mode\n");
printf("Input Name: ");scanf("%s",nm);
printf("Input Age: ");scanf("%d",&ag);insert(nm,ag);break;
case 2: system("cls");printf("Delete Mode\n");
printf("Input Name: ");scanf("%s",nm);del(nm);break;
case 3: display();break;
case 4: save();exit(0);
default: printf("\n1-4 lang!\n");system("pause");
}
}
return 0;
}
void makenull(){
L.last = -1;
}
void insert(char n[31],int a){
if (isfull()){
printf("List is full.\n");
system("pause");
}
else {
L.last++;
strcpy(L.name[L.last],n);
L.age[L.last]=a;
}
}
void del(char n[31]){
int p;
if (isempty()){
printf("List is empty.\n");
system("pause");
}
else {
p=locate(n);
if (p==-1){
printf("Not found.\n");
system("pause");
}
else{
for(int i = p;i<L.last;i++){
strcpy(L.name[i],L.name[i+1]);
L.age[i]=L.age[i+1];
}
L.last--;
printf("Successful delete operation.\n");
system("pause");
}
}
}
void display(){
int i;
system("cls");
printf(" Name Age \n");
for(i=0;i<=L.last;i++)
printf("%d.) %s %d\n",i+1,L.name[i],L.age[i]);
system("pause");
}
int locate(char n[31]){
int i;
for (i=0;i<=L.last;i++)
if(strcmp(L.name[i],n)==0)
return i;
return -1;
}
int isfull(){
if (L.last==MAX-1)
return 1;
else
return 0;
}
int isempty(){
return(L.last==-1);
}
int menu(){
int op;
system("cls");
printf("MENU\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("\nSelect(1-4): ");
scanf("%d",&op);
return(op);
}
void save(){
FILE *fp;
int i;
fp=fopen("Practice4.dbf","w+");
if (fp==NULL){
printf("File Error.\n");
system("pause");
}
else{
for (i=0;i<=L.last;i++)
fprintf(fp,"%s %d\n",L.name[i],L.age[i]);
}
fclose(fp);
}
void retrieve(){
FILE *fp;
char n[31];
int i,a;
fp=fopen("Practice4.dbf","r+");
if (fp==NULL){
printf("File Error.\n");
system("pause");
}
else {
while(!feof(fp)){
fscanf(fp,"%s %d\n",n,&a);
insert(n,a);
}
}
fclose(fp);
}
Your code isn't properly formatted and there are no comments. I can't give you a direct answer with some code in it, but summing up all my comments (and of course I deleted them), this is what I've to say:
Consider this scenario-
if your .dbf has more than MAX 50 elements, then your while (!feof(fp)) inside retrieve() will keep calling insert() and insert() will keep executing its if () { } block.
You should put something like while (!feof(fp) && L.last < MAX) to prevent that situation and you'll need to further modify your code in insert(). Another thing is, this code doesn't have any update() function and scores variable. You'll need to add scores in your struct as well as there must be scores fields in your .dbf.
Now, for a moment let's say everything else is good to go in your code, then you should follow these following steps:
Declare variables
char nameInput[31];
float ex_marks[3], sum = 0, avr = 0;
in main().
Add another case 5 in your switch () block inside main() and translate and convert the following pseudocode into C code:
Read name in nameInput
locate()
if found then
3.a for i = 0 to 2
Read marks in ex_marks[i]
sum = sum + ex_marks[i]
3.b Calculate avr = sum / 3
3.c Display name and avr
else
Display name is not in the list.
exit
Also read about why is while(!feof()) always wrong?
I cant figure out what I am doing wrong here / how do I fix it. It is giving me an indication of expecting an expression before struct on line 90. Can somebody help me out with this problem?
I want to print a structure that is in a function, but because of the 2 arguments behind it I cant find a proper way to code it. The other functions do work fine, but when I add this one it all goes wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct geboorteDatum
{
int geboortedag;
int geboortemaand;
int geboortejaar;
};
struct Persoon
{
struct geboorteDatum;
char naam[20];
};
void printPersonen(struct Persoon *pt, int persoonCount);
void scanPersonen(int persoonCount);
int startMenu(void);
int main()
{
startMenu();
}
void printPersonen(struct Persoon *pt, int persoonCount)
{
printf("Gegevens persoon 1 : ");
printf("\n%s",pt[persoonCount].naam);
printf("\n%d",pt[persoonCount].geboortedag);
printf("\n%d",pt[persoonCount].geboortemaand);
printf("\n%d",pt[persoonCount].geboortejaar);
printf("\n");
}
void scanPersonen(int persoonCount)
{
struct Persoon pt[100];
printf("Voer Gegevens persoon . : \n");
scanf("%s", pt[persoonCount].naam);
scanf("%d", &pt[persoonCount].geboortedag);
scanf("%d", &pt[persoonCount].geboortemaand);
scanf("%d", &pt[persoonCount].geboortejaar);
}
int startMenu(void)
{
int keuze = 0;
int persoonCount = 0;
int * p1 = &persoonCount;
do
{
printf("MENU \n");
printf("1 : Voer een persoon in \n");
printf("2 : Toon alle geboortedatums \n");
printf("3 : Toon de jongste persoon \n");
printf("4 : Toon verjaardagen in een maand \n");
printf("0 : Einde \n\n");
printf("Maak een keus : ");
scanf("%d", &keuze);
printf("\n");
if(keuze == 1)
{
persoonCount++;
scanPersonen(persoonCount);
}
else if(keuze == 2)
{
printPersonen(struct Persoon *pt, int persoonCount);
}
else if(keuze == 3)
{
printf("jo3");
}
else if(keuze == 4)
{
printf("%d",persoonCount);
}
else
{
printf("Deze keus is niet mogelijk, kies opnieuw \n\n");
return startMenu();
}
} while (keuze > 0);
return persoonCount;
}
Well, in:
struct Persoon
{
struct geboorteDatum;
char naam[20];
};
that struct geboorteDatum specifies a type, but no member name! You can fix that by adding one struct geboorteDatum d;
Then in your print and scan functions replace
printf("\n%d",pt[persoonCount].geboortedag);
with
printf("\n%d",pt[persoonCount].d.geboortedag);
But even better would be to change the struct geboorteDatum to
struct Date {
int day;
int month;
int year;
}
as a birthday is just a date. you do not need a special type to represent birthdays.
Then struct Persoon becomes:
struct Person {
struct Data birthday;
char name[20];
};
And the print statements become
printf("\n%d", pt[persoonCount].birthday.day);
(And translate all code to English for easier sharing. I could understand the dutch part, but most people cannot.)
Cough
well....
printPersonen(struct Persoon *pt, int persoonCount);
means you are declaring a prototype, and in that context is incorrect. May be it should be:
printPersonen(pt, persoonCount);
So I am starting to use C, and I have some problems almost always with memory allocation. Basically I am getting stuck when I am trying to copy a struct to another struct.
I will put you the code here:
The headers file is:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
//declaration constants
#define MAX_NAME 15+1
#define SEATS_PERCENTAGE 0.95
#define IN_TIME 60
#define OUT_TIME 120
//type declarations
typedef enum {FORBIDDEN, ALLOWED_WITH_COMPANION, ALLOWED} tFairgroundRideAccess;
typedef struct{
tFairgroundRideAccess lessThan100;
tFairgroundRideAccess between100_120;
tFairgroundRideAccess between120_140;
tFairgroundRideAccess greaterThan140;
} tFairgroundRideHeightRequirement;
typedef struct {
char name[MAX_NAME];
tFairgroundRideHeightRequirement accessHeight;
int durationTrip;
int numPersonsTrip;
int peopleInQueue;
} tFairgroundRide;
tFairgroundRide myFairgroundRide;
//define function headers
void readFairgroundRide(tFairgroundRide *fRide);
void writeFairgroundRide(tFairgroundRide fRide);
void copyFairgroundRide(tFairgroundRide fRideSrc, tFairgroundRide *fRideDst);
int waitingTime (tFairgroundRide fRide, int people);
bool accessWithoutCompanion (tFairgroundRide fRide, int height);
void selectFairgroundRide (tFairgroundRide fRide1, tFairgroundRide fRide2, int people1, int people2, int height2);
The file with the functions is the following one (what I want to check why I am not allocating properly in memory the new copy in the function copyFairgroundRide):
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "fairgroundRide.h"
void readFairgroundRide(tFairgroundRide *fRide){
printf("NAME >> \n");
scanf("%s", fRide->name);
getchar();
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.lessThan100);
getchar();
printf("ACCESS HEIGHT, BETWEEN100_120 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.between100_120);
getchar();
printf("ACCESS HEIGHT, BETWEEN120_140 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.between120_140);
getchar();
printf("ACCESS HEIGHT, GREATERTHAN140 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED) >> \n");
scanf("%d", &fRide->accessHeight.greaterThan140);
getchar();
printf("TRIP DURATION >> \n");
scanf("%d", &fRide->durationTrip);
getchar();
printf("NUMBER OF PERSONS ON A TRIP >> \n");
scanf("%d", &fRide->numPersonsTrip);
getchar();
}
void writeFairgroundRide(tFairgroundRide fRide){
printf("NAME: %s\n", fRide.name);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.lessThan100);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.between100_120);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.between120_140);
printf("ACCESS HEIGHT, LESSTHAN100 (0-FORBIDDEN, 1-ALLOWED_WITH_COMPANION, 2-ALLOWED): %d\n",fRide.accessHeight.greaterThan140);
printf("TRIP DURATION: %d\n", fRide.durationTrip);
printf("NUMBER OF PERSONS ON A TRIP: %d\n", fRide.numPersonsTrip);
}
void copyFairgroundRide(tFairgroundRide fRideSrc, tFairgroundRide *fRideDst){
strcpy(fRideDst->name,fRideSrc.name);
fRideDst->accessHeight.lessThan100 = fRideSrc.accessHeight.lessThan100;
fRideDst->accessHeight.between100_120 = fRideSrc.accessHeight.between100_120;
fRideDst->accessHeight.between120_140 = fRideSrc.accessHeight.between120_140;
fRideDst->accessHeight.greaterThan140 = fRideSrc.accessHeight.greaterThan140;
fRideDst->durationTrip = fRideSrc.durationTrip;
fRideDst->numPersonsTrip = fRideSrc.numPersonsTrip;
fRideDst->peopleInQueue = fRideSrc.peopleInQueue;
}
int waitingTime (tFairgroundRide fRide, int people){
int result;
result = ((IN_TIME+OUT_TIME+(fRide.durationTrip*60)) * (fRide.numPersonsTrip * SEATS_PERCENTAGE) * people);
return result;
}
bool accessWithoutCompanion (tFairgroundRide fRide, int height){
if (height < 100 && fRide.accessHeight.lessThan100 == 2){
return true;
}
if(height >= 100 && height<120 && fRide.accessHeight.between100_120 == 2){
return true;
}
if(height >= 120 && height<=140 && fRide.accessHeight.between120_140 == 2){
return true;
}
if (height > 140 && fRide.accessHeight.greaterThan140 == 2){
return true;
}else{
return false;
}
}
void selectFairgroundRide (tFairgroundRide fRide1, tFairgroundRide fRide2, int people1, int people2, int height2){
if((accessWithoutCompanion(fRide1,height2) == true) && (waitingTime(fRide1,people1)<=waitingTime(fRide2,people2))){
copyFairgroundRide(fRide1,&myFairgroundRide);
}
if((accessWithoutCompanion(fRide1,height2) == true) && (accessWithoutCompanion(fRide2,height2)==false)){
copyFairgroundRide(fRide1,&myFairgroundRide);
}
if((accessWithoutCompanion(fRide1,height2) == false) && (accessWithoutCompanion(fRide2,height2))){
copyFairgroundRide(fRide2,&myFairgroundRide);
}
}
And the main code is:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "fairgroundRide.h"
int main(){
tFairgroundRide myFairgroundRide;
tFairgroundRide fairgroundRide1;
tFairgroundRide fairgroundRide2;
int height1,people1,people2;
printf("ENTER DATA FOR FIRST FAIRGROUND RIDE >>\n");
readFairgroundRide(&fairgroundRide1);
printf("ENTER THE PEOPLE IN THE QUEUE OF FAIRGROUND RIDE 1 >> \n");
scanf("%d", &people1);
getchar();
printf("ENTER DATA FOR SECOND FAIRGROUND RIDE >>\n");
readFairgroundRide(&fairgroundRide2);
printf("ENTER THE PEOPLE IN THE QUEUE OF FAIRGROUND RIDE 2 >> \n");
scanf("%d", &people2);
getchar();
printf("ENTER THE HEIGHT >> \n");
scanf("%d", &height1);
getchar();
selectFairgroundRide(fairgroundRide1,fairgroundRide2,people1,people2,height1);
printf("RESULTS:\n");
writeFairgroundRide(myFairgroundRide);
getchar();
return 0;
}
So when I am running the program I can get all the info of both inputs, faigroundRide1 and fairgroundRide2, but if I copy it to another struct called myFairgroundRide I start getting weird characters and numbers, and I know that is due to memory allocation but I cannot find why or where is the issue. If you need further explanations about the code or what is my doubt just let me know and I will try to re-do it in another way.
Thanks in advance,
Jorge.
You have two myFairgroundRide. One in global scope, and one as local variable in main. In selectFairgroundRide you copy to the global one, but later in main you print the local one.
And by the way, copyFairgroundRide can be simplified:
void copyFairgroundRide(tFairgroundRide fRideSrc, tFairgroundRide *fRideDst)
{
*fRideDst = fRideSrc;
}
My idea is to make this program to first queue the number (start from 1001)until 10 loop.But at the same time every twice loop. i want it to delete the first number insert.Then it continue insert number after the last number insert. For example. (0) insert 1001,(1) insert 1002,(2) delete 1001,(3) insert 1003,(4) insert 1004,(5)delete 1002. This is what i imagine and the desire output. But now. When it delete it reset to the initial number.
#include <stdio.h>
#define MAX 10 /* The maximum size of the queue */
#include <stdlib.h>
void insert(int queue[], int *rear, int value)
{
if(*rear < MAX-1)
{
*rear= *rear +1;
queue[*rear] = value;
printf("\n%d queue at counter 1",value);
}
else
{
printf("\nThe queue is full can not insert a value\n");
exit(0);
}
}
void delete(int queue[], int *front, int rear, int * value)
{
if(*front == rear)
{
printf("\nThe queue is empty can not delete a value\n");
exit(0);
}
*front = *front + 1;
*value = queue[*front];
printf("\n%d left counter 1",*value);
}
int main()
{
int queue[MAX];
int iCounter,front,rear,loop=0,a,b,c;
front=rear=-1;
a=1001;
do{
printf("\n------------------------------");
printf("\n\tWelcome!!\n");
printf("\n------------------------------");
printf("\nPress which counter you prefer");
printf("\n1-Pay bill");
printf("\n2-Check up");
printf("\n3-QnA");
printf("\n------------------------------\n");
scanf(" %d",&iCounter);
loop++;
switch(iCounter)
{
case 1:
insert(queue,&rear,a);
a++;
break;
default:
printf("\nError input!");
break;
}
while(loop==2)
{
delete(queue,&front,rear,&a);
loop=0;
}
}while(rear<MAX-1);
return 0;
}
Your delete function take the address of a and writes the deleted value into it. So it will get set to the value you deleted.
You could just remove
*value = queue[*front];
and change
printf("\n%d left counter 1",*value);
to
printf("\n%d left counter 1",queue[*front]);
and also remove the whole parameter and then I think it does what you want it to do.
your delete() function pass a as pass by reference that's why you did not get what you want
delete(queue,&front,rear,&a);
For your design you should pass a as pass by value
delete(queue,&front,rear,a);
void delete(int queue[], int *front, int rear, int value)
I have a small doubly-linked list application. I want to add elements inside the list and then display the list normally. At the output i get my inserted elements allright, but after them i get a bunch of strange numbers( such as .... 28482 -20048 2817 ...... )
I believe it's a problem of space allocation.
Any help is appreciated, thanks in advance!
# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>
typedef struct elem {
int number;
struct elem * urm;
struct elem * prec;
}nod;
nod *prim=NULL,*ultim=NULL, *local=NULL, *p=NULL;
void insert_element(int numb){
nod *local=(nod *)malloc(sizeof(nod));
local->number = numb;
if (prim==NULL){
prim=local;
ultim=local;
}
else{
ultim->urm = local;
local->prec = ultim;
ultim=local;
}
}
void load_data()
{
int i,n;
nod *c = (nod *)malloc(sizeof(nod));
printf("\n cate elemente va avea lista?");
scanf("%d", &n);
printf("avem %d elemente", n);
for(i=1;i<=n;i++){
printf("\n number: ");
scanf("%d", &c->number);
insert_element(c->number);
}
}
void list_left_to_right()
{
nod *p = (nod*) malloc(sizeof(nod));
p=prim;
while(p){
printf("%d ", p->number);
p=p->urm;
}
printf("\n");
}
int main()
{
int op;
do{
printf("\n1.Enter some data\n");
printf("2.Display left - > right the data\n");
printf("0.Exit\n");
printf("choice : ");
scanf("%d",&op);
switch(op){
case 1: load_data(); break;
case 2: list_left_to_right(); break;
case 0: break;}
}
while (op!=0);
return 0;
}
(1) You have a memory leak in list_left_to_right():
nod *p = (nod*) malloc(sizeof(nod));
p=prim;
This leaks the block returned by malloc().
(2)
void insert_element(int numb) {
nod *local=(nod *)malloc(sizeof(nod));
local->number = numb;
// TODO: set local->urm and local->prec to NULL
if (prim==NULL) {
prim=local;
ultim=local;
OK, so the first time insert_element() is called, the new element is both the head and the tail.
Bug: You need to set the urm and prec fields to NULL. They have undefined values initially.
}
else {
ultim->urm = local;
local->prec = ultim;
ultim=local;
}
}
After that, the subsequent elements are inserted as a new tail (ultim).
Bug: But again you need to make sure that local->urm is set to NULL.