创建一个单链表,输出该单链表,然后删除第i位上元素,若删除成功,输出新的单链表,否则输出“无法删除”
创建一个单链表,输出该单链表,然后删除第i位上元素,若删除成功,输出新的单链表,否则输出“无法删除”
#include<stdio.h> #include<stdlib.h> typedef struct node { node *next; int data; }node; node *create(int ); void display(node *); void display1(node *); int length(node *head); node *search(node *head, int n); node *search1(node *head, int n); node *insert(node *, int , int ); node *deletenode(node *, int ); node *reversed(node *head); int main(int argc, char **argv) { int x; int y; int m,n; node *result; printf("输入要创建多长的链表:\n"); scanf("%d",&x); result=create(x); printf("输入要删除第几个节点:\n"); scanf("%d",&y); // printf("?????????????λ???\n"); // scanf("%d,%d",&m,&n); //printf("????????????\n"); //scanf("%d",&y); //result=insert(result,m,n); // result=search(result,y); //length(result); result=deletenode(result,y); //result=reversed(result); display(result); //display1(result); return 0; } node *create(int n) { node *head,*p,*q; int i; head=(node *)malloc(sizeof(node)); head->next=NULL; if(n>0) { p=head; for(i=0;i<n;i++) { q=(node *)malloc(sizeof(node)); p->next=q; printf("第%d+1节点内容: \n",i); scanf("%d",&q->data); p=q; } p->next=NULL; } return head; } void display(node *head) { node *p; p=head; while(p->next!=NULL) { printf("内容为:%d\n",p->next->data); p=p->next; } } int length(node *head) { int len=0; node *p=head; while(p->next!=NULL) { len++; p=p->next; } printf("???????%d \n",len); } node *search(node *head,int n) { node *p=head; int k=0; while(p->next!=NULL&&k<n) { p=p->next; k++; } if(p!=NULL) return p; else return NULL; } node *search1(node *head,int n) { node *p=head; int k=0; while(p->next!=NULL&&k<n-1) { p=p->next; k++; } if(p!=NULL) return p; else return NULL; } void display1(node *head) { node *p=head; printf("内容为:%d \n",p->data); } node *insert(node *head, int data, int n) { node *p=head; node *q; int k=0; while(p->next!=NULL&&k<n) { p=p->next; k++; } if(p!=NULL) { q=(node *)malloc(sizeof(node)); q->next=p->next; p->next=NULL; p->next=q; q->data=data; return head; } else return NULL; } node *deletenode(node *head, int n) { node *p=head; int m; p=search1(head,n); if(p==NULL||p->next==NULL) return NULL; else { node *q=(node *)malloc(sizeof(node)); q=p->next; p->next=p->next->next; m=q->data; printf("删除节点的内容:%d\n",m); q->next=NULL; free(q); q=NULL; return head; } } node *reversed(node *head) { node *p=head->next; node *q; head->next=NULL;//??????????????? //??????????head->next=NULL??????????????? while(q!=NULL) { q=p->next; p->next=head->next; head->next=p; p=q; } return head; }给你看个吧,里面有链表逆置,查找,删除之类的功能。按你题目要求我把其他功能注释掉了: