单链表的操作
2010-09-12 20:29
113 查看
// DataStructure.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
// 链表节点定义
typedef struct node
{
int data;
node *next;
}node;
node *CreateList()
{
int i = 0; // 链表中数据的个数
node *head,*p,*q;
int x = 0;
head = (node *)malloc(sizeof(node)); // 创建头节点
while(1)
{
printf("Please input the data:");
scanf("%d",&x);
if(x == 0) // 输入数据为0时创建结束
break;
p = (node *)malloc(sizeof(node));
p->data = x;
if(++i == 1) // 链表只有一个元素
{
head->next = p; // 连接到head的后面
}
else
{
q->next = p; // 连接到链表的尾端
}
q = p; // q指向末节点
}
q->next = NULL; // 链表的最有一个指针为NULL
return head;
}
// 测量单链表的长度
int ListLength(node *head)
{
int len = 0;
node *p;
p = head->next;
while(p != NULL) //遍历链表
{
len++;
p = p->next;
}
return len;
}
//打印单链表
void PrintList(node *head)
{
node *p;
int index = 0;
if(head->next == NULL) //链表为空
{
printf("The List is a empty List!!/n");
return;
}
else
{
p = head->next;
while(p != NULL) //遍历链表
{
printf("The %dth node in List is:%d /n",++index,p->data);
p = p->next;
}
}
// 单链表节点查找
node *SearchNode(node *head,int pos)
{
node *p = head->next;
int index = 1;
if(pos < 0) //pos位置不对
{
printf("Incorrect position to search node!!/n");
return NULL;
}
if(pos == 0) //head位置
{
return head;
}
if(p == NULL) //链表为空
{
printf("List is empty !!/n");
return NULL;
}
while(p->next != NULL && index < pos)
{
p = p->next;
index++;
}
if(index == pos)
{
return p;
}
else
{
return NULL;
}
//单链表节点插入
void InsertList(node *head,int pos,int data)
{
node *item = NULL;
node *p;
item = (node *)malloc(sizeof(node));
item->data = data;
if(pos == 0)
{
head->next = item;
}
p = SearchNode(head,--pos);
if(p != NULL)
//单链表节点删除
void DeleteList(node *head,int pos)
{
node *item = NULL;
node *p = head->next;
if(p == NULL) //链表为空
{
printf("List is empty !!/n");
}
p = SearchNode(head,--pos); //获取位置pos的节点指针
if(p != NULL && p->next != NULL)
{
item = p->next;
p->next = item->next;
delete item;
}
//单链表的逆置
void ReverseList(node *head)
{
node *p,*q,*r;
if(head->next == NULL) //链表为空
{
printf("The List is a empty List!!/n");
return;
}
p = head->next;
q = p->next; // 保存原第二个节点
p->next = NULL; // 原第一个节点为末节点
while(q != NULL)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = p; //新的第一个节点为末节点
//寻找单链表的中间元素
node *SearchMiddleNode(node *head)
{
int i = 0;
int j = 0;
node *current = NULL;
node *middle = NULL;
current = head->next;
middle =head->next;
while(current != NULL)
{
if(i/2 > j)
{
j++;
middle = middle->next;
}
i++;
current = current->next;
}
return middle;
//单链表正向排序
void SortList(node *head)
{
node *p,*q;
int temp;
if(head->next == NULL) //链表为空
{
printf("The List is a empty List!!/n");
return;
}
for(p = head->next;p->next != NULL;p = p->next)
{
for(q = head->next;q->next != NULL;q = q->next)
{
if(q->data > q->next->data)
{
temp = q->data;
q->data = q->next->data;
q->next->data = temp;
}
}
}
}
//单链表是否有回环
bool IsLoop(node *head)
{
node *p1 = head;
node *p2 = head;
if(head == NULL || head->next == NULL) //head为空或者链表为空是返回false
{
return false;
}
do
{
p1 = p1->next; // p1走一步
p2 = p2->next->next; // p2走两步
}while(p2 != NULL && p2->next !=NULL && p1 != p2);
if(p1 == p2)
{
return true;
}
else
{
return false;
}
//有序单链表的合并
node *MergeList(node *list1,node *list2)
{
node *head = NULL; //合并后单链表的头指针
if(list1 == NULL) //有一个链表为空,直接返回另外一个链表
{
return list2;
}
else if(list2 == NULL)
{
return list1;
}
if(list1->data < list2->data) // 递归调用方法
{
head = list1;
head->next = MergeList(list1->next,list2);
}
else
{
head = list2;
head->next = MergeList(list1,list2->next);
}
return head;
}
int _tmain(int argc, _TCHAR* argv[])
{
node *L;
int len;
L = CreateList(); // 创建单链表
printf("/n");
len = ListLength(L); // 测量单链表的长度
printf("The Length of List is:%d/n",len);
printf("/n");
printf("The data of List is:/n");
PrintList(L); //打印单链表
printf("/n");
int pos = 4; // 点链表节点查找
int data;
data = SearchNode(L,4)->data;
printf("The %dth node in List is:%d /n",pos,data);
printf("/n");
int InsertPos = 4; // 单链表节点插入
int InaertData = 1000;
InsertList(L,InsertPos,InaertData);
printf("After being inserted,The data of List is:/n");
PrintList(L); //打印单链表
printf("/n");
int DeletePos = 4; // 单链表节点删除
DeleteList(L,DeletePos);
printf("After being deleted,The data of List is:/n");
PrintList(L); //打印单链表
printf("/n");
ReverseList(L); //单链表的逆置
printf("After being reversed,The data of List is:/n");
PrintList(L); //打印单链表
printf("/n");
int MiddleData; //寻找单链表的中间元素
MiddleData = SearchMiddleNode(L)->data;
printf("The middle node in List is:%d /n",MiddleData);
printf("/n");
SortList(L); //单链表正向排序
printf("After being sroted,The data of List is:/n");
PrintList(L); //打印单链表
printf("/n");
//单链表是否有回环
printf("There is %s a loop in the List",IsLoop(L)?"":"not");
//有序单链表的合并
node *L2;
printf("/n");
L2 = CreateList(); // 创建单链表L2
printf("/n");
printf("The data of List2 is:/n");
PrintList(L2); //打印单链表L2
printf("/n");
SortList(L); //单链表正向排序
SortList(L2); //单链表正向排序
printf("The data of MergeList of List1 and List2 is:/n");
PrintList(MergeList(L,L2)); //打印合并单链表
printf("/n");
return 0;
}
//
#include "stdafx.h"
#include <stdlib.h>
// 链表节点定义
typedef struct node
{
int data;
node *next;
}node;
node *CreateList()
{
int i = 0; // 链表中数据的个数
node *head,*p,*q;
int x = 0;
head = (node *)malloc(sizeof(node)); // 创建头节点
while(1)
{
printf("Please input the data:");
scanf("%d",&x);
if(x == 0) // 输入数据为0时创建结束
break;
p = (node *)malloc(sizeof(node));
p->data = x;
if(++i == 1) // 链表只有一个元素
{
head->next = p; // 连接到head的后面
}
else
{
q->next = p; // 连接到链表的尾端
}
q = p; // q指向末节点
}
q->next = NULL; // 链表的最有一个指针为NULL
return head;
}
// 测量单链表的长度
int ListLength(node *head)
{
int len = 0;
node *p;
p = head->next;
while(p != NULL) //遍历链表
{
len++;
p = p->next;
}
return len;
}
//打印单链表
void PrintList(node *head)
{
node *p;
int index = 0;
if(head->next == NULL) //链表为空
{
printf("The List is a empty List!!/n");
return;
}
else
{
p = head->next;
while(p != NULL) //遍历链表
{
printf("The %dth node in List is:%d /n",++index,p->data);
p = p->next;
}
}
// 单链表节点查找
node *SearchNode(node *head,int pos)
{
node *p = head->next;
int index = 1;
if(pos < 0) //pos位置不对
{
printf("Incorrect position to search node!!/n");
return NULL;
}
if(pos == 0) //head位置
{
return head;
}
if(p == NULL) //链表为空
{
printf("List is empty !!/n");
return NULL;
}
while(p->next != NULL && index < pos)
{
p = p->next;
index++;
}
if(index == pos)
{
return p;
}
else
{
return NULL;
}
//单链表节点插入
void InsertList(node *head,int pos,int data)
{
node *item = NULL;
node *p;
item = (node *)malloc(sizeof(node));
item->data = data;
if(pos == 0)
{
head->next = item;
}
p = SearchNode(head,--pos);
if(p != NULL)
//单链表节点删除
void DeleteList(node *head,int pos)
{
node *item = NULL;
node *p = head->next;
if(p == NULL) //链表为空
{
printf("List is empty !!/n");
}
p = SearchNode(head,--pos); //获取位置pos的节点指针
if(p != NULL && p->next != NULL)
{
item = p->next;
p->next = item->next;
delete item;
}
//单链表的逆置
void ReverseList(node *head)
{
node *p,*q,*r;
if(head->next == NULL) //链表为空
{
printf("The List is a empty List!!/n");
return;
}
p = head->next;
q = p->next; // 保存原第二个节点
p->next = NULL; // 原第一个节点为末节点
while(q != NULL)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = p; //新的第一个节点为末节点
//寻找单链表的中间元素
node *SearchMiddleNode(node *head)
{
int i = 0;
int j = 0;
node *current = NULL;
node *middle = NULL;
current = head->next;
middle =head->next;
while(current != NULL)
{
if(i/2 > j)
{
j++;
middle = middle->next;
}
i++;
current = current->next;
}
return middle;
//单链表正向排序
void SortList(node *head)
{
node *p,*q;
int temp;
if(head->next == NULL) //链表为空
{
printf("The List is a empty List!!/n");
return;
}
for(p = head->next;p->next != NULL;p = p->next)
{
for(q = head->next;q->next != NULL;q = q->next)
{
if(q->data > q->next->data)
{
temp = q->data;
q->data = q->next->data;
q->next->data = temp;
}
}
}
}
//单链表是否有回环
bool IsLoop(node *head)
{
node *p1 = head;
node *p2 = head;
if(head == NULL || head->next == NULL) //head为空或者链表为空是返回false
{
return false;
}
do
{
p1 = p1->next; // p1走一步
p2 = p2->next->next; // p2走两步
}while(p2 != NULL && p2->next !=NULL && p1 != p2);
if(p1 == p2)
{
return true;
}
else
{
return false;
}
//有序单链表的合并
node *MergeList(node *list1,node *list2)
{
node *head = NULL; //合并后单链表的头指针
if(list1 == NULL) //有一个链表为空,直接返回另外一个链表
{
return list2;
}
else if(list2 == NULL)
{
return list1;
}
if(list1->data < list2->data) // 递归调用方法
{
head = list1;
head->next = MergeList(list1->next,list2);
}
else
{
head = list2;
head->next = MergeList(list1,list2->next);
}
return head;
}
int _tmain(int argc, _TCHAR* argv[])
{
node *L;
int len;
L = CreateList(); // 创建单链表
printf("/n");
len = ListLength(L); // 测量单链表的长度
printf("The Length of List is:%d/n",len);
printf("/n");
printf("The data of List is:/n");
PrintList(L); //打印单链表
printf("/n");
int pos = 4; // 点链表节点查找
int data;
data = SearchNode(L,4)->data;
printf("The %dth node in List is:%d /n",pos,data);
printf("/n");
int InsertPos = 4; // 单链表节点插入
int InaertData = 1000;
InsertList(L,InsertPos,InaertData);
printf("After being inserted,The data of List is:/n");
PrintList(L); //打印单链表
printf("/n");
int DeletePos = 4; // 单链表节点删除
DeleteList(L,DeletePos);
printf("After being deleted,The data of List is:/n");
PrintList(L); //打印单链表
printf("/n");
ReverseList(L); //单链表的逆置
printf("After being reversed,The data of List is:/n");
PrintList(L); //打印单链表
printf("/n");
int MiddleData; //寻找单链表的中间元素
MiddleData = SearchMiddleNode(L)->data;
printf("The middle node in List is:%d /n",MiddleData);
printf("/n");
SortList(L); //单链表正向排序
printf("After being sroted,The data of List is:/n");
PrintList(L); //打印单链表
printf("/n");
//单链表是否有回环
printf("There is %s a loop in the List",IsLoop(L)?"":"not");
//有序单链表的合并
node *L2;
printf("/n");
L2 = CreateList(); // 创建单链表L2
printf("/n");
printf("The data of List2 is:/n");
PrintList(L2); //打印单链表L2
printf("/n");
SortList(L); //单链表正向排序
SortList(L2); //单链表正向排序
printf("The data of MergeList of List1 and List2 is:/n");
PrintList(MergeList(L,L2)); //打印合并单链表
printf("/n");
return 0;
}
相关文章推荐
- 单链表的操作练习
- 在无头结点的动态单链表上实现线性表操作Insert(L,i,b)
- 单链表操作之反转单链表
- 工大数据结构之实验指导之【一】单链表的操作
- C++单链表的创建与操作
- (2)单链表的操作 ① 输入一组整型元素序列,使用尾插法建立一个带有头结点的单链表。 ② 实现该线性表的遍历。 ③ 在该单链表的第i个元素前插入一个整数。 ④ 删除该单链表中的第i个元素,其值通过参数
- 单链表的操作
- 数据结构学习笔记2(链表 上 单链表基础操作&实现多项式相乘)
- 单链表的一些操作
- 单链表的各种操作——创建及遍历
- 对单链表的一些操作
- 笔试面试常考数据结构-单链表常用操作编程实现
- 链表算法一之单链表基本操作
- C++实现:单链表的反转(序)操作
- 单链表操作
- 单链表的一些操作
- 单链表的操作
- 单链表的各种操作 笔试 面试
- 数据结构_完成单链表的后续操作实现
- 单链表操作(数据结构实验一)