编程实现单链表和双链表删除节点
来源:互联网 发布:office2013 mac破解版 编辑:程序博客网 时间:2024/04/30 01:03
#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;}
- 编程实现单链表和双链表删除节点
- 编程实现单链表的删除节点
- 编程实现一个单链表节点的删除
- 面试题2 编程实现单链表删除节点
- 编程实现单链表和双链表的插入节点
- php实现双链表的删除和插入节点
- 单链表实现删除最小节点的算法
- javascript实现treeview无刷新添加和删除节点
- GMF中,删除节点和连线的实现
- 实现链表的逆序输出和删除某节点
- C语言实现链表节点的插入和删除
- GMF中,删除节点和连线的另一种实现
- C语言实现链表节点的插入和删除
- 04编程实现单链表的节点查找
- 05编程实现单链表的节点插入
- 编程实现一个单链表节点的插入
- 二叉树的建立、节点查找以及节点删除C和C++实现
- 二叉树的建立、节点查找以及节点删除C和C++实现
- WAS 日志分析(websphere application server)
- 以Apache服务器为例 详解动态网站访问
- NSUserDefaults
- 一行转多行效果(一)
- Oracle 中时间的计算
- 编程实现单链表和双链表删除节点
- 简单动画AlertView
- Exercise 4.2 E2
- python 调用 C语言 方法
- MySQL 5.6 root 初始密码
- Windows CE内核启动分析
- oracle错误大全
- java序列化讲解
- 小米2013年度发布会直播