您的位置:首页 > 其它

单链表的各种操作(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;





转发至微博
 



转发至微博
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: