/**
Program Name: Singly Linked List
Description: This Program is for Implemeting Singly Linked List
Author: Tauqirul Haque
*/
struct Linklist
{
int item;
struct Linklist *next;
};
typedef struct Linklist node;
void insertAtBeginning(node **);
void createAppendNode(node **);
void insertInMiddle(node **);
void deleteNode(node **);
void displayNode(node **);
void countNode(node **);
void searchElement(node **);
void reverse(node **);
void main()
{
node *head = NULL;
int choice = 0;
while(choice != 9)
{
printf("\n\t\t\t1. Add Node At The Beginning ");
printf("\n\t\t\t2. Insert Element in the Middle ");
printf("\n\t\t\t3. Append New Node ");
printf("\n\t\t\t4. Delete Node ");
printf("\n\t\t\t5. Display Node ");
printf("\n\t\t\t6. Count Nodes ");
printf("\n\t\t\t7. Search Element ");
printf("\n\t\t\t8. Reverse The List");
printf("\n\t\t\t9. Exit Program ");
printf("\n Enter Your Choice # ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insertAtBeginning(&head);
break;
case 2:
insertInMiddle(&head);
break;
case 3:
createAppendNode(&head);
break;
case 4:
deleteNode(&head);
break;
case 5:
displayNode(&head);
break;
case 6:
countNode(&head);
break;
case 7:
searchElement(&head);
break;
case 8:
reverse(&head);
break;
case 9:
printf("\n\nProgram Terminating ... \n");
exit(0);
}
}
}
void createAppendNode(node **head)
{
int data;
printf("Enter The Data # ");
scanf("%d",&data);
node *temp, *newNode; //temp is just for temporarily getting head address and newNode for inserting new node
temp = *head; //storing head's address
if(*head == NULL) //if no node then;
{
temp = (node *)malloc(sizeof(node)); //allocating memory for temp
temp->item = data;
temp->next = NULL;
*head = temp; //now storing the value of temp in head,, head is the first node
}
else
{
temp = *head;
while(temp->next != NULL) //moving to the last node
{
temp = temp->next;
}
newNode = (node *)malloc(sizeof(node )); //allocating memory for the new node
newNode->item = data; //assigning value to newNode
newNode->next = NULL; //assigning NULL to the address of newNode
temp->next = newNode; //now the old list points to the newly created node;
}
}
void displayNode(node **head)
{
node *temp = *head;
if(temp == NULL) //checking if the list is empty then printing No elements to display
{
printf("\n\nThe List is EMPTY!!!\n\n");
return;
}
printf("\n\nThe Elements of the List are # ");
while(temp != NULL) //running the loop till the end to display every element
{
printf("%4d",temp->item);
temp = temp->next;
}
printf("\n\n");
}
void deleteNode(node **head)
{
node *temp, *previousNode; //previousNode will hold the address of previous node
int data;
int flag = 0; //assuming the element is not in the list
temp = *head;
if(temp == NULL)
{
printf("\n\n List EMPTY!!! .. No Element To Be Deleted \n\n");
return;
}
printf("\n\nEnter The Element To Be Deleted # ");
scanf("%d",&data);
while(temp != NULL)
{
if(temp->item == data)
{
if(temp == *head) //checking for the first node
{
*head = temp->next; //shifting the head address to the next node
}
else
{
previousNode->next = temp->next; //storing the address of previous node
}
flag = 1; //item found
free(temp); //releasing the memory
}
else
{
previousNode = temp;
temp = temp->next;
}
}
if(flag==1)
{
printf("\n\nThe Element %d was Successfully DELETED \n\n",data);
}
else
{
printf("\n\nThe Element %d is NOT in The List \n\n",data);
}
}
void countNode(node **head)
{
int count = 0;
node *temp = *head;
if(temp == NULL)
{
printf("\n\nList Empty ... No Elements\n\n");
}
else
{
while(temp != NULL)
{
count++;
temp = temp->next;
}
printf("\n\nTotal Nodes # %d ",count);
}
}
void insertAtBeginning(node **head)
{
node *temp; //this will be our new node
int data;
printf("\n\nEnter The Element You Want To Insert # ");
scanf("%d",&data);
temp = (node *)malloc(sizeof(node));
if(temp == NULL)
{
printf("\n\n No Memory Available .... OVERFLOW \n\n");
return;
}
temp->item = data ; //assigning the element
temp->next = *head; //now this will point to the head node as this will be the first element
*head = temp; //now making head point to this node
}
void insertInMiddle(node **head)
{
node *newNode;
node *temp = *head;
int data;
int count = 1;
int flag = 0;
int location;
if(temp == NULL)
{
printf("\n\nList Empty ... Cannot Insert \n\n");
return;
}
printf("\n\nEnter The Location Where You Want to Insert New Node # ");
scanf("%d",&location);
printf("\nEnter The Element You Want To Insert # ");
scanf("%d",&data);
newNode = (node *)malloc(sizeof(node));
if(newNode == NULL)
{
printf("\n\nMemory Full...OVERFLOW !!! \n\n");
return;
}
while(temp != NULL)
{
if(count == location)
{
newNode->item = data;
newNode->next = temp->next;
temp->next = newNode;
flag = 1;
}
temp = temp->next;
count++;
}
if(flag == 1)
{
printf("\n\nNode Inserted Successfully ... \n\n");
}
else
{
printf("\n\nSpecified Location Does Not Exits ..\n\n");
}
}
void searchElement(node **head)
{
node *temp = *head;
int item;
int location = 1;
printf("\n\nEnter The Element To Be Searched # ");
scanf("%d",&item);
while(temp != NULL)
{
if(temp->item == item)
{
printf("\n\nSearch Successfull.. Element Found At Location %d \n\n", location);
return;
}
location++;
temp = temp->next;
}
printf("\n\nSearch UNSUCCESSFULL !!!... Element Not In The List\n\n");
}
void reverse(node **head)
{
node *tempHead = *head;
node *reverseNode = NULL;
node *swapper;
while(tempHead) //this loops runs while the value of tempHead is not NULL
{
swapper = reverseNode; //doing the swapping job
reverseNode = tempHead;
tempHead = tempHead->next;
reverseNode->next = swapper;
}
*head = reverseNode; //now making the head point to the reversed list
}
Program Name: Singly Linked List
Description: This Program is for Implemeting Singly Linked List
Author: Tauqirul Haque
*/
struct Linklist
{
int item;
struct Linklist *next;
};
typedef struct Linklist node;
void insertAtBeginning(node **);
void createAppendNode(node **);
void insertInMiddle(node **);
void deleteNode(node **);
void displayNode(node **);
void countNode(node **);
void searchElement(node **);
void reverse(node **);
void main()
{
node *head = NULL;
int choice = 0;
while(choice != 9)
{
printf("\n\t\t\t1. Add Node At The Beginning ");
printf("\n\t\t\t2. Insert Element in the Middle ");
printf("\n\t\t\t3. Append New Node ");
printf("\n\t\t\t4. Delete Node ");
printf("\n\t\t\t5. Display Node ");
printf("\n\t\t\t6. Count Nodes ");
printf("\n\t\t\t7. Search Element ");
printf("\n\t\t\t8. Reverse The List");
printf("\n\t\t\t9. Exit Program ");
printf("\n Enter Your Choice # ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insertAtBeginning(&head);
break;
case 2:
insertInMiddle(&head);
break;
case 3:
createAppendNode(&head);
break;
case 4:
deleteNode(&head);
break;
case 5:
displayNode(&head);
break;
case 6:
countNode(&head);
break;
case 7:
searchElement(&head);
break;
case 8:
reverse(&head);
break;
case 9:
printf("\n\nProgram Terminating ... \n");
exit(0);
}
}
}
void createAppendNode(node **head)
{
int data;
printf("Enter The Data # ");
scanf("%d",&data);
node *temp, *newNode; //temp is just for temporarily getting head address and newNode for inserting new node
temp = *head; //storing head's address
if(*head == NULL) //if no node then;
{
temp = (node *)malloc(sizeof(node)); //allocating memory for temp
temp->item = data;
temp->next = NULL;
*head = temp; //now storing the value of temp in head,, head is the first node
}
else
{
temp = *head;
while(temp->next != NULL) //moving to the last node
{
temp = temp->next;
}
newNode = (node *)malloc(sizeof(node )); //allocating memory for the new node
newNode->item = data; //assigning value to newNode
newNode->next = NULL; //assigning NULL to the address of newNode
temp->next = newNode; //now the old list points to the newly created node;
}
}
void displayNode(node **head)
{
node *temp = *head;
if(temp == NULL) //checking if the list is empty then printing No elements to display
{
printf("\n\nThe List is EMPTY!!!\n\n");
return;
}
printf("\n\nThe Elements of the List are # ");
while(temp != NULL) //running the loop till the end to display every element
{
printf("%4d",temp->item);
temp = temp->next;
}
printf("\n\n");
}
void deleteNode(node **head)
{
node *temp, *previousNode; //previousNode will hold the address of previous node
int data;
int flag = 0; //assuming the element is not in the list
temp = *head;
if(temp == NULL)
{
printf("\n\n List EMPTY!!! .. No Element To Be Deleted \n\n");
return;
}
printf("\n\nEnter The Element To Be Deleted # ");
scanf("%d",&data);
while(temp != NULL)
{
if(temp->item == data)
{
if(temp == *head) //checking for the first node
{
*head = temp->next; //shifting the head address to the next node
}
else
{
previousNode->next = temp->next; //storing the address of previous node
}
flag = 1; //item found
free(temp); //releasing the memory
}
else
{
previousNode = temp;
temp = temp->next;
}
}
if(flag==1)
{
printf("\n\nThe Element %d was Successfully DELETED \n\n",data);
}
else
{
printf("\n\nThe Element %d is NOT in The List \n\n",data);
}
}
void countNode(node **head)
{
int count = 0;
node *temp = *head;
if(temp == NULL)
{
printf("\n\nList Empty ... No Elements\n\n");
}
else
{
while(temp != NULL)
{
count++;
temp = temp->next;
}
printf("\n\nTotal Nodes # %d ",count);
}
}
void insertAtBeginning(node **head)
{
node *temp; //this will be our new node
int data;
printf("\n\nEnter The Element You Want To Insert # ");
scanf("%d",&data);
temp = (node *)malloc(sizeof(node));
if(temp == NULL)
{
printf("\n\n No Memory Available .... OVERFLOW \n\n");
return;
}
temp->item = data ; //assigning the element
temp->next = *head; //now this will point to the head node as this will be the first element
*head = temp; //now making head point to this node
}
void insertInMiddle(node **head)
{
node *newNode;
node *temp = *head;
int data;
int count = 1;
int flag = 0;
int location;
if(temp == NULL)
{
printf("\n\nList Empty ... Cannot Insert \n\n");
return;
}
printf("\n\nEnter The Location Where You Want to Insert New Node # ");
scanf("%d",&location);
printf("\nEnter The Element You Want To Insert # ");
scanf("%d",&data);
newNode = (node *)malloc(sizeof(node));
if(newNode == NULL)
{
printf("\n\nMemory Full...OVERFLOW !!! \n\n");
return;
}
while(temp != NULL)
{
if(count == location)
{
newNode->item = data;
newNode->next = temp->next;
temp->next = newNode;
flag = 1;
}
temp = temp->next;
count++;
}
if(flag == 1)
{
printf("\n\nNode Inserted Successfully ... \n\n");
}
else
{
printf("\n\nSpecified Location Does Not Exits ..\n\n");
}
}
void searchElement(node **head)
{
node *temp = *head;
int item;
int location = 1;
printf("\n\nEnter The Element To Be Searched # ");
scanf("%d",&item);
while(temp != NULL)
{
if(temp->item == item)
{
printf("\n\nSearch Successfull.. Element Found At Location %d \n\n", location);
return;
}
location++;
temp = temp->next;
}
printf("\n\nSearch UNSUCCESSFULL !!!... Element Not In The List\n\n");
}
void reverse(node **head)
{
node *tempHead = *head;
node *reverseNode = NULL;
node *swapper;
while(tempHead) //this loops runs while the value of tempHead is not NULL
{
swapper = reverseNode; //doing the swapping job
reverseNode = tempHead;
tempHead = tempHead->next;
reverseNode->next = swapper;
}
*head = reverseNode; //now making the head point to the reversed list
}
Comments