Sunday, September 6, 2009

I'm upto my ears in Linear Linked Lists

I've finally completed that mammoth (for a beginner like me) 21 case switch case program.. All of these 21 cases perform one or the other function on linear linked lists. It took me about 4 days to write the code and I've got the C source file on my hard disk. And it feels awesome .Six Hundred and Seventy Five lines of pure unadulterated linear linked list based geekiness. You can (though I don't see why you will or why you should) download the source file from here.......(Notepad's the best way to see this file if you aren't interested in much programming)

This is usually the kind of situation (for me and for times such as this one) that warrants the posting of the following kind of photograph:



I'm trying to use SyntaxHighlighter to post the entire program in nice shiny colours but given my limited knowledge in HTML (as of now), I'll leave this post as it is.. Without SyntaxHighlighter, 675 lines of code in Courier would be intolerable..

Update: I finally got the hang of SyntaxHighlighter.. I've posted the program after the jump now (i.e. click "read more" to see the entire post)..


#include <stdio.h>
#include <conio.h>
#include <process.h> // 'exit(0)' requires this
#include <alloc.h>
struct node
{
int data;
struct node *next;
}*start;
void addnodebeginning();
void addnodeatend();
void displayall();
void displaybyvalue();
void displaybypos();
int countall();
void displaymaxvalnode();
void displayminvalnode();
void sumall();
void totalpositive();
void totalnegative();
void modifybyvalue();
void modifybypos();
void deletebypos();
void deletefirstnode();
void deletelast();
void deletebyval();
void deleteall();
void reverse();
void sortlist();
void addnodeatloc();
main()
{
int choice;
clrscr();
while(1)
{
printf("\n 1)Add a node at the beginning");
printf("\n 2)Add a node at the end");
printf("\n 3)Add a node after a particular location");
printf("\n 4)Display all the nodes");
printf("\n 5)Display all the nodes by value");
printf("\n 6)Display a node by position");
printf("\n 7)Delete a node by value");
printf("\n 8)Delete a node by position");
printf("\n 9)Modify a node by value");
printf("\n 10)Modify a node by position");
printf("\n 11)Count all the nodes");
printf("\n 12)Delete all the nodes");
printf("\n 13)Display the node with maximum value");
printf("\n 14)Display the node with minimum value");
printf("\n 15)Find total no. of positive numbers");
printf("\n 16)Find total no. of negative numbers");
printf("\n 17)Delete first node");
printf("\n 18)Delete last node");
printf("\n 19)Sum of all the nodes in the list");
printf("\n 20)Sort all the elements of the list");
printf("\n 21)Reverse the list");
printf("\n 22)Exit");
printf("\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
addnodebeginning();
break;
case 2:
addnodeatend();
break;
case 4:
displayall();
break;
case 5:
displaybyvalue();
break;
case 6:
displaybypos();
break;
case 11:
countall();
break;
case 13:
displaymaxvalnode();
break;
case 14:
displayminvalnode();
break;
case 19:
sumall();
break;
case 15:
totalpositive();
break;
case 16:
totalnegative();
break;
case 9:
modifybyvalue();
break;
case 10:
modifybypos();
break;
case 8:
deletebypos();
break;
case 17:
deletefirstnode();
break;
case 18:
deletelast();
break;
case 7:
deletebyval();
break;
case 12:
deleteall();
break;
case 21:
reverse();
break;
case 20:
sortlist();
break;
case 3:
addnodeatloc();
break;
case 22:
exit(0);
}
}
}

void addnodeatend()
{
int a;
struct node *t,*temp;
t=(struct node *)malloc(sizeof(struct node));
clrscr();
printf("\n Enter element : ");
scanf("%d",&a);
t->data=a;
t->next=NULL;
if(start==NULL)
{
start=t;
}
else
{
temp=start;
}
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=t;
}

void addnodebeginning()
{
int a;
struct node *t;
t=(struct node *)malloc(sizeof(struct node));
clrscr();
printf("\n Enter element : ");
scanf("%d",&a);
t->data=a;
t->next=NULL;
if(start==NULL)
{
start=t;
}
else
{
t->next=start;
start=t;
}
}

void displayall()
{
struct node *temp;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
while(temp!=NULL)
{
printf(" %d",temp->data);
temp=temp->next;
}
}
}

void displaybyvalue()
{
int num,z=0,point=1;
struct node *temp;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
printf("\n Enter number you want to search : ");
scanf("%d",&num);
while(temp!=NULL)
{
if(temp->data==num)
{
printf("\n  %d is present at location %d and at address %d",temp->data,point,temp);
z=z+1;
}
temp=temp->next;
point=point+1;
}
if(z==0)
{
printf("\n The number you were searching for is not present.");
}
}
}

void displaybypos()
{
int pos,z;
struct node *temp;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
printf("\n Enter the position of the node you want to display : ");
scanf("%d",&pos);
for(z=1;z<=pos;z++)
{
if(z==pos)
{
printf("\n The data in the node at position no. %d is %d. ",z,temp->data);
}
temp=temp->next;
}
}
}

int countall()
{
int total=0;
struct node *temp;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
while(temp!=NULL)
{
total=total+1;
temp=temp->next;
}
printf("\n The total number of nodes is %d.",total);
}
return(total);
}

