Sie sind auf Seite 1von 6

INFORMATION TECHNOLOGY EDUCATION DEPARTMENT

ITSDTRUC
(DATA STRUCTURES AND ALGORITHMS FOR
IT)

PROJECT

TEVES, DARLENE
SALES, AINA DANIELLA
W191
September 17, 2016
#include <iostream>
#include <cstdlib>
using namespace std;

void create();
void insert();
void del();
void search();
void locate();
void sort();
void display();
void note();

struct node
{
int data;
struct node*link;
};
struct node *first=NULL,*last=NULL, *next,*prev,*cur;

void create()
{
If (first==NULL)
{
cur=(struct node*)malloc(sizeof (struct node));
cout << "\nEnter the data: ";
cin >> cur->data;
cur->link=NULL;
first=cur;
last=cur;
}
else
insert();
}

void insert()
{
int pos,c=1;
cur=(struct node*)malloc(sizeof (struct node));
cout << "\nEnter data: ";
cin >> cur->data;
cout <<"\nEnter the position(1-beginning or 2- ending): ";
cin >> pos;

if( (pos==1) && (first!=NULL))


{
cur->link=first;
first=cur;
}
else
{
next=first;
while(c<pos)
{
prev=next;
next=prev->link;
c++;
}
if(prev==NULL)
cout << "\n INVALID POSITION\n";
else
{
cur->link=prev->link;
prev->link=cur;
}
}
}

void del()
{
int a;
cout<<"Enter position to be deleted(1-Front or 2-Back): ";
cin>>a;
if(a==1)
{
node *temp;
temp = (node*)malloc(sizeof(node));
temp = first;
first = temp->link;
free(temp);
}
else if (a==2)
{
node *temp1;
temp1 = (node*)malloc(sizeof(node));
temp1 = first;
node *old_temp;
old_temp = (node*)malloc(sizeof(node));

while(temp1->link!=NULL)
{
old_temp = temp1;
temp1 = temp1->link;
}
old_temp->link = NULL;
free(temp1);
}
else
{
return del();
}
display();
}

void search()
{
int x;
cout<<"Enter number to be searched: ";
cin>>x;

node *cur=first;
int find(0);
int ctr=0;

while(cur!=NULL)
{
if (cur->data==x)
{
find=1;
ctr++;
}
cur=cur->link;
}
if(find==1)
{
cout<<x<<" is in the list and is located at postion "<<ctr<<endl;
}
else
{
cout<<x<<" is not in the list"<<endl;
}
cout<<endl;
display();
}

void sort()
{
node *temp1;
temp1 = (node*)malloc(sizeof(node));
node *temp2;
temp2 = (node*)malloc(sizeof(node));
int temp = 0;

for( temp1 = first ; temp1!=NULL ; temp1 = temp1->link )


{
for( temp2 = temp1->link ; temp2!=NULL ; temp2 = temp2->link )
{
if( temp1->data > temp2->data )
{
temp = temp1->data;
temp1->data = temp2->data;
temp2->data = temp;
}
}
}
display();
}

void display()
{
if(first==NULL)
cout << "\n list is empty";
else
{
cur=first;
while(cur!=NULL)
{
cout << cur->data <<" -->";
cur=cur->link;
}
}
}

int main()
{
int ch;
cout << "\n\n<<<< My Linked List >>>>>>>\n";
do
{
cout << "\n1.Create a node \n2.Delete a node \n3.Search a node
\n4.Sort a node \n5.Display the list \n6.Exit";
cout << "\n\nEnter your choice: ";
cin >> ch;
switch(ch)
{
case 1: create();
display();
break;
case 2: del();
break;
case 3: search();
break;
case 4: sort();
break;
case 5: display();
break;
case 6: exit(0);
break;
default:
cout << "\nINVALID CHOICE\n";
exit(0);
}
} while(1);
return 0;
}

Das könnte Ihnen auch gefallen