简单写了个 你试哈?
#include
#include
struct node
{
int data;
node *next;
};
struct node *head = null;
struct node *tail = null;
void createlist();
void displaynode();
void delnode(int node);
node *searchnode(int node);
int main()
{
createlist();
displaynode();
delnode(1);//删除第三个结点
displaynode();
getchar();
getchar();
return 0;
}
void createlist()
{
int num;
printf("请输入结点数:");
scanf("%d",&num);
for( int i = 0; i < num; i++ )
{
struct node *test = (node *)malloc(sizeof(node));
printf("请输入第%d结点数据:",i+1);
scanf("%d",&test->data);
if ( head == null )
{
head = test;
test->next = null;
tail =test;
}
else
{
tail->next = test;
test->next = null;
tail = test;
}
}
}
void displaynode()
{
node *ptemp = head;
while(ptemp)
{
printf("%d ",ptemp->data);
ptemp = ptemp->next;
}
printf("\n");
}
void delnode(int node)
{
node *cur = searchnode(node-1);
if( cur == head )
head = cur->next;
else
{
node *pre = searchnode(node-2);
pre->next = cur->next;
}
}
node *searchnode(int node)
{
node *phead = head;
int nnum = 0;
while ( phead )
{
if( nnum == node )
break;
phead = phead->next;
nnum++;
}
return phead;
}