编程实现单链表和双链表删除节点

#include <tchar.h>
#include <iostream>
using namespace std;

typedef struct student{
	int data;
	struct student * next; 
} node;

node* create()
{
	node *head, *p, *s;
	int x, cycle = 1;
	head = (node *)malloc(sizeof(node));
	p = head;
	while(cycle)
	{
		cout << "please input the data: ";
		cin >> x;
		cout << endl;
		if( x != 0)
		{
			s = (node *)malloc(sizeof(node));
			s->data = x;
			p->next = s;
			p = s;
		}
		else
		{
			cycle = 0;
		}
		
	}
	p->next = NULL;
	head = head->next;
	return head;
}

int length(node *head)
{
	int n = 0;
	node *p;
	p = head;
	while (p != NULL)
	{
		p = p->next;
		n++;
	}
	return n;
}

void print(node *head)
{
	int n;
	node *p;
	p = head;
	n = length(head);
	cout << "There is " << n << " data in list\n" << endl;
	while(p != NULL)
	{
		cout << p->data << " -> ";
		p = p->next;
	}
	cout << endl;
}

node *del(node *head, int num)
{
	node *p1, *p2;
	p1 = head;
	while(num != p1->data && p1->next != NULL)
	{
		p2 = p1;
		p1 = p1->next;
	}
	
	if(num == p1->data)
	{
		if(p1 == head)//删除头节点
		{
			head = p1->next;
			free(p1);   //需要释p1指向的堆空间
		}
		else //删除其他节点
		{
			p2->next = p1->next;
			free(p1); //需要释p1指向的堆空间
		}
	}
	else
	{
		cout << "list has no number you want to delete!" << endl;
	}
	return head;
}


int _tmain(int argc, _TCHAR * argv[])
{
	node *head;
	head = create();
	print(head);
	head = del(head, 1);
	print(head);
	return 0;
}

双链表情况

#include <tchar.h>
#include <iostream>
using namespace std;

typedef struct student{
	int data;
	struct student * next;
	struct student * pre;
} node;

node* create()
{
	node *head, *p, *s;
	int x, cycle = 1;
	head = (node *)malloc(sizeof(node));
	p = head;
	while(cycle)
	{
		cout << "please input the data: ";
		cin >> x;
		cout << endl;
		if( x != 0)
		{
			s = (node *)malloc(sizeof(node));
			s->data = x;
			p->next = s;
			s->pre = p;
			p = s;
		}
		else
		{
			cycle = 0;
		}
		
	}
	p->next = NULL;
	head = head->next;
	head->pre = NULL;
	return head;
}

int length(node *head)
{
	int n = 0;
	node *p;
	p = head;
	while (p != NULL)
	{
		p = p->next;
		n++;
	}
	return n;
}

void print(node *head)
{
	int n;
	node *p;
	p = head;
	n = length(head);
	cout << "There is " << n << " data in list\n" << endl;
	while(p->next != NULL)
	{
		cout << p->data << " -> ";
		p = p->next;
	}
	cout << p->data << endl;
	while(p->pre !=NULL)
	{
		cout << p->data << " -> ";
		p = p->pre;
	}
	cout << p->data << endl;
}

node *del(node *head, int num)
{
	node *p1, *p2;
	p1 = head;
	while(num != p1->data && p1->next != NULL)
	{
		p2 = p1;
		p1 = p1->next;
	}
	
	if(num == p1->data)
	{
		if(p1 == head)//删除头节点
		{
			head = p1->next;
			head->pre = NULL;
			free(p1);   //需要释p1指向的堆空间
		}
		else if(p1->next == NULL)//删除尾节点
		{
			p1->pre->next = NULL;
			free(p1);//需要释p1指向的堆空间
		}
		else //删除中间节点
		{
			p1->next->pre = p1->pre;
			p1->pre->next = p1->next;
			free(p1); //需要释p1指向的堆空间
		}
	}
	else
	{
		cout << "list has no number you want to delete!" << endl;
	}
	return head;
}

int _tmain(int argc, _TCHAR * argv[])
{
	node *head;
	head = create();
	print(head);
	head = del(head, 2);
	print(head);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值