I was making a DEQUE program utilizing two separate lists for forward and backward traversal. But what I think I've done is ended up making two separate lists, having same data but only their order reversed, thus wasting memory :'(.
So what I want to know is this how a DEQUE push function is done(push as in pushing from top as in pushing in stack)? If I'm wrong, just tell me where to change the code :confused:
typedef struct list
{
int data;
struct list *forward;
struct list *backward;
}node;
void push_top( node *head, node *tail, int n )
{
node *temp1; // CONTROL VARIABLE FOR ALL FORWARD TRAVERSAL RELATED TASK
node *temp2; // CONTROL VARIABLE FOR ALL BACKWARD TRAVERSAL RELATED TASK
node *temp3; // STORES NEWLY GENERATED ADDRESS FOR FORWARD LIST
node *temp4; // STORES NEWLY GENERATED ADDRESS FOR BACKWARD LIST
temp1 = head;
temp2 = tail;
if(temp1 == NULL) // EMPTY LIST
{
// 1ST TIME CREATING HEAD
temp3 = (node *)malloc(sizeof(node));
temp3 -> data = n;
temp3 -> forward = NULL;
head = temp3;
// 1ST TIME CREATING TAIL
temp4 = (node *)malloc(sizeof(node));
temp4 -> data = n;
temp4 -> backward = NULL;
tail = temp4;
}
else
{
// ADDING NEW ELEMENT & MAKING IT THE NEW HEAD
temp3 = (node *)malloc(sizeof(node));
temp3 -> data = n;
temp3 -> forward = head;
head = temp3;
// GOING TO 2ND LAST ELEMENT OF BACKWARD LIST
while(temp2 -> backward != NULL)
temp2 = temp2 -> backward;
// ADDING NEW ELEMENT & MAKING IT POINT TO NULL
temp4 = (node *)malloc(sizeof(node));
temp4 -> data = n;
temp4 -> backward = NULL;
// POINTING THE 2ND LAST ELEMENT TO THE NEW LAST ELEMENT
temp2 -> backward = temp4;
}
}