单链表操作

3 篇文章 0 订阅
3 篇文章 0 订阅

1、单链表结构

typedef struct ListNode {
	int data;
	ListNode *next;
} ListNode;

2、单链表创建

//创建一个单链表 
ListNode *create_list() {
	int len = 0;
	ListNode *head, *p, *q;
	int data;
	head = (ListNode*)malloc(sizeof(ListNode));
	q = head;
	while(1) {
		cout << "Please input the data: " ;
		cin >> data;
		if(data == 0) break;
		p = (ListNode*)malloc(sizeof(ListNode));
		p->data = data;
		q->next = p;
		q = p;
	}
	q->next = NULL;
	return head;
}

3、单链表打印

//单链表的打印 
void print_list(ListNode *head) {
	ListNode *p = head->next;
	if(p == NULL) {
		cout << "List is empty." << endl;
		return;
	}
	while(p != NULL) {
		cout << p->data;
		p = p->next;
		if(p != NULL) cout << " -> ";
	}
	cout << endl;
}

4、单链表长度

//测试单链表的长度
int length_list(ListNode *head) {
	ListNode *p = head->next;
	int len = 0;
	while(p != NULL) {
		len++;
		p = p->next;
	}
	return len;
} 

5、单链表查找

//单链表的查找
ListNode *search_list(ListNode *head, int pos) {
	ListNode *p = head->next;
	if(pos < 0) {
		cout << "Incorrect postion to search list." << endl;
		return NULL;
	}
	if(pos == 0) {
		return head;
	}
	if(p == NULL) {
		cout << "List is empty." << endl;
		return NULL;
	}
	while(--pos) {
		if((p = p->next) == NULL) {
			cout << "Incorrenct postion to search list." << endl;
			return NULL;
		}
	}
	return p;
} 

6、单链表插入

//单链表的插入
ListNode *insert_list(ListNode *head, int pos, int data) {
	ListNode *p, *item;
	item = (ListNode*)malloc(sizeof(ListNode));
	item->data = data;
	if(pos == 0) {
		item->next = head->next;
		head->next = item;
		return head;
	}
	p = search_list(head, pos);
	if(p != NULL) {
		item->next = p->next;
		p->next = item;
	}
	return head;
} 

7、单链表删除

//单链表的删除
ListNode *delete_list(ListNode *head, int pos) {
	ListNode *p = head->next;
	ListNode *item = NULL;
	if(p == NULL) {
		cout << "List is empty." << endl;
		return NULL;
	}
	p = search_list(head, pos - 1);
	if(p != NULL && p->next != NULL) {
		item = p->next;
		p->next = item->next;
		free(item);
	}
	return head;
} 

8、单链表逆置

//将单链表逆置
//例如原链表为: 1->2->3->4;逆置后为: 4->3->2->1 
ListNode *reverse_list(ListNode *head) {
	ListNode *p, *q;
	if(head->next == NULL) {
		return head;
	}
	p = head->next;
	q = p->next;
	p->next = NULL;
	while(q != NULL) {
		ListNode *r = q->next;
		q->next = head->next;
		head->next = q;
		q = r;
	}
	return head;
} 

9、单链表查找中间值

//查找单链表中间的元素,只需要一次遍历
ListNode *search_middle(ListNode *head) {
	ListNode *current = NULL;
	ListNode *middle = NULL;
	int i = 0, j = 0;
	current = middle = head->next;
	while(current != NULL) {
		if(j < i / 2) {
			j++;
			middle = middle->next;
		}
		i++;
		current = current->next;
	}
	return middle;
} 

10、单链表测试

int main()
{
	int pos, data;
	ListNode *head, *item;
	head = create_list();
	cout << endl;
	print_list(head);
	cout << endl;
	int len = length_list(head);
	cout << "The length is : " << len << endl;
	cout << endl;
	cout << "Please input search pos: ";
	cin >> pos;
	item = search_list(head, pos);
	if(item != NULL) {
		cout << "The pos " << pos << "'th data is: " << item->data << endl;
	}	
	cout << endl;
	cout << "The input insert pos and data is: ";
	cin >> pos >> data;
	head = insert_list(head, pos, data);
	print_list(head);
	cout << endl;
	cout << "Please input the delete pos: ";
	cin >> pos;
	head = delete_list(head, pos);
	print_list(head);
	head = reverse_list(head);
	print_list(head);	
	item = search_middle(head);
	cout << "The middle data is : " << item->data << endl;
	
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值