I have a circular queue using DLL which is using globally declared pointers.
the problem now is that it is not being initialize properly or being cleared thus my code is not working as expected.
in my code you will be asked how many nodes do you wish to enter "i made 2 default for now", after that you may then add or delete the node. add only works now since delete is still on progress.
when you add the nodes "2 nodes by default" the program will only record the latest input so if i were to input 1 and 2, only 2 will be displayed. I know that this maybe because of my *first and *last
variables not being initialize properly.
how should i really work with global pointers?
also im really new to project file programming and not a fan of pointers or linked list at all.
any help would be really appreciated and explanations.
main.c
void main(){
int ch, number, numdum = 0;
n *new, *ptr, *prev, *first, *last;
first = NULL;
last = NULL;
clrscr();
printf("Enter number of nodes: ");
scanf("%d", &number);
while (1){
printf("\n\n[1] Insert Node \n[2] Delete Node\n[3] Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch){
case 1:
if(number != numdum){
add_node(&first,&last);
/* display(&first, &last); */
numdum++;
}else{
printf("Number of nodes achieved!");
}
break;
case 2:
delete_node();
display();
break;
case 3:
exit(0);
default:
printf("\ninvalid choice");
}
}
}
circ.h
#ifndef CIRC_H
#define CIRC_H
struct node{
int val;
struct node *next;
struct node *prev;
};
typedef struct node n;
extern int number;
int add_node(n **first, n **last);
int delete_node();
int display();
n* create_node(int data);
#endif
add.c
int add_node(n **first, n **last){
int info,i, number = 2;
n *new, *ptr, *t1, *t2, *prev;
t1 = *first;
t2 = *last;
printf("\nEnter the value you would like to add: ");
scanf("%d", &info);
new = create_node(info);
printf("%d",t1);
if (t1 == NULL){
printf("\nfirst\n");
t1 = t2 = new;
t1->next = t2->next = NULL;
t1->prev = t2->prev = NULL;
printf("\n\n%d\n",t1);
}else{
printf("\nsecond\n");
t2->next = new;
new->prev = t2;
t2 = new;
t2->next = t1;
t1->prev = t2;
}
if (t1 == t2 && t1 == NULL)
printf("\nlist is empty no elements to print");
else{
printf("\n%d number of nodes are there", number);
for (ptr = t1, i = 0;i < number;i++,ptr = ptr->next){
printf("\n --- %d", ptr->val);
}
}
return 1;
}
create.c
n* create_node(int info){
n *new;
new = (n *)malloc(sizeof(n));
new->val = info;
new->next = NULL;
new->prev = NULL;
return new;
}