链表
程序1奇数值结点链表:
输入若干个正整数(输入-1为结束标志)建立一个单向链表,头指针为L,将链表L中奇数值的结点重新组成一个新的链表NEW,并输出新建链表的信息。
#include<stdio.h>
#include<stdlib.h>
struct list_node
{
int num;
struct list_node*next;
};
struct list_node*Creat_List();
struct list_node*New_List(struct list_node*head);
void Print_List_one(struct list_node*head);
void Print_List_two(struct list_node*head);
int main()
{
struct list_node*head;
head=Creat_List();
Print_List_one(head);
head=New_List(head);
Print_List_two(head);
return 0;
}
struct list_node*Creat_List()
{
struct list_node*head,*p,*tail;
int a;
int size=sizeof (struct list_node);
head=tail=NULL;
printf(\"Input:\\n\");
scanf(\"%d\",&a);
while(a!=-1)
{
p=(struct list_node*)malloc(size);
p->num=a;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf(\"%d\",&a);
}
return head;
}
void Print_List_one(struct list_node*head)
{
struct list_node*ptr;
if(head==NULL)
{
printf(\"\\nNo Records\\n\");
return;
}
printf(\"The old list is\\n\");
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{
if(ptr==head)
printf(\"%d\",ptr->num);
else
printf(\"->%d\",ptr->num);
}
printf(\"->NULL\\n\");
}
struct list_node*New_List(struct list_node*head)
{
struct list_node*ptr1,*ptr2;
while(head!=NULL&&head->num%2==0)
{
ptr2=head;
head=head->next;
free(ptr2);
}
if(head==NULL)
return NULL;
ptr1=head;
ptr2=head->next;
while(ptr2!=NULL)
{
if(ptr2->num%2==0)
{
ptr1->next=ptr2->next;
free(ptr2);
}
else
ptr1=ptr2;
ptr2=ptr1->next;
}
return head;
}
void Print_List_two(struct list_node*head)
{
struct list_node*ptr;
if(head==NULL)
{
printf(\"\\nNo Records\\n\");
return;
}
printf(\"The NEW list is:\\n\");
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{
if(ptr==head)
printf(\"%d\",ptr->num);
else
printf(\"->%d\",ptr->num);
}
printf(\"->NULL\\n\");
}
程序2 删除结点:
输入若干个正整数(输入-1为结束标志)建立一个单向链表,再输入一个整数m,删除链表中值为m的所有结点。
#include<stdio.h>
#include<stdlib.h>
struct list_node
{
int num;
struct list_node*next;
};
struct list_node*Creat_List();
void Print_List_one(struct list_node*head);
struct list_node*Delete_List(struct list_node*head,int num);
void Print_List_two(struct list_node*head);
int main()
{
int num;
struct list_node*head;
head=Creat_List();
printf(\"Please input the delete number:\");
scanf(\"%d\",&num);
printf(\"Output:\\n\");
Print_List_one(head);
head=Delete_List(head,num);
Print_List_two(head);
return 0;
}
struct list_node*Creat_List()
{
struct list_node*head,*p,*tail;
int a;
int size=sizeof (struct list_node);
head=tail=NULL;
printf(\"Input:\\n\");
scanf(\"%d\",&a);
while(a!=-1)
{
p=(struct list_node*)malloc(size);
p->num=a;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf(\"%d\",&a);
}
return head;
}
void Print_List_one(struct list_node*head)
{
struct list_node*ptr;
if(head==NULL)
{
printf(\"\\nNo Records\\n\");
return;
}
printf(\"The old list is:\");
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{
if(ptr==head)
printf(\"%d\",ptr->num);
else
printf(\"->%d\",ptr->num);
}
printf(\"->NULL\\n\");
}
struct list_node*Delete_List(struct list_node*head,int num)
{
struct list_node*ptr1,*ptr2;
while(head!=NULL&&head->num==num)
{
ptr2=head;
head=head->next;
free(ptr2);
}
if(head==NULL)
return NULL;
ptr1=head;
ptr2=head->next;
while(ptr2!=NULL)
{
if(ptr2->num==num)
{
ptr1->next=ptr2->next;
free(ptr2);
}
else
ptr1=ptr2;
ptr2=ptr1->next;
}
return head;
}
void Print_List_two(struct list_node*head)
{
struct list_node*ptr;
if(head==NULL)
{
printf(\"\\nNo Records\\n\");
return;
}
printf(\"The remain list is:\\n\");
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{
if(ptr==head)
printf(\"%d\",ptr->num);
else
printf(\"->%d\",ptr->num);
}
printf(\"->NULL\\n\");
}
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。



