Create a singly linked list with a dummy node according to the input linear list, and complete the following functions.
(1) insert a new node after the specified position insert(i, value)
(2) delete a specified node delete(i)
(3) reverse the order of all the nodes in the linked list.
e.g. 1,2,3,4,5 change to 5,4,3,2,1
#include<iostream> #include<cstdlib> #define n 10 typedef struct node{ int data; struct node *next; }lnode,*link; static int a[] = {0,1,2,3,4,5,6,7,8,9}; int main(){ link createList(lnode*); void printList(lnode *); void insertList(lnode *,int,int); void deleteList(lnode *,int,int *); void reverseList(lnode *); lnode* List; int e; List = (lnode*)malloc(sizeof(lnode)); List->next = NULL; createList(List); printList(List); insertList(List,1,20); printList(List); deleteList(List,3,&e); std::cout<<e<<std::endl; printList(List); reverseList(List); printList(List); return 0; } //正序建立链表 //void createList(lnode *L){ // lnode *p,*q; // p = L; // for(int i = 0;i<n;i++){ // q = (lnode*)malloc(sizeof(lnode)); // q->data = a[i]; // p->next = q; // p = q; // } // p->next = NULL; //} //反序建立链表 void createList(lnode* L){ lnode *p; for(int i = n-1;i>=0;i--){ p = (lnode*)malloc(sizeof(lnode)); p->data = a[i]; p->next = L->next; L->next = p; } } void insertList(lnode *L,int i,int e){ lnode *p = L,*q; int j = 0; while(p&&j<i-1){ p = p->next; j++; } if(!p){ exit(0); } q = (lnode*)malloc(sizeof(lnode)); q->data = e; q->next = p->next; p->next = q; } void deleteList(lnode *L,int i,int *e){ lnode *p = L,*q; int j = 0; while(p&&j<i-1){ p = p->next; j++; } if(!p) exit(0); q = p->next; p->next = q->next; *e = q->data; free(q); } void reverseList(lnode *L){ lnode *p,*q,*r; p = L->next; r = NULL; while(p){ q = p; p = p->next; q->next = r; r = q; } L->next = q; } void printList(lnode *L){ lnode *p; p = L->next; std::cout<<"The list is:"<<std::endl; while(p){ std::cout<<p->data<<""; p = p->next; } std::cout<<std::endl; }