Anda di halaman 1dari 16

Mohammed Manzir maqbool

Fa12-bce-040
Data structures and algorithms,
Assign 1
Submitted to: miss shawana jamil

#include <iostream>

#include <conio.h>
using namespace std;

struct linkedlist
{
int data;
struct linkedlist *nextpointer;

};

struct linkedlist *start, *last=NULL;

main()
{
begins: cout << "*****START MENU*****\n";
cout << "please enter \n1 to insert,\n2 to display,\n3 to find the maximum
number,\n4 to find the minimum number,\n5 to search for a number,\n6 to find the
average.\n\n";
int i;
cin >> i;
cout << "\n";
switch(i)
{
case 1:
{
begins1: cout << "please enter \n1 to insert at front,\n2 to insert
at back,\n3 to insert inbetween,\n4 to insert in ascending order.\n\n";

int j;
cin >> j;

switch(j)
{
case 1:
{
cout << "Please insert 6 numbers one by one
\n\n";
for(int i=0;i<=5;i++)
{
if(start==NULL)
{
struct linkedlist
*newnode;
newnode=new(linkedlist);
cin
>>newnode->data;
newnode>nextpointer=NULL;
start=newnode;
last=newnode;
}
else
{
struct linkedlist
*newnode;

newnode=new(linkedlist);
cin
>>newnode->data;
newnode>nextpointer=start;
start=newnode;
}

}
cout << "Your list has been saved, please
choose any option \nother than 1 from the start menu to perform an operation.\nIf
you wish to display, then the list will be displayed in reverse order\nsince you have
inserted the elements at front. \n\n";
goto begins;
break;
}
case 2:
{
cout << "Please insert 6 numbers one by one
\n\n";
for(int i=0;i<=5;i++)
{
struct linkedlist
*current=NULL;
if(start==NULL)
{
struct
linkedlist *newnode;

newnode=new(linkedlist);
cin
>>newnode->data;
newnode->nextpointer=NULL;
start=newnode;
}
else
{
struct
linkedlist *newnode;
newnode=new(linkedlist);
cin
>>newnode->data;
newnode->nextpointer=NULL;
current=start;
while(current->nextpointer!=NULL)
{
current=current->nextpointer;
}
current>nextpointer=newnode;

last=newnode;
}
}
cout << "Your list has been saved, please
choose any option \nother than 1 from the start menu to perform an operation \n\n";
goto begins;
break;
}
case 3:
{

if(start==NULL && last==NULL)


{
cout << "the list doesn't exist,
please enter a list first\n\n";
goto begins1;

}
else
{

int a;
cout << "Please enter the number
after which you want to insert\n\n";
cin >> a;

struct linkedlist *current=NULL;

current=start;
while(current->data!=a && current>nextpointer!=NULL)
{
current=current->nextpointer;
}
struct linkedlist *newnode;
newnode=new(linkedlist);
cout << "please enter the number
you want to save now\n\n";
cin >> newnode->data;
newnode->nextpointer=current>nextpointer;
current->nextpointer=newnode;

}
cout << "Your list has been saved, please choose
any option \nother than 1 from the start menu to perform an operation \n\n";
goto begins;
break;
}
case 4:
{
cout << "Please insert 6 numbers one by one
\n\n";
for(int i=0;i<=5;i++)
{
struct linkedlist *newnode;

newnode=new(linkedlist);
cin >>newnode->data;
newnode>nextpointer=NULL;
//struct linkedlist
*current,*previous=NULL;
if(start==NULL)
{
start=newnode;
}
else
{
if(start>nextpointer==NULL)
{
if(start->data>newnode->data)
{
newnode->nextpointer=start;
start=newnode;
}
else
{

start->nextpointer=newnode;
}
}
else
{
if(newnode->data<start->data)
{
newnode->nextpointer=start;
start=newnode;
}
else
{
struct linkedlist *current,*previous=NULL;
current=start;previous=start;
while(current->nextpointer!=NULL && current->data<newnode->data)
{
previous=current;

current=current->nextpointer;
}
newnode->nextpointer=current;
previous->nextpointer=newnode;
}
}
}

}
cout << "Your list has been saved, please
choose any option \nother than 1 from the start menu to perform an operation \n\n";
goto begins;
break;
}

}
case 2:
{
if(start==NULL)
{
cout << "the list doesn't exist,
please enter a list first\n\n";
goto begins1;

}
else
{
struct linkedlist *current;
current=start;
while(current->nextpointer!=NULL)
{
cout <<
current->data << "\n";
current=current->nextpointer;
}
cout << current->data << "\n";
}

break;
}
case 3:
{
if(start==NULL)
{
cout << "the list doesn't exist,
please enter a list first\n\n";
goto begins1;

}
else
{
struct linkedlist *current;
int max=0;
current=start;
while(current!=NULL)
{
if(current>data>max)
max=current->data;
current=current->nextpointer;
}
cout << "Maximum number in the
list is" << max << "\n";

break;
}
case 4:
{
if(start==NULL)
{
cout << "the list doesn't exist,
please enter a list first\n\n";
goto begins1;

}
else
{
struct linkedlist *current;
int min=10000000;
current=start;
while(current!=NULL)
{
if(current>data<min)
min=current->data;
current=current->nextpointer;
}

cout << "Minimum number in the


list is" << min << "\n";
}

break;
}
case 5:
{
if(start==NULL)
{
cout << "the list doesn't exist,
please enter a list first\n\n";
goto begins1;

}
else
{
struct linkedlist *current;
int a;
cout << "please enter the number
that you want to search.\n";
cin >> a;
current=start;
while(current->data!=a && current>nextpointer!=NULL)
{
current=current->nextpointer;

}
if(current->nextpointer==NULL)
cout << "the
number you want to search does not exist in the list\n";
else
cout << "The
number that you want to search exists\n";
}

break;
}
case 6:
{
if(start==NULL)
{
cout << "the list doesn't exist,
please enter a list first\n\n";
goto begins1;

}
else
{
struct linkedlist *current;
int sum=0;
int count=0;
float av;
current=start;

while(current!=NULL)
{
sum=sum+current->data;
count++;
current=current->nextpointer;
}
av=(float) sum/count;
cout << "average is" << av <<
"\n";
//cout << "sum" << sum << "\n";
//cout << "count" << count <<
"\n";
}

break;
}

cout << "press any key to terminate the program now";


getch();
}

Anda mungkin juga menyukai