//Any Problems can be discussed in comments.
//operations: insertNode, deleteNode, printList, reverseList
#include<stdio.h>
typedef struct node *nodePointer;
struct node
{
int data;
struct node *link;
}node;
int length=0;
void insertNode(int,nodePointer);
void print(nodePointer);
void deleteNode(int,nodePointer);
nodePointer reverseList(nodePointer);
main()
{
nodePointer root;
root=malloc(sizeof(node));
root->link=NULL;
insertNode(34,root);
insertNode(56,root);
insertNode(45,root);
print(root);
printf("Length is %d\n",length);
root=reverseList(root);
print(root);
printf("Length is %d\n",length);
deleteNode(56,root);
deleteNode(34,root);
insertNode(53,root);
print(root);
printf("Length is %d\n",length);
}
void insertNode(int data,nodePointer root)
{
int i;
if(length>0)
{
while(root->link)
root=root->link;
}
nodePointer temp;
temp=malloc(sizeof(node));
temp->data=data;
root->link=temp;
temp->link=NULL;
length++;
}
void print(nodePointer root)
{
printf("Root");
while(root->link)
{
printf("->%d",root->link->data);
root=root->link;
}
printf("\n");
}
void deleteNode(int data,nodePointer root)
{
nodePointer temp;
temp=root;
while(root->link)
{
if(root->link->data==data)
{
temp=root->link->link;
free(root->link);
root->link=temp;
length--;
return;
}
else
{
temp=root;
root=root->link;
}
}
}
nodePointer reverseList(nodePointer root)
{
nodePointer middle,trail,temp;
temp=root;
middle=NULL;
root=root->link;
while(root)
{
trail=middle;
middle=root;
root=root->link;
middle->link=trail;
}
temp->link=middle;
return temp;
}
No comments:
Post a Comment