与链表相关的题目(一)

来源:互联网 发布:软件替代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;}