void displaymaxvalnode()
{
int max;
struct node *temp;
temp=start;
clrscr();
max=temp->data;
if(start==NULL)
{
printf("\n No Data!");
}
else
{
while(temp!=NULL)
{
if(temp->data>max)
{
max=temp->data;
}
temp=temp->next;
}
printf("\n The node with the maximum value is %d.",max);
}
}

void displayminvalnode()
{
int min;
struct node *temp;
temp=start;
clrscr();
min=temp->data;
if(start==NULL)
{
printf("\n No Data!");
}
else
{
while(temp!=NULL)
{
if(temp->data<min)
{
min=temp->data;
}
temp=temp->next;
}
printf("\n The node with the minimum value is %d.",min);
}
}

void sumall()
{
int sum=0;
struct node *temp;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
while(temp!=NULL)
{
sum=sum+temp->data;
temp=temp->next;
}
printf("\n The sum of all the nodes is %d.",sum);
}
}

void totalpositive()
{
int s=0;
struct node *temp;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
while(temp!=NULL)
{
if(temp->data>=0)
{
s=s+1;
}
temp=temp->next;
}
printf("\n The total no. of positive numbers is %d.",s);
}
}

void totalnegative()
{
int s=0;
struct node *temp;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
while(temp!=NULL)
{
if(temp->data<0)
{
s=s+1;
}
temp=temp->next;
}
printf("\n The total no. of negative numbers is %d.",s);
}
}

void modifybyvalue()
{
int x,y,z=0;
struct node *temp;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
printf("\n Enter the value of the data in the node you want to overwrite : ");
scanf("%d",&x);
printf("\n Enter the new value of the node : ");
scanf("%d",&y);
while(temp!=NULL)
{
if(temp->data==x)
{
temp->data=y;
z=z+1;
}
temp=temp->next;
}
if(z==0)
{
printf("\n The number you wanted to overwrite (%d) is not present in the list.",x);
}
else
{
printf("\n %d node(s) modified.",z);
}
}
}

void modifybypos()
{
int z=1,pos,num,check;
struct node *temp;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
check=countall();
printf("\n Enter the position of the node you want to modify : ");
scanf("%d",&pos);
if(pos>check)
{
printf("\n The number of nodes is less than the position you entered!");
}
else
{
printf("\n Enter the new value of the node : ");
scanf("%d",&num);
for(z=1;z<=pos;z++)
{
if(z==pos)
{
temp->data=num;
printf("\n Node at position no. %d modified.",z);
}
temp=temp->next;
}
}
}
}

void deletebypos()
{
int p,i=0,z=0;
struct node *temp,*k;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
printf("\n Enter position of the node you want to delete : ");
scanf("%d",&p);
while(i<=p-1)
{
temp=temp->next;
i++;
}
k=temp->next;
temp->next=k->next;
free(k);
z++;
}
if(z!=0)
{
printf("\n The node at position no.%d has been deleted.",p);
}
}

void deletefirstnode()
{
struct node *temp;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
start=temp->next;
temp->next=NULL;
free(temp);
printf("\n First node deleted.");
}
}

void deletelast()
{
struct node *temp;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data to delete!");
}
else
{
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
free(temp->next);
}
}

void deletebyval()
{
int x;
struct node *temp,*k;
temp=start;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
printf("\n Enter the value of the number you want to delete : ");
scanf("%d",&x);
if(start->data==x)
{
deletefirstnode();
}
else
{
while(temp->next!=NULL)
{
k=temp->next;
if(k->data==x)
{
temp->next=k->next;
free(k);
}
temp=temp->next;
}
}
printf("\n Node(s) with the value %d deleted.",x);
}
}

void deleteall()
{
struct node *d;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
while(start!=NULL)
{
d=start;
start=start->next;
free(d);
}
printf("\n All nodes deleted.");
}
}

void reverse()
{
struct node *p1,*p2,*p3;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
if(start->next==NULL)
{
return;
}
p1=start;
p2=p1->next;
p3=p2->next;
p1->next=NULL;
p2->next=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->next;
p2->next=p1;
}
start=p2;
}
}

void sortlist()
{
int n,i,j,x;
struct node *temp,*temp1;
n=countall();
temp=start;
if(start==NULL)
{
printf("\n No Data!");
}
else
{
for(i=0;i<n-1;i++)
{
temp1=temp->next;
for(j=i+1;j<n;j++)
{
if(temp->data>temp1->data)
{
x=temp->data;
temp->data=temp1->data;
temp1->data=x;
}
temp1=temp1->next;
}
temp=temp->next;
}
printf("\n Data sorted in ascending order.");
}
}

void addnodeatloc()
{
struct node *p,*temp;
int k,loc,item;
clrscr();
if(start==NULL)
{
printf("\n No Data!");
}
else
{
printf("\n Enter location after which you want to insert the node : ");
scanf("%d",&loc);
printf("\n Enter element you want to insert : ");
scanf("%d",&item);
temp=start;
for(k=1;k<loc;k++)
{
temp=temp->next;
}
p=(struct node *)malloc(sizeof(struct node));
p->data=item;
p->next=temp->next;
temp->next=p;
}
}

1 comment: