#include <iostream>
using namespace std;
struct Node
{
Node *next;
int elem;
};
void creatList(Node* &head)
{
head = new Node;
int elem;
cin>>elem;
head->elem = elem;
Node *p = head;
while(cin>>elem&&elem)
{
Node *q = new Node;
q->elem = elem;
p->next = q;
p = p->next;
}
p->next = NULL;
}
void printList(Node* &head)
{
cout<<head->elem<<" ";
Node *p = head->next;
while(p)
{
cout<<p->elem<<" ";
p = p->next;
}
cout<<endl;
}
void removeX(Node* &head,int x)
{
Node *p;
if(head == NULL)
return;
else if(head->elem == x)
{
p = head;
head = head->next;
free(p);
removeX(head, x);
}
else
removeX(head->next, x);
}
Node* getElem(Node* head,int i)
{
int j = 1;
Node *p = head;
if(head==NULL)
return head;
while(p&&j<i)
{
j++;
p = p->next;
}
return p;
}
void insertNode(Node* &head,int i,int data)
{
Node* p = getElem(head, i-1);
Node* q = new Node;
q->elem = data;
q ->next = p ->next;
p ->next = q;
}
void deleteNode(Node* &head,int i)
{
Node* p = getElem(head, i-1);
Node* q = p->next;
p->next = q->next;
free(q);
}
void reverseList(Node* &head)
{
if(head == NULL||head ->next == NULL)
return ;
Node *pre = head;
Node *cur = head->next;
Node *nex = NULL;
while(cur)
{
nex = cur -> next;
cur ->next = pre;
pre = cur;
cur = nex;
}
head->next = NULL;
head = pre;
}
int main()
{
Node *p;
creatList(p);
printList(p);
//removeX(p, 4);
insertNode(p, 2, 100);
printList(p);
cout<<getElem(p, 3)->elem<<endl;
deleteNode(p, 2);
printList(p);
reverseList(p);
printList(p);
return 0;
}