链表-----删除结点的前驱结点

#include 
#include 
using namespace std;

typedef struct ListNode
{
	int data;
	struct ListNode *next,*prior;
}linklist;

linklist *head = NULL;

linklist *CreateList(int *arr, int len)
{
	linklist* p, *rear;
	head = (linklist*)malloc(sizeof(linklist));
	head->data = arr[0];
	rear = head;
	int count = 1;
	while (count < len )
	{
		p = (linklist*)malloc(sizeof(linklist));
		p->data = arr[count];
		rear->next = p;
		rear = p;
		count++;
	}
	rear->next = NULL;
	return head;
}

//删除结点元素为x的前驱结点
void DelNode(linklist *head, int x)
{
	linklist *p = head;
	linklist *q = NULL;
	while (p->next->data != x)
	{
		q = p;
		p = p->next;
	}
	q->next = p->next;
	free(p);
}


int main()
{
	int array[] = { 1,2,8,2,3,4,5};
	linklist *head1 = CreateList(array, 7);
	linklist *head2 = head1;
	DelNode(head1, 4);
	while (head2)
	{
		printf("%d ", head->data);
		head = head->next;
	}
	return 0;
}

 

你可能感兴趣的:(链表-----删除结点的前驱结点)