实验11 链表 程序1奇数值结点链表

输入若干个正整数(输入-1为结束标志)建立一个单向链表,头指针为L,将链表L中奇数值的结点重新组成一个新的链表NEW,并输出新建链表的信息。

第一种方法:逆向思维

#include<stdio.h>
#include<stdlib.h>
struct stu_node{
    int num;
    struct stu_node*next;
};
struct stu_node*Create_Stu_Doc();/*新建链表*/
struct stu_node*DeleteDoc(struct stu_node*head);/*奇数项相连就是去掉偶数项*/ 
void Print_Stu_Doc(struct stu_node*head);/*遍历*/ 


int main(void)
{
    struct stu_node*head;
    head=Create_Stu_Doc();
    head=DeleteDoc(head);
    Print_Stu_Doc(head);
}

struct stu_node*Create_Stu_Doc()
{
    struct stu_node*head,*tail,*p;
    int num;
    int size=sizeof(struct stu_node);
    head=tail=NULL;
    scanf("%d",&num);
    while(num!=-1)
    {
        p=(struct stu_node*)malloc(size);
        p->num=num;
        p->next=NULL;
        if(head==NULL)
        head=p;
        else
        tail->next=p;
        tail=p;
        scanf("%d",&num);
    }
    return head;
}

struct stu_node*DeleteDoc(struct stu_node*head)
{
    struct stu_node*ptr1,*ptr2;
    while(head!=NULL&&head->num%2==0)
    {
        ptr1=head;
        ptr2=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_Stu_Doc(struct stu_node*head)
{
    struct stu_node*ptr;
    if(head==NULL)
    {
        printf("N0 Records\n");
        return;
    }
    for(ptr=head;ptr;ptr=ptr->next)
    printf("%d",ptr->num);
    printf("\n");
}

 第二种方法:正向思维

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stud_node{
int num;
struct stud_node *next;
};
void Ptrint_Stu_Doc(struct stud_node *head);
int main()
{
struct stud_node *L,*tail1,*tail2,*p1,*p2,*NEW;
int num;
int size=sizeof(struct stud_node);

L=tail1=NULL;
scanf("%d",&num);
while(num!=-1){                        /*---新建链表L--*/
p1=(struct stud_node *)malloc(size);
p1->num=num;
p1->next=NULL;
if(L==NULL)
L=p1;
else
tail1->next=p1;
tail1=p1;
scanf("%d",&num);
}

NEW=tail2=NULL;      
p1=L;
while(p1!=NULL){
if(p1->num%2==0&&p1!=NULL){         /*--删除链表L中的偶数值---*/
if(p1->next!=NULL){    
p1=p1->next;
continue;
}
else
break;
}
if(p1==NULL)         break;                   /*----将链表L中奇数值的结点重新组成一个新的链表NEW--*/
p2=(struct stud_node *)malloc(size);
p2->num=p1->num;
p2->next=NULL;
if(NEW==NULL)
NEW=p2;
else
tail2->next=p2;
tail2=p2;
p1=p1->next;
}
tail2->next=NULL;
Ptrint_Stu_Doc(NEW);
}
void Ptrint_Stu_Doc(struct stud_node *head)
{
struct stud_node *ptr;
if(head==NULL){
printf("No Records\n");
return;
}
for(ptr=head;ptr;ptr=ptr->next)
printf("%d",ptr->num);
printf("\n");
}

 再加一个

#include<stdio.h>
struct elem
{
int data;
elem *next;
};
int main()
{
int temp;
//head指向链表头元素。tail指向链表最后元素,tempP是个临时存放地址
//frontP指向即将删除元素的前一个元素,deleteP指向将要删除的元素
elem * head,*tail,*tempP,*frontP,*deleteP;
head=tail=NULL;
//读取信息
scanf("%d",&temp);
while(-1!=temp)
{
if(head==NULL)
{
tail=head=new elem;
head->data=temp;
head->next=NULL;
}
else
{
tempP=new elem;
tempP->data=temp;
tempP->next=NULL;
tail->next=tempP;
tail=tempP;
}
scanf("%d",&temp);
}
//删除偶数
tempP=head;
while(NULL!=tempP)
{
if(0==tempP->data%2)
{
if(tempP==head)
{
head=tempP->next;
}
else
{
//找到偶数的前一个数,该数的next指向偶数的next;
for(frontP=head;NULL!=frontP;++frontP)
{
if(frontP->next==tempP)
{
frontP->next=tempP->next;
break;
}
}
}
}
deleteP=tempP;
tempP=tempP->next;
if(0==deleteP->data%2)
delete deleteP;
}
//打印数据
tempP=head;
while(NULL!=tempP)
{
printf("%d\n",tempP->data);
deleteP=tempP;
tempP=tempP->next;
delete deleteP;
}
}

 

转载于:https://www.cnblogs.com/chenlong991223/p/10151737.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值