单链表的各种操作(VS2010测试通过…

#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));

head;

while(cycle)

{

printf("\nplease input the data: ");

scanf("%d",&x);

if(x != 0)

{

(node *)malloc(sizeof(node));

s->data x;

printf("\n%d",s->data);

p->next s;

s;

}

else

cycle 0;

}

head head->next;

p->next NULL;

printf("\n  yyy  %d",head->data);

return (head);

}

//单链表测长

int length(node *head)

{

int 0;

node *p;

head;

while(p != NULL)

{

p->next;

n++;

}

return (n);

}

//单链表打印

void print(node *head)

{

node *p;

int n;

=length(head);

printf("\nNow,These %d records are :\n",n);

head;

if(head != NULL)   //空链表

{}

while(p != NULL)

{

printf("  uuu  %d  \n",p->data);

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;

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;

length(head);


if((head == NULL)||(head->next == NULL))  //非空链表或单数据链表

return (head);

head;


for(i=1; i<L; i++)

{

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->next;

}

}


return (head);

}


//单链表逆置(仅仅逆置了数据,前驱后继关系没有逆置)

node *reverse1(node *head)

{

node *p, *p2;

int L, temp;

int i, j;

length(head);


if((head == NULL)||(head->next == NULL))  //非空链表或单数据链表

return (head);

head;


for(i=1; i<L; i++)

{

head;             //每个循环后把指针指向链表头

for(j=i+1; j<=L; j++)

{

p2 p->next;

if(1)

{

temp p2->data;

p2->data p->data;

p->data temp;

}

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;

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;



<script type="text/javascript" id="wumiiRelatedItems"> </script>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值