Monday, January 26, 2015
C Programming: Inserting at Start and End of a Singly Linked List and Displaying the List
Here is a program which inserts at the start and end of a Singly Linked List, as well as, display the list from start to end.The program consists of 4 functions:
1. insertEnd(int data): void
-This function inserts the data entered by the user at the end of the list
2. insertStart(int data): void
-This function inserts the data entered by the user at the start of the list
3. display(): void
-This function displays all the data entered by the user from start to end
4. main()
-Well, this is our main function.
Here are the codes.
struct node
{
int data; /*the data*/
struct node *next; /*This is a structure within a structure.*/
}*start=NULL,*end=NULL; /*We instantiate var start and end with NULL since there is no node yet*/
void insertEnd(int data)
{
/*new_node is the new node we create each time this function is called.
current is for keeping track of the current node we are dealing with.*/
struct node *new_node,*current;
if(end==NULL) /*if the list is empty*/
{
/*printing something to indicate that there is no node yet. This is useful for debugging.*/
printf("no nodes. creating starting node for %d...\npress any key...",data);
getch();
/*Here, we allocate memory space for the variable new_node of type node.*/
new_node=(struct node *)malloc(sizeof(struct node));
/*We store the passed value of data to the variable data inside new_node.*/
new_node->data=data;
/*We instantiate the variable next inside new_node with NULL because there is nothing next
to this current node*/
new_node->next=NULL;
/*Since this is the first and only node being created, we will store this node to the
variable start and end*/
start=new_node;
end=start;
/*new_node is the current node we are dealing with.*/
current=new_node;
}
else /*If the list is not empty, we will go through the list until the end and add a new node
there.*/
{
current=start; /*let's start from the start*/
while(1) /*an infinite loop*/
{
if(current->next!=NULL) /*if this current node is not the end of the list,*/
{
current=current->next; /*we will look at the next one*/
} /*until there is none next*/
else
{
printf("nodes exist. creating new node for %d...\npress any key...",data);
getch();
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=data;
new_node->next=NULL;
end=new_node; /*this is the last in the list, thus, we store this to var end*/
current->next=new_node; /*this is next to the current node*/
current=new_node; /*then, this will be our current node*/
break; /*we'll break the infinity loop here*/
}
}
}
printf(" done\n"); /*printing something to indicate we are done inserting*/
}
void insertStart(int data)
{
/*We don't need a current variable of type node in this function since, we don't need to
traverse through the list.*/
struct node *new_node;
if(end==NULL) /*This is just the same with the creation of the first node in the insertEnd
function above except that we don't have the variable current*/
{
printf("no nodes. creating starting node for %d...\npress any key...",data);
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=data;
new_node->next=NULL;
start=new_node;
end=start;
}
else
{
printf("nodes exist. creating a new node for %d...\npress any key...",data);
getch();
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=data;
new_node->next=start; /*since we are inserting at the start of the list, this new node will
be ahead of the starting node. In other words, start will be next
to this in the list.*/
start=new_node; /*...and the starting node will, now, be this new node we are creating*/
}
printf("done");
}
void display()
{
struct node *current; /*Here, we are only using the variable current to traverse through the
list*/
current=start; /*Let's start from the start of the list to the user*/
printf("\nList:\n\nStart-->"); /*indicating the start of the list*/
while(current!=NULL) /*while this current node is not the end of the list, (next to the end of
the list is NULL)*/
{
printf("%d-->",current->data); /*printing the data*/
current=current->next; /*let's go to the next node*/
}
printf("End\n\npress any key..."); /*indicating the end of the list to the user*/
getch();
}
main()
{
/*This is easy right here. If you have any question in this part, though, just place a comment
below*/
int data,pos,choice;
while(1)
{
clrscr();
printf("[1] Insert at End\n[2] Insert at Start\n[3] Display Linked List\n[4] Exit\nChoice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
clrscr();
printf("Inserting at End...\n\nEnter data: ");
scanf("%d",&data);
insertEnd(data);
break;
case 2:
clrscr();
printf("Inserting at Start...\n\nEnter data: ");
scanf("%d",&data);
insertStart(data);
break;
case 3:
display();
break;
case 4:
exit(1);
break;
default:
printf("\nPlease choose one operation from the menu.")
break;
}
}
}
You may use this or any part of this program for noncommercial use, i.e. your assignments, projects, teaching programs.
If you have questions/suggestions, quickly place a comment below.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment