与链表相关的题目(一)
来源:互联网 发布:软件替代ac控制器 编辑:程序博客网 时间:2024/04/29 17:18
1.首先是怎么创建一个链表,怎么插入节点,怎么删除一个节点。
程序如下:
// LinkTest1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef struct node
{
int data;
struct node* next;
}Node;
Node* create_Node_NoRing(int a[],int len)
{
Node* head,*p,*q;
p=new Node();
p->data=a[0];
head=p;
for (int i=1;i<len;i++)
{
q=new Node();
q->data=a[i];
p->next=q;
p=q;
}
p=NULL;
return head;
}
Node* create_Node_HaveRing(int a[],int len)
{
Node* head,*p,*q;
p=new Node();
p->data=a[0];
head=p;
for (int i=1;i<len;i++)
{
q=new Node();
q->data=a[i];
p->next=q;
p=q;
}
p->next=head;
return head;
}
void printfLink_NoRing(Node* head)
{
while (head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}
//在num这个节点后面添加target这个节点
void InsertIntoAfter(Node* head,int num,int target)
{
Node* p=NULL;
Node* temp=NULL;
Node* q=NULL;
if (head)
{
p=head;
while(p->data != num)
p=p->next;
q=p->next;
temp=new Node();
temp->data=target;
temp->next=q;
p->next=temp;
}
}
void InsertIntoBefore(Node* head,int num,int target)
{
Node* p=NULL;
Node* temp=NULL;
Node* q=NULL;
if (head)
{
p=head;
while (p->next->data!=num)
{
p=p->next;
}
q=p;//要好插入位置的前一个节点
p=p->next;
temp=new Node();
temp->data=target;
temp->next=p;
q->next=temp;
}
}
void printfLink_HaveRing(Node* head)
{
if (head!=NULL)
{
Node* p=head;
do
{
cout<<p->data<<" ";
p=p->next;
} while (p!=head);
}
cout<<endl;
}
//删除指定的节点
void DeleteTheTarget(Node* head,int target)
{
Node*p,* q,*temp;
if (head!=NULL)
{
p=head;
while(p->next->data!=target)
p=p->next;
q=p;//要删除节点的前面一个节点
p=p->next;//p 就是要删除的节点
temp=p->next;//要删除节点的后面一个节点
q->next=temp;
free(p);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[]={1,2,3,4,5,6,7,8,9,10};
int len=sizeof(a)/sizeof(int);
Node* head1=create_Node_NoRing(a,len);
cout<<"打印没有环的link"<<endl;
printfLink_NoRing(head1);
Node* head2=create_Node_HaveRing(a,len);
cout<<"打印有环的link"<<endl;
printfLink_HaveRing(head2);
cout<<"在节点后面添加节点"<<endl;
InsertIntoAfter(head1,5,55);
cout<<"打印没有环的link"<<endl;
printfLink_NoRing(head1);
cout<<"在节点前面添加节点"<<endl;
InsertIntoBefore(head1,5,555);
cout<<"打印没有环的link"<<endl;
printfLink_NoRing(head1);
cout<<"删除指定的节点"<<endl;
DeleteTheTarget(head1,55);
cout<<"打印没有环的link"<<endl;
printfLink_NoRing(head1);
system("pause");
return 0;
}
// LinkTest1.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>using namespace std;typedef struct node{int data;struct node* next;}Node;Node* create_Node_NoRing(int a[],int len){Node* head,*p,*q;p=new Node();p->data=a[0];head=p;for (int i=1;i<len;i++){q=new Node();q->data=a[i];p->next=q;p=q;}p=NULL;return head;}Node* create_Node_HaveRing(int a[],int len){Node* head,*p,*q;p=new Node();p->data=a[0];head=p;for (int i=1;i<len;i++){q=new Node();q->data=a[i];p->next=q;p=q;}p->next=head;return head;}void printfLink_NoRing(Node* head){while (head!=NULL){cout<<head->data<<" ";head=head->next;}cout<<endl;}//在num这个节点后面添加target这个节点void InsertIntoAfter(Node* head,int num,int target){Node* p=NULL;Node* temp=NULL;Node* q=NULL;if (head){p=head;while(p->data != num)p=p->next;q=p->next;temp=new Node();temp->data=target;temp->next=q;p->next=temp;}}void InsertIntoBefore(Node* head,int num,int target){Node* p=NULL;Node* temp=NULL;Node* q=NULL;if (head){p=head;while (p->next->data!=num){p=p->next;}q=p;//要好插入位置的前一个节点p=p->next;temp=new Node();temp->data=target;temp->next=p;q->next=temp;}}void printfLink_HaveRing(Node* head){if (head!=NULL){Node* p=head;do {cout<<p->data<<" ";p=p->next;} while (p!=head);}cout<<endl;}//删除指定的节点void DeleteTheTarget(Node* head,int target){Node*p,* q,*temp;if (head!=NULL){p=head;while(p->next->data!=target)p=p->next;q=p;//要删除节点的前面一个节点p=p->next;//p 就是要删除的节点temp=p->next;//要删除节点的后面一个节点q->next=temp;free(p);}}int _tmain(int argc, _TCHAR* argv[]){int a[]={1,2,3,4,5,6,7,8,9,10};int len=sizeof(a)/sizeof(int);Node* head1=create_Node_NoRing(a,len);cout<<"打印没有环的link"<<endl;printfLink_NoRing(head1);Node* head2=create_Node_HaveRing(a,len);cout<<"打印有环的link"<<endl;printfLink_HaveRing(head2);cout<<"在节点后面添加节点"<<endl;InsertIntoAfter(head1,5,55);cout<<"打印没有环的link"<<endl;printfLink_NoRing(head1);cout<<"在节点前面添加节点"<<endl;InsertIntoBefore(head1,5,555);cout<<"打印没有环的link"<<endl;printfLink_NoRing(head1);cout<<"删除指定的节点"<<endl;DeleteTheTarget(head1,55);cout<<"打印没有环的link"<<endl;printfLink_NoRing(head1);system("pause"); return 0;}
- 与链表相关的题目(一)
- 与概率相关的题目
- 与排列相关的题目
- 链表相关题目
- 链表相关题目的实现
- 贪心算法及相关题目(一)
- 三道与数据结构相关的题目
- 分享一道与二进制相关的题目
- LintCiode:链表相关题目
- 概率相关的题目
- cocos2相关的题目
- 栈的相关题目
- 树的相关题目
- 几道与背包九讲相关的题目(HDU - 1712,3033,2844,2159)
- free与df相关题目
- 算法艺术与信息学竞赛的相关题目
- 与倒数第N个节点相关的题目
- 与正则相关的一切 (一)基础知识
- 从头开始构建开源的Android应用研发ALM解决方案(二)建立个人桌面开发环境
- seesion 与cookie 参考资料
- 移动网络名词理解(2)--- 蜂窝网络
- 多线程程序设计的成功关键
- 基于Linux的Web服务器性能测试
- 与链表相关的题目(一)
- 博客开通以来的心得
- cvCreateVideoWriter的用法
- httpclient下载文件
- 浅谈主流SNS开放平台的接入(一)——概述
- 怎么向android模拟器中传文件
- php 模拟 SQLSERVER的两个函数:dateadd(),datediff()
- hdu 1054 Strategic Game
- DCT原型 ——傅里叶级数