您的位置:首页 > 其它

单链表的建立,插入,删除,逆置

2010-01-22 16:45 435 查看
#include<stdio.h>
#include<stdlib.h>
#define FLAG -1

typedef struct node
{
int data;
struct node *next;
}Node, *Linklist;

/*

Linklist HeadCreat()
{
Linklist H = NULL;
Node *s;
int x = 0;

scanf("%d",&x);
while(x != FLAG)
{
s = (Linklist)malloc(sizeof(Node));
s->data = x;
s->next = H;
H = s;
scanf("%d",&x);
}

return H;
}
*/

Linklist TailCreat()
{
Linklist H = NULL;
Node *s,*t = NULL;
int x = 0;

scanf("%d",&x);
while(x != FLAG)
{
s = (Linklist)malloc(sizeof(Node));
s->data = x;
s->next = NULL;

if(H == NULL)
H = s;
else t->next = s;
t = s;
scanf("%d",&x);
}
if(t != NULL)
t->next = NULL;
return H;
}

int Listlen(Linklist H)
{
int j = 0;
Node *s = H;

if (s == NULL)
return 0;
else

while (s->next)
{
s = s->next;
j++;
}
return j+1;
}

Linklist Locate(Linklist H,int x)
{
Node *before = NULL;
Node *local = H;

while((local) && (local->data < x))
{
before = local;
local = local->next;
}

if((local)&&(before->data < x)&&(local->data > x))
return before;
}

Linklist Insert(Linklist H,int x)
{
Node *p = NULL;
Node *s = NULL;

p = Locate(H,x);

if(p == NULL)
{
printf(" Error!");
return H;
}
else
{
s = (Linklist)malloc(sizeof(Node));
s->data = x;
s->next = p->next;
p->next = s;
return H;
}

}

Node * Get(Linklist H,int i)
{
Node *p = H;
int j=0;
while( (p->next != NULL) && (j<i) )
{
p=p->next;
j++;
}
if(j == i)
return p;
else
return NULL;
}

Linklist Del(Linklist H,int i)
{
Node *p = NULL;
Node *s = NULL;

p = Get(H,i-1);

if (p == NULL)
{
printf("The (i-1)th node is not in! ");
return H;
}
else if(p->next == NULL)
{
printf("The ith node is not in!!");
return H;
}
else
{
s=p->next;
p->next=s->next;
free(s);
return H;
}
}

Linklist Delsame(Linklist H)
{ // 若输入11221133只能输出1213 怎么改可以去掉所有相同的节点
Node *p = NULL;
Node *s = NULL;
Node *t = NULL;

if (H == NULL)
return H;
p = H;
while( p->next)
{
s = p;
t = p->next;

while(s->next)
{
if (s->data == t->data)
{
s->next = t->next;
free(t);
t = s->next;
}
else
{
s = s->next;
t = t->next;

}
}
s = s->next;
if( s == NULL)
return H;
}
}

Linklist Reverse(Linklist H)
{
Node *p_1 = NULL;
Node *p_2 = NULL;

p_1=H;
p_2 = p_1->next;
H = p_2->next;
p_1->next = NULL;

while(H->next)
{
p_2->next = p_1;
p_1 = p_2;
p_2 = H;
H = H->next;
}

H->next = p_2;
p_2->next = p_1;

return H;
}

void print(Linklist H)
{
Linklist p = NULL;
p = H;

while(p)
{
printf("<<%d>>",p->data);
p=p->next;
}
}

int main()
{
Linklist H = NULL;
Node *p;
int x=0;
int n=0;
printf("Please input the numbers:!/n");
// H=HeadCreat();
H=TailCreat();
printf("The numbers you input is :/n");
print(H);
printf("/n");

x=Listlen(H);
printf("The lenth of the list is : %d /n",x);

printf("Please in put the number you wanna to add in :/n");
scanf("%d",&x);
H=Insert(H,x);
printf("The total numbers you input is :/n");
print(H);
printf("/n");

printf("Please in put the number you wanna to delete:/n");
scanf("%d",&n);
printf("The total numbers you input is :/n");
H=Del(H,n);
print(H);
printf("/n");

printf("The total numbers you input after reverseis :/n");
H=Reverse(H);
print(H);
printf("/n");

printf("Please input the numbers:/n");
H=TailCreat();
H=Delsame(H);
print(H);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