Created
July 14, 2017 15:44
-
-
Save BBloggsbott/4feeb97a00a0f6641d56a825bdb3722f to your computer and use it in GitHub Desktop.
A C++ program to use inputs from a file to perform basic operations on a Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains the input for the program link_list_using_file.cpp. Change the content in this file to change the inputs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<fstream> | |
using namespace std; | |
class single_llist | |
{ | |
struct node | |
{ | |
char info; | |
struct node *next; | |
}*start; | |
public: | |
single_llist() | |
{ | |
start = NULL; | |
} | |
node *create_node(char data) | |
{ | |
struct node *temp, *s; | |
temp = new(struct node); | |
if (temp == NULL) | |
{ | |
cout<<"Memory not allocated "<<endl; | |
return NULL; | |
} | |
else | |
{ | |
temp->info = data; | |
temp->next = NULL; | |
return temp; | |
} | |
} | |
void insert_begin(char data) | |
{ | |
struct node *temp, *p; | |
temp = create_node(data); | |
if (start == NULL) | |
{ | |
start = temp; | |
start->next = NULL; | |
} | |
else | |
{ | |
p = start; | |
start = temp; | |
start->next = p; | |
} | |
cout<<"Element Inserted at beginning"<<endl; | |
} | |
void insert_last(char data) | |
{ | |
struct node *temp, *s; | |
temp = create_node(data); | |
s = start; | |
while (s->next != NULL) | |
s=s->next; | |
temp->next = NULL; | |
s->next = temp; | |
cout<<"Element Inserted at last"<<endl; | |
} | |
void insert_pos(char data) | |
{ | |
int pos, counter = 0; | |
struct node *temp, *s, *ptr; | |
temp = create_node(data); | |
cout<<"Enter the postion at which node to be inserted: "; | |
cin>>pos; | |
int i; | |
s = start; | |
while (s != NULL) | |
{ | |
s = s->next; | |
counter++; | |
} | |
if (pos==1) | |
{ | |
if (start==NULL) | |
{ | |
start=temp; | |
start->next=NULL; | |
} | |
else | |
{ | |
ptr=start; | |
start=temp; | |
start->next=ptr; | |
} | |
} | |
else if (pos > 1 && pos <= counter) | |
{ | |
s = start; | |
for (i = 1; i < pos; i++) | |
{ | |
ptr = s; | |
s = s->next; | |
} | |
ptr->next = temp; | |
temp->next = s; | |
} | |
else | |
{ | |
cout<<"Positon out of range"<<endl; | |
} | |
} | |
void delete_pos() | |
{ | |
int pos, i, counter = 0; | |
if (start == NULL) | |
{ | |
cout<<"List is empty"<<endl; | |
return; | |
} | |
cout<<"Enter the position of data to be deleted: "; | |
cin>>pos; | |
struct node *s, *ptr; | |
s = start; | |
if (pos == 1) | |
{ | |
start = s->next; | |
} | |
else | |
{ | |
while (s != NULL) | |
{ | |
s = s->next; | |
counter++; | |
} | |
if (pos > 0 && pos <= counter) | |
{ | |
s = start; | |
for (i = 1;i < pos;i++) | |
{ | |
ptr = s; | |
s = s->next; | |
} | |
ptr->next = s->next; | |
} | |
else | |
{ | |
cout<<"Position out of range"<<endl; | |
} | |
delete s; | |
cout<<"Element Deleted"<<endl; | |
} | |
} | |
void update(char data) | |
{ | |
int pos, i; | |
if (start == NULL) | |
{ | |
cout<<"List is empty"<<endl; | |
return; | |
} | |
cout<<"Enter the node postion to be updated: "; | |
cin>>pos; | |
struct node *s, *ptr; | |
s = start; | |
if (pos == 1) | |
{ | |
start->info = data; | |
} | |
else | |
{ | |
for (i = 0;i < pos - 1;i++) | |
{ | |
if (s == NULL) | |
{ | |
cout<<"There are less than "<<pos<<" elements"; | |
return; | |
} | |
s = s->next; | |
} | |
s->info = data; | |
} | |
cout<<"Node Updated"<<endl; | |
} | |
void search(char data) | |
{ | |
int pos = 0; | |
bool flag = false; | |
if (start == NULL) | |
{ | |
cout<<"List is empty"<<endl; | |
return; | |
} | |
struct node *s; | |
s = start; | |
while (s != NULL) | |
{ | |
pos++; | |
if (s->info == data) | |
{ | |
flag = true; | |
cout<<"Element "<<data<<" is found at position "<<pos<<endl; | |
} | |
s = s->next; | |
} | |
if (!flag) | |
cout<<"Element "<<data<<" not found in the list"<<endl; | |
} | |
void display() | |
{ | |
struct node *temp; | |
if (start == NULL) | |
{ | |
cout<<"The List is Empty"<<endl; | |
return; | |
} | |
temp = start; | |
cout<<"Elements of list are: "<<endl; | |
while (temp != NULL) | |
{ | |
cout<<temp->info<<"->"; | |
temp = temp->next; | |
} | |
cout<<"NULL"<<endl; | |
} | |
}; | |
int main() | |
{ | |
fstream inputs; | |
inputs.open("inputs.txt",ios::in); | |
char data; | |
int choice, nodes, element, position, i; | |
single_llist sl; | |
do | |
{ | |
cout<<"\nMenu\n1.Insert at Begining\n2.Insert at Last\n3.Insert at position\n4.Delete\n5.Update\n6.Search\n7.Display\n8.Exit\nEnter Choice: "; | |
cin>>choice; | |
switch(choice) | |
{ | |
case 1: if(inputs>>data) | |
sl.insert_begin(data); | |
else | |
cout<<"\nFile Error"; | |
break; | |
case 2: if(inputs>>data) | |
sl.insert_last(data); | |
else | |
cout<<"\nFile Error!!"; | |
break; | |
case 3: if(inputs>>data) | |
sl.insert_pos(data); | |
else | |
cout<<"\nFile Error!!!"; | |
break; | |
case 4: sl.delete_pos(); | |
break; | |
case 5: if(inputs>>data) | |
sl.update(data); | |
else | |
cout<<"\nFile Error!!"; | |
break; | |
case 6: cout<<"\nEnter element to be searched: "; | |
cin>>data; | |
sl.search(data); | |
break; | |
case 7: sl.display(); | |
break; | |
case 8: break; | |
default:cout<<"Wrong choice"<<endl; | |
} | |
}while((choice!=8)&&(!inputs.eof())); | |
cout<<"\n\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment