Below is the source that just for those APDS begginer on how
to evaluate the Add node at Front, Back and Delete node at Front, Back. Please
do email me if you have any suggestion
on it and thank for your comment and guide.
Update and complete correct source code at download page, seach for APDS. c ya.
#include
#include
#include
struct node {
int data; struct node *next; };
typedef struct node LIST;
void initial_node(LIST *f_node)
{
f_node->data=1; f_node->next=NULL;
}
LIST *Add_Front(LIST *h_ADD_FRONT,int num) // notice i return pointer type same
as argument passing
{ // u don't have any shortcut 2 create new node, so the following statement
must have compare 2 Add_Back
LIST *add_front_node; // i make new node using pointer here 2 tally with passing
argument
add_front_node=new(LIST);
add_front_node->data=num;
add_front_node->next=h_ADD_FRONT; // i point the added node 2 the node that
head pointer h point h_ADD_FRONT=add_front_node; // i move the(update) the head
pointer 2 the new added node
return h_ADD_FRONT;
}
LIST *Add_Back(LIST *n_ADD_BACK,int num)
{
n_ADD_BACK->next=new(LIST); // *** i create new node by node's next pointer
n_ADD_BACK=n_ADD_BACK->next; // then i update it here
n_ADD_BACK->data=num; // ***
n_ADD_BACK->next=NULL; // *** i assign the next pointer 2 NULL again
return n_ADD_BACK;
}
LIST *Del_Front(LIST *h_DEL_FRONT)
{
LIST *del_front_node=h_DEL_FRONT; // i declare another pointer point 2 first
node
h_DEL_FRONT=h_DEL_FRONT->next; // i update the head pointer that i need 2 return
n update
delete(del_front_node); // delete goes here
return h_DEL_FRONT; // i return n update the head pointer
}
LIST *Del_Back(LIST *n_DEL_BACK) // this Delete the Back node still have problem
althought i exspect don't have, pls check and email me if u can solve it, thx
{
delete(n_DEL_BACK); // the easiest of delete function EVER
return NULL;
}
void Display_List(LIST *display_ptr)
{
while(display_ptr!=NULL)
{
printf("%d node -> ",display_ptr->data);
display_ptr=display_ptr->next;
}
}
void main()
{
char option;
int node_num=2; // declaration of new node pointer in the list, i use list with
head pointer h
LIST *n_ptr, *h, *temp; // make the new node
n_ptr=new(LIST); // u must use
initial_node(n_ptr); // initial node value
h=temp=n_ptr; // initial head pointer 2 first node
while(node_num<=3)
{
n_ptr->next=new(LIST); // *** i create new node at the node next pointer
n_ptr=n_ptr->next; // *** then i update the pointer 2 the next node
n_ptr->data=node_num; // ***
n_ptr->next=NULL; // *** i assign the next pointer 2 NULL again
node_num++;
}
printf("\n\tStarting like this...\n\n\t");
while(temp!=NULL)
{ printf("%d node -> ",temp->data);
temp=temp->next;
}
printf("NULL");
again:
printf("\n\n\tSelect option: ");
printf("\n\t\tA.) Add node(front)\n\t\tB.) Add node(back)"); printf("\n\t\tC.)
Delete node(front)\n\t\tD.) Delete node(back)"); printf("\n\t\tL.) Display the
list pattern."); printf("\n\t\tE.) Exit program");
printf("\n\n\tOption: ");
scanf(" %c",&option);
if(toupper(option)=='E')
{
printf("\n\n\tBye...");
return;
}
else
{
switch(toupper(option))
{
case 'A':
h=Add_Front(h,node_num); // here have 2 return the h(head pointer) 2 update
pointer
node_num++;
printf("\n\tNew Node added at front!");
system("pause");
system("cls");
fflush(stdin);
goto again;
case 'B':
n_ptr=Add_Back(n_ptr,node_num); // this is just similar 2 *** part, but just
xtra argument
node_num++;
printf("\n\tNew Node added at back!");
system("pause");
system("cls");
fflush(stdin);
goto again;
case 'C':
if(h!=NULL) // just in case that empty list occur
{
h=Del_Front(h);
node_num--;
printf("\n\tNode deleted at front!");
}
else
{
node_num=1;
printf("\a\n\tOop! Empty list! Please check!");
}
system("pause");
system("cls");
fflush(stdin);
goto again;
case 'D': // the hardest way of updating the pointer 2 last node, because last
node oredi deleted n there is no more n_ptr 4 last node
if(h!=NULL)
{
temp=h; // h is the header node
if(temp->next!=NULL)
{
while(temp->next!=NULL)
{
n_ptr=temp; // point to the second last node
temp=temp->next;
}
temp=Del_Back(temp); // pass last node to delete
}
else
h=Del_Back(temp); // if there is only one node(last node)
node_num--;
printf("\n\tNode deleted at back!");
}
else
{
node_num=1;
printf("\a\n\tOop! Empty list! Please check!");
}
system("pause");
system("cls");
fflush(stdin);
goto again;
case 'L':
printf("\n\t");
Display_List(h);
printf("NULL");
system("pause");
system("cls");
fflush(stdin);
goto again;
default:
printf("\a\n\tOop! Not a valid option! Please check!");
system("pause");
system("cls");
fflush(stdin); goto again;
}
}
}