|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include<stdlib.h>
- typedef char datatype;
- typedef struct node {
- datatype data;
- struct node *next;
- } linklist;
- linklist *creatlistafter() {
- linklist *p,*s,*head;
- datatype d;
- head=malloc(sizeof(linklist));
- head->next=NULL;
- p=head;
- printf("输入元素值(回车结束):");
- rewind(stdin);
- scanf("%c",&d);
- while(d!='\n') {
- s=malloc(sizeof(linklist));
- s->data=d;
- p->next=s;
- p=s;
- scanf("%c",&d);
- }
- if(p!=NULL) p->next=NULL;
- return head;
- }
- linklist *spe(linklist *L1,linklist *L2,int i) {
- int j;
- linklist *p;
- p=L1;
- L2=malloc(sizeof(linklist));//L2申请空间
- L2->next=NULL;
- if(i<0) printf("下标不能为负数\n");
- for(j=0; j<i; j++) {
- if(p->next==NULL) {
- printf("链表长度不够!\n");
- break;
- }
- p=p->next;
- }
- if(p->next!=NULL) {
- L2->next=p->next;
- p->next=NULL;
- }
- }
- void print(head)
- linklist *head;
- {
- linklist *p;
- p=head->next;
- while(p!=NULL) {
- printf("%c ",p->data);
- p=p->next;
- }
- printf("\n");
- }
- int main() {
- linklist *L1,*L2;
- int i;
- datatype x;
- L1=creatlistafter();
- print(L1);
- printf("输入要分开的位置:");
- scanf("%d",&i);
- spe(L1,L2,i);
- printf("L1:");
- print(L1);
- printf("L2:");
- print(L2);
- }
复制代码
测试了一下发现L2申请写在主函数没问题,但写在别的函数就不行,想请教各位是什么原因? |
|