#include<iostream>
using namespace std;
struct node
{
int data;
struct node *next;
}*head,*temp;
int main()
{
int t,s;
cin>>t;
s=t/2;
head=0;
while(t--)
{
struct node* new_node =(struct node*) malloc(sizeof(struct node));
cin>>new_node->data;
new_node->next=0;
if(head==0)
{
head=new_node;
temp=new_node;
}
else
{
temp->next=new_node;
temp=new_node;
}
}
struct node *prevnode,*currnode,*nextnode;
prevnode=0;
currnode=nextnode=head;
while(nextnode!=0)
{
nextnode=nextnode->next;
currnode->next=prevnode;
prevnode=currnode;
currnode=nextnode;
}
head=prevnode;
temp=head;
cout<<"Linked list : ";
while(temp!=0)
{
cout<<"-->"<<temp->data;
temp=temp->next;
}
cout<<endl;
temp=head;
while(s--)
{
temp=temp->next;
}
cout<<"The middle element is ["<<temp->data<<"]";
return 0;
}
using namespace std;
struct node
{
int data;
struct node *next;
}*head,*temp;
int main()
{
int t,s;
cin>>t;
s=t/2;
head=0;
while(t--)
{
struct node* new_node =(struct node*) malloc(sizeof(struct node));
cin>>new_node->data;
new_node->next=0;
if(head==0)
{
head=new_node;
temp=new_node;
}
else
{
temp->next=new_node;
temp=new_node;
}
}
struct node *prevnode,*currnode,*nextnode;
prevnode=0;
currnode=nextnode=head;
while(nextnode!=0)
{
nextnode=nextnode->next;
currnode->next=prevnode;
prevnode=currnode;
currnode=nextnode;
}
head=prevnode;
temp=head;
cout<<"Linked list : ";
while(temp!=0)
{
cout<<"-->"<<temp->data;
temp=temp->next;
}
cout<<endl;
temp=head;
while(s--)
{
temp=temp->next;
}
cout<<"The middle element is ["<<temp->data<<"]";
return 0;
}
No comments:
Post a Comment