单链表的各种操作(VS2010测试通过…
2013-11-01 09:23
274 查看
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
using namespace std;
typedef struct student
{
int data;
struct student *next;
}node;
//建立单链表
node *creat()
{
node *head, *p, *s;
int x, cycle = 1;
head = (node *)malloc(sizeof(node));
p = head;
while(cycle)
{
printf("\nplease input the data: ");
scanf("%d",&x);
if(x != 0)
{
s = (node *)malloc(sizeof(node));
s->data = x;
printf("\n%d",s->data);
p->next = s;
p = s;
}
else
cycle = 0;
}
head = head->next;
p->next = NULL;
printf("\n yyy %d",head->data);
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)
{
node *p;
int n;
n =length(head);
printf("\nNow,These %d records are :\n",n);
p = head;
if(head != NULL) //空链表
{}
while(p != NULL)
{
printf(" uuu %d \n",p->data);
p = p->next;
}
}
//单链表删除节点
node *del(node *head, int x)
{
node *p1, *p2;
p1 = head;
while((x != p1->data) && (p1->next != NULL)) //元素x不是首节点&&没到链表尾
{
p2 = p1;
p1 = p1->next;
}
if(x = p1->data)
{
if(p1 == head) //被删除节点为首节点
{
head = p1->next;
free(p1);
}
else
p2->next = p1->next;
}
else
printf("\n%d cound not been found",x);
return (head);
}
//单链表插入节点(在第k个节点后插入)
node *insert(node *head, int k, int x)
{
node *p0, *p1;
int i, L;
L = length(head);
p1 = head;
p0 = (node *)malloc(sizeof(node)); //创建新节点
p0->data = x;
if(k < 0)
printf("\nout of bounds");
for(i=1; (i<k)&&p1; i++)
{
p1 = p1->next;
}
if(k)
{
p0->next = p1->next;
p1->next = p0;
}
else
{
p0->next = head;
head = p0;
}
return (head);
}
//单链表从小到大排序
node *sort(node *head)
{
node *p, *p2;
int L, temp;
int i, j;
L = length(head);
if((head == NULL)||(head->next == NULL)) //非空链表或单数据链表
return (head);
p = head;
for(i=1; i<L; i++)
{
p = head; //每个循环后把指针指向链表头
for(j=i+1; j<=L; j++)
{
p2 = p->next;
if((p->data) > (p2->data))
{
temp = p2->data;
p2->data = p->data;
p->data = temp;
}
p = p->next;
}
}
return (head);
}
//单链表逆置(仅仅逆置了数据,前驱后继关系没有逆置)
node *reverse1(node *head)
{
node *p, *p2;
int L, temp;
int i, j;
L = length(head);
if((head == NULL)||(head->next == NULL)) //非空链表或单数据链表
return (head);
p = head;
for(i=1; i<L; i++)
{
p = head; //每个循环后把指针指向链表头
for(j=i+1; j<=L; j++)
{
p2 = p->next;
if(1)
{
temp = p2->data;
p2->data = p->data;
p->data = temp;
}
p = p->next;
}
}
return (head);
}
//单链表逆置(连接顺序逆置)
node *reverse2(node *head)
{
node *p1, *p2, *p3;
if((head == NULL)||(head->next == NULL)) //非空链表或单数据链表
return (head);
p1 = head;
p2 = p1->next;
while(p2)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head->next = NULL;
head = p1;
return (head);
}
//main函数调用测试
int main(int, char**, char**)
{
node *a, *head_del, *head_ins, *head_sort, *head_rev;
a = creat();
length(a);
print(a);
printf("\nPlease enter the data which you want to delete: ");
int x;
scanf("%d",&x);
head_del = del(a, x);
print(head_del);
printf("\nPlease enter the number which you want to insert: ");
int k, y;
scanf("%d",&k);
scanf("%d",&y);
printf("\nk = %d\ny = %d\n", k, y);
head_ins = insert(head_del, k, y);
print(head_ins);
printf("\nSorting from small to large:");
head_sort = sort(head_ins);
print(head_sort);
printf("\nReversing the data:");
//reverse1(head_sort);
//print(head_sort);
head_rev = reverse2(head_sort);
print(head_rev);
return 0;
}
转发至微博
转发至微博
#include <stdio.h>
#include <string.h>
#include <conio.h>
using namespace std;
typedef struct student
{
int data;
struct student *next;
}node;
//建立单链表
node *creat()
{
node *head, *p, *s;
int x, cycle = 1;
head = (node *)malloc(sizeof(node));
p = head;
while(cycle)
{
printf("\nplease input the data: ");
scanf("%d",&x);
if(x != 0)
{
s = (node *)malloc(sizeof(node));
s->data = x;
printf("\n%d",s->data);
p->next = s;
p = s;
}
else
cycle = 0;
}
head = head->next;
p->next = NULL;
printf("\n yyy %d",head->data);
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)
{
node *p;
int n;
n =length(head);
printf("\nNow,These %d records are :\n",n);
p = head;
if(head != NULL) //空链表
{}
while(p != NULL)
{
printf(" uuu %d \n",p->data);
p = p->next;
}
}
//单链表删除节点
node *del(node *head, int x)
{
node *p1, *p2;
p1 = head;
while((x != p1->data) && (p1->next != NULL)) //元素x不是首节点&&没到链表尾
{
p2 = p1;
p1 = p1->next;
}
if(x = p1->data)
{
if(p1 == head) //被删除节点为首节点
{
head = p1->next;
free(p1);
}
else
p2->next = p1->next;
}
else
printf("\n%d cound not been found",x);
return (head);
}
//单链表插入节点(在第k个节点后插入)
node *insert(node *head, int k, int x)
{
node *p0, *p1;
int i, L;
L = length(head);
p1 = head;
p0 = (node *)malloc(sizeof(node)); //创建新节点
p0->data = x;
if(k < 0)
printf("\nout of bounds");
for(i=1; (i<k)&&p1; i++)
{
p1 = p1->next;
}
if(k)
{
p0->next = p1->next;
p1->next = p0;
}
else
{
p0->next = head;
head = p0;
}
return (head);
}
//单链表从小到大排序
node *sort(node *head)
{
node *p, *p2;
int L, temp;
int i, j;
L = length(head);
if((head == NULL)||(head->next == NULL)) //非空链表或单数据链表
return (head);
p = head;
for(i=1; i<L; i++)
{
p = head; //每个循环后把指针指向链表头
for(j=i+1; j<=L; j++)
{
p2 = p->next;
if((p->data) > (p2->data))
{
temp = p2->data;
p2->data = p->data;
p->data = temp;
}
p = p->next;
}
}
return (head);
}
//单链表逆置(仅仅逆置了数据,前驱后继关系没有逆置)
node *reverse1(node *head)
{
node *p, *p2;
int L, temp;
int i, j;
L = length(head);
if((head == NULL)||(head->next == NULL)) //非空链表或单数据链表
return (head);
p = head;
for(i=1; i<L; i++)
{
p = head; //每个循环后把指针指向链表头
for(j=i+1; j<=L; j++)
{
p2 = p->next;
if(1)
{
temp = p2->data;
p2->data = p->data;
p->data = temp;
}
p = p->next;
}
}
return (head);
}
//单链表逆置(连接顺序逆置)
node *reverse2(node *head)
{
node *p1, *p2, *p3;
if((head == NULL)||(head->next == NULL)) //非空链表或单数据链表
return (head);
p1 = head;
p2 = p1->next;
while(p2)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head->next = NULL;
head = p1;
return (head);
}
//main函数调用测试
int main(int, char**, char**)
{
node *a, *head_del, *head_ins, *head_sort, *head_rev;
a = creat();
length(a);
print(a);
printf("\nPlease enter the data which you want to delete: ");
int x;
scanf("%d",&x);
head_del = del(a, x);
print(head_del);
printf("\nPlease enter the number which you want to insert: ");
int k, y;
scanf("%d",&k);
scanf("%d",&y);
printf("\nk = %d\ny = %d\n", k, y);
head_ins = insert(head_del, k, y);
print(head_ins);
printf("\nSorting from small to large:");
head_sort = sort(head_ins);
print(head_sort);
printf("\nReversing the data:");
//reverse1(head_sort);
//print(head_sort);
head_rev = reverse2(head_sort);
print(head_rev);
return 0;
}
转发至微博
转发至微博
相关文章推荐
- 单链表的各种操作(增删查改)
- 通过CountDownLatch模拟高并发下测试各种单例模式的执行结果
- web项目中,Java Resoures连接数据库测试通过,但JSP页面连接操作数据库失败的解决方案
- VS2010测试功能之旅:编码的UI测试(4)-通过编写测试代码的方式建立UI测试(下)
- 一个自己测试通过的发邮件操作
- VS2010通过OLE操作Excel2010
- 单链表的各种操作
- java各种文件的读写,通过不同的方式进行操作。
- 数据结构单链表的各种操作C++实现
- ConvertCStringToFloat函数(VS2010测试通过)
- C# 操作线程的通用类[测试通过]
- 一个自己测试通过的操作EXCEL
- 基于半边数据结构(翼边数据结构)的Eular操作来实现扫成 通过OpenGL进行CAD实体建模 经测试可直接运行
- “.NET研究”VS2010测试功能之旅:编码的UI测试(3)-操作对象的识别原理
- 数据库基本操作小摘(代码由mysql-essential-5.1.50-win32测试通过)
- 使用容器操作单链表并测试
- 一起谈.NET技术,VS2010 测试功能之旅:编码的UI测试(4)-通过编写测试代码的方式建立UI测试(上)
- 通过LDAP在AD域控上进行添加、删除、修改、查询等各种操作
- 通过CSS,Jquery实现DIV的各种动作(包括漂移,未测试IE6)
- 单链表的各种操作(面试必备)