#include <stdio.h>
#include <stdlib.h>
typedef struct node //自定义链表结构体
{
int data;
struct node *next;
}LNode ;
LNode *create(int n)
{
LNode *head,*p,*q;
int i,m;
p=(LNode*)malloc(sizeof(LNode));
p->next=NULL;
head=p; //建立头结点
printf("Please enter %d integers:\n",n);
for(i=1;i<=n;i++)
{
q=(LNode*)malloc(sizeof(LNode));
scanf("%d",&m); //输入链表的data值
q->data=m;q->next=NULL;
p->next=q;p=q;
}
return head;
}
int DeleteLink(LNode* head,int i)
{
LNode *p,*q;
int j=0;
p=head;
while((p!=NULL)&&(j<i-1))
{
p=p->next;
j++;
}
if(p==NULL) return 0;
q=p->next;
p->next=q->next;
free(q);
return 0;
}
Print(LNode *head,int m) //输出函数
{
LNode *p;
p=head;
printf ("\n");
while (p->next!=NULL)
{
p=p->next;
printf ("%d ",p->data);
}
return 0;
}
int main ()
{
int n,i;
LNode *head,*p;
p=(LNode*)malloc(sizeof(LNode));
p->next=NULL;
head=p;
LNode *create(int);
int DeleteLink(LNode *, int);
int Print(LNode *,int);
printf("How many nodes do you want in this linklist?\n");
scanf("%d",&n);
create(n);
printf("Which node do you want to delete?\n");
scanf("%d",&i);
DeleteLink(head,i);
Print(head,n-1);
return 0;
}
虽然不太清楚你想要啥,给你看看我的写法,希望对你有帮助:
struct CListNode{