#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}NODE, *PNODE;
PNODE create();
//void traverse(PNODE);
void traverse2(PNODE);
int yue(PNODE);
//int delete_p(PNODE,int);
void delete_p2(PNODE, int);
void main()
{
PNODE p = NULL;
p = create();
delete_p2(p, 3);
}
PNODE create()
{
PNODE pHead = (PNODE)malloc(sizeof(NODE));
PNODE pTail = pHead;
int len, i;
printf("请输入人数:");
scanf("%d", &len);
pTail->next = NULL;
for(i = 1; i <= len; i++)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
pNew->data = i;
pTail->next = pNew;
pNew->next = NULL;
pTail = pNew;
}
pTail->next = pHead->next;
return pHead;
}
void traverse2(PNODE ps)
{
PNODE p = ps;
p = p->next;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void delete_p2(PNODE pHead, int pos)
{
int num = 0;
int val, i;
PNODE p = pHead;
PNODE pp;
while(p->next != p) //这里
{
for(i = 1; i<pos; i++)
{
p = p->next;
}
pp = p->next;
val = p->next->data;
p->next = p->next->next;
free(pp);
printf("%d\n", val);
// return val;
}
//这里
printf("%d\n", p->data);
free(p);
}
|