单链表的各项操作,添加,删除,遍历,查找,逆序... 下面给出实现代码:#include "stdio.h" #include "stdlib.h" #include "string.h" typedef struct student { char name[20]; struct student *next; }Node; Node *CreateNode(int len) { Node *head=(Node*)malloc(sizeof(Node));// head link if (head==0) { fprintf(stderr,"fail to create head node!"); return 0; } Node *prior=head; char nodeName[20]; for (int i=0;i<len;i++) { printf("input name: "); scanf("%s",nodeName);//input a name Node *currentNode=(Node*)malloc(sizeof(Node)); strcpy(currentNode->name,nodeName); prior->next=currentNode; prior=currentNode; currentNode->next=0; } return head; } void OutputLink(Node *head) { Node *curNode=head->next; printf("--------OutPut-----------/n/n"); while (curNode!=NULL) { printf("%s/n",curNode->name); curNode=curNode->next; } } void InsertNode(Node *pNode,const char* strName) //insert one node after pNode { Node *newNode=new(Node); strcpy(newNode->name,strName); newNode->next=pNode->next; pNode->next=newNode; } Node *SearchNode(Node *head, const char *srcName) { if (head==0) { fprintf(stderr,"fail to search this node!"); return 0; } Node *cur=head->next; while (cur!=NULL) { if (strcmp(cur->name,srcName)==0) { return cur; } cur=cur->next; } return 0; } void pDeleteNode(Node *pNode) // delete pNode->next node { Node *temp; temp=pNode->next; pNode->next=pNode->next->next; delete(temp); } int DeleteNode(Node *head,const char *delName)//delete pNode { Node *prior=head; Node *cur=prior->next; while(cur!=NULL) { if (strcmp(cur->name,delName)==0) break; else { prior=cur; cur=cur->next; } } if (cur->next == 0) // without this node in this link return -1; prior->next=cur->next; delete(cur); return 0; } Node *ReverseLink(Node *head) { Node *p1,*p2,*p3; p1=head; p2=p1->next; while(p2) { p3=p2->next; p2->next=p1; p1=p2; p2=p3; } head->next->next=NULL; head->next=p1; return head; } void main() { // Node *headNode=0; // int length=0; // printf("please input the length of your link:"); // scanf("%d",&length); // headNode=CreateNode(length); // OutputLink(headNode); //**************************** /*Node *headNode=0; int length=0; printf("please input the length of your link:"); scanf("%d",&length); headNode=CreateNode(length); printf("请输入你要在谁后面插入:"); char sName[20]; Node *cur; char insertName[20]; scanf("%s",sName); cur=SearchNode(headNode,sName); printf("请输入要插入人的名字:"); scanf("%s",insertName); InsertNode(cur,insertName); OutputLink(headNode);*/ //******************************** /* Node *headNode=0; int length=0; printf("please input the length of your link:"); scanf("%d",&length); headNode=CreateNode(length); printf("请输入要删除节点的前一个节点:"); char pdName[20]; Node *cur; scanf("%s",pdName); cur=SearchNode(headNode,pdName); DeleteNode(cur); OutputLink(headNode);*/ /*Node *headNode=0; int length=0; printf("please input the length of your link:"); scanf("%d",&length); headNode=CreateNode(length); printf("请输入要删除的节点:"); char pdName[20]; scanf("%s",pdName); DeleteNode(headNode,pdName); OutputLink(headNode);*/ Node *headNode=0; Node *headNode1=0; int length=0; printf("please input the length of your link:"); scanf("%d",&length); headNode=CreateNode(length); headNode1=ReverseLink(headNode); OutputLink(headNode1); }