#include <iostream>
#include <algorithm>
#include <stdio.h>
struct node{
int data;
struct node* link;
};
// This is the head pointer
struct node * head;
// This function inserts element at the first position of linked list
void insert_first(int element){
struct node * temp = (node*)malloc(sizeof(struct node));
temp->data = element;
temp->link = head;
head = temp;
}
// This function can be used to insert element at any position of list
// You need to pass the position and the value of data
void insert_element(int n, int value){
struct node * temp = (node*)malloc(sizeof(struct node));
temp->data = value;
struct node* prev = head;
for(int i = 0; i<n-2; i++){
prev = prev->link;
}
temp->link = prev->link;
prev->link = temp;
}
// This function inserts element at the last position of linked list
void insert_last(int element){
struct node * temp = (node*)malloc(sizeof(struct node));
temp->data = element;
temp->link = NULL;
if(head==NULL){
head = temp;
return;
}
struct node * temp1 = head;
while(temp1->link!=NULL){
temp1 = temp1->link;
}
temp1->link = temp;
}
// This function can be used to reverse the linked list iteratively
void reverse(){
struct node * temp, *temp1, *current;
temp = NULL;
temp1 = head;
current = head->link;
while(temp1 != NULL){
temp1->link = temp;
temp = temp1;
temp1 = current;
current = current->link;
}
head = temp1;
}
// This function can be used to reverse the linked list using recursion
void reverse_recursive(struct node * p){
if(p->link == NULL){
head = p;
return;
}
reverse_recursive(p->link);
struct node * q = p->link;
q->link = p;
p->link = NULL;
}
// This function deletes the element at the n'th position
// You need to pass the position of the element you need to delete from the list
void delete_element(int n){
struct node * temp = head, *temp1;
for(int i = 0; i<n-2; i++){
temp = temp->link;
std::cout<<" i "<<i<<" data :"<<temp->data<<std::endl;
}
temp1 = temp->link;
temp->link = temp1->link;
delete(temp1);
}
// This function can be used to print the linked list using recursion
void print_recursive(node * p){
if(p==NULL)
return;
printf("%d\t", p->data);
print_recursive(p->link);
}
// This function prints the linked list in opposite order using recursion
void print_recursive_reverse(node *p){
if(p == NULL){
return;
}
print_recursive_reverse(p->link);
printf("%d\t",p->data);
}
// This function can be used to print the linked list iteratively
void print(){
node * p = head;
while(p!=NULL){
printf("%d\t",p->data);
p=p->link;
}
}
int main()
{
head = NULL;
printf("Enter the no. of nodes or elements you want to make linked list of. ");
int n;
scanf("%d",&n);
int element = 0;
for(int i = 0; i<n; i++){
printf("Enter the element\n");
scanf("%d",&element);
insert_last(element);
std::cout<<"Element inserted\n\n";
}
print();
// Call any above defined function here
print();
}
#include <algorithm>
#include <stdio.h>
struct node{
int data;
struct node* link;
};
// This is the head pointer
struct node * head;
// This function inserts element at the first position of linked list
void insert_first(int element){
struct node * temp = (node*)malloc(sizeof(struct node));
temp->data = element;
temp->link = head;
head = temp;
}
// This function can be used to insert element at any position of list
// You need to pass the position and the value of data
void insert_element(int n, int value){
struct node * temp = (node*)malloc(sizeof(struct node));
temp->data = value;
struct node* prev = head;
for(int i = 0; i<n-2; i++){
prev = prev->link;
}
temp->link = prev->link;
prev->link = temp;
}
// This function inserts element at the last position of linked list
void insert_last(int element){
struct node * temp = (node*)malloc(sizeof(struct node));
temp->data = element;
temp->link = NULL;
if(head==NULL){
head = temp;
return;
}
struct node * temp1 = head;
while(temp1->link!=NULL){
temp1 = temp1->link;
}
temp1->link = temp;
}
// This function can be used to reverse the linked list iteratively
void reverse(){
struct node * temp, *temp1, *current;
temp = NULL;
temp1 = head;
current = head->link;
while(temp1 != NULL){
temp1->link = temp;
temp = temp1;
temp1 = current;
current = current->link;
}
head = temp1;
}
// This function can be used to reverse the linked list using recursion
void reverse_recursive(struct node * p){
if(p->link == NULL){
head = p;
return;
}
reverse_recursive(p->link);
struct node * q = p->link;
q->link = p;
p->link = NULL;
}
// This function deletes the element at the n'th position
// You need to pass the position of the element you need to delete from the list
void delete_element(int n){
struct node * temp = head, *temp1;
for(int i = 0; i<n-2; i++){
temp = temp->link;
std::cout<<" i "<<i<<" data :"<<temp->data<<std::endl;
}
temp1 = temp->link;
temp->link = temp1->link;
delete(temp1);
}
// This function can be used to print the linked list using recursion
void print_recursive(node * p){
if(p==NULL)
return;
printf("%d\t", p->data);
print_recursive(p->link);
}
// This function prints the linked list in opposite order using recursion
void print_recursive_reverse(node *p){
if(p == NULL){
return;
}
print_recursive_reverse(p->link);
printf("%d\t",p->data);
}
// This function can be used to print the linked list iteratively
void print(){
node * p = head;
while(p!=NULL){
printf("%d\t",p->data);
p=p->link;
}
}
int main()
{
head = NULL;
printf("Enter the no. of nodes or elements you want to make linked list of. ");
int n;
scanf("%d",&n);
int element = 0;
for(int i = 0; i<n; i++){
printf("Enter the element\n");
scanf("%d",&element);
insert_last(element);
std::cout<<"Element inserted\n\n";
}
print();
// Call any above defined function here
print();
}
0 comments:
Post a Comment