链表作业

6-1 建立学生信息链表 (20分)

本题要求实现一个将输入的学生成绩组织成单向链表的简单函数。

函数接口定义:
void input();

该函数利用scanf从输入中获取学生的信息,并将其组织成单向链表。链表节点结构定义如下:

struct stud_node {
int num; /学号/
char name[20]; /姓名/
int score; /成绩/
struct stud_node *next; /指向下个结点的指针/
};

void input()
{
	struct stud_node *p;
	head=tail=(struct stud_node *)malloc(sizeof(struct stud_node));
	int a;
	char s[20];
	int b;
	while(1)	
	{
	scanf("%d",&a);
	if(a==0)
		break;
	scanf("%s %d",s,&b);
		p=(struct stud_node *)malloc(sizeof(struct stud_node));
		p->num =a;
		strcpy(p->name ,s );
		p->score =b;
		tail->next =p;
		tail=p;
		scanf("%d",&a);
    }
	tail->next =NULL;
	head=head->next ;
}

6-3 逆序数据建立链表 (20分)

本题要求实现一个函数,按输入数据的逆序建立一个链表。

函数接口定义:
struct ListNode *createlist();

函数createlist利用scanf从输入中获取一系列正整数,当读到−1时表示输入结束。按输入数据的逆序建立一个链表,并返回链表头指针。链表节点结构定义如下:

struct ListNode {
int data;
struct ListNode *next;
};

裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>

struct ListNode {
int data;
struct ListNode *next;
};

struct ListNode *createlist();

int main()
{
struct ListNode *p, *head = NULL;

head = createlist();
for ( p = head; p != NULL; p = p->next )
    printf("%d ", p->data);
printf("\n");

return 0;

}

/* 你的代码将被嵌在这里 */

输入样例:
1 2 3 4 5 6 7 -1

输出样例:
7 6 5 4 3 2 1

struct ListNode *createlist()
{
    struct ListNode *head,*p;
    head=(struct ListNode *)malloc(sizeof(struct ListNode));
    head->next=NULL;
    int x;
    scanf("%d",&x);
    while(x!=-1)
    {
        p=(struct ListNode *)malloc(sizeof(struct ListNode));
        p->data =x;
        p->next =head->next;
        head->next=p;
        scanf("%d",&x);
    }
    head=head->next ; 
    return head;
}

6-4 链表拼接 (20分)

本题要求实现一个合并两个有序链表的简单函数。链表结点定义如下:

struct ListNode {
int data;
struct ListNode *next;
};

函数接口定义:
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2);

其中list1和list2是用户传入的两个按data升序链接的链表的头指针;函数mergelists将两个链表合并成一个按data升序链接的链表,并返回结果链表的头指针。

裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>

struct ListNode {
int data;
struct ListNode *next;
};

struct ListNode *createlist(); /裁判实现,细节不表/
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2);
void printlist( struct ListNode *head )
{
struct ListNode *p = head;
while § {
printf("%d “, p->data);
p = p->next;
}
printf(”\n");
}

int main()
{
struct ListNode *list1, *list2;

list1 = createlist();
list2 = createlist();
list1 = mergelists(list1, list2);
printlist(list1);

return 0;

}

/* 你的代码将被嵌在这里 */

输入样例:
1 3 5 7 -1
2 4 6 -1

输出样例:
1 2 3 4 5 6 7


struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2)
{
	struct ListNode *head,*p,*q,*s;
	p=head=list1 ;
	//p=q=head->next ;
	int min;
    if(list1==NULL&&list2==NULL)
        return NULL;
	while(list1->next )
		list1=list1->next ;
	//list2=list2->next ;
	list1->next =list2;
	for(;p!=NULL;p=p->next  )
	{
		min=p->data ;
		for(q=p;q!=NULL; q=q->next  )
			if(min>q->data )
			{
				min=q->data ;
				s=q;
			}
		if(min==p->data )
			continue;
			else
			{
				s->data =p->data ;
				p->data =min;
			}
		//	printf("%d***",p->data );
	}
	//head=head->next ;
	return head;
}

6-5 奇数值结点链表 (20分)

本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中奇数值的结点重新组成一个新的链表。链表结点定义如下:

struct ListNode {
int data;
ListNode *next;
};

函数接口定义:
struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );

函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。

函数getodd将单链表L中奇数值的结点分离出来,重新组成一个新的链表。返回指向新链表头结点的指针,同时将L中存储的地址改为删除了奇数值结点后的链表的头结点地址(所以要传入L的指针)。

裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>

struct ListNode {
int data;
struct ListNode *next;
};

struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );
void printlist( struct ListNode *L )
{
struct ListNode *p = L;
while § {
printf("%d “, p->data);
p = p->next;
}
printf(”\n");
}

int main()
{
struct ListNode *L, *Odd;
L = readlist();
Odd = getodd(&L);
printlist(Odd);
printlist(L);

return 0;

}

/* 你的代码将被嵌在这里 */

输入样例:
1 2 2 3 4 5 6 7 -1

输出样例:
1 3 5 7
2 2 4 6

/* 你的代码将被嵌在这里 */
struct ListNode *readlist()
{
		struct ListNode *head,*pNew,*r;
	head=(struct ListNode *)malloc(sizeof(struct ListNode));
	head->next =NULL;
	r=head;
	int x;
		scanf("%d",&x);
	while(x!=-1)
	{
		pNew=(struct ListNode *)malloc(sizeof(struct ListNode));
		pNew->data =x;
		r->next =pNew;
		r =pNew;
		scanf("%d",&x);
	}
	r->next =NULL;
	head=head->next ;
	return head; 
}
struct ListNode *getodd( struct ListNode **L )
{
	struct ListNode *head1,*head2,*p,*q,*a,*b;
	a=(struct ListNode *)malloc(sizeof(struct ListNode));
	b=(struct ListNode *)malloc(sizeof(struct ListNode));
	head1=a,head2=b;
	while(*L)
	{
		if(((*L)->data)%2 )
			{
				a->next=(*L);
				a=(*L)	;
			}
		else
		{
			b->next =(*L);
			b=(*L);
		}
        *L=(*L)->next;
	}
	a->next =NULL;
	b->next =NULL;
    head2=head2->next;
    head1=head1->next;
	*L=head2;
	return head1;
}

6-6 单链表结点删除 (20分)

本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中所有存储了某给定值的结点删除。链表结点定义如下:

struct ListNode {
int data;
ListNode *next;
};

函数接口定义:
struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );

函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。

函数deletem将单链表L中所有存储了m的结点删除。返回指向结果链表头结点的指针。

裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>

struct ListNode {
int data;
struct ListNode *next;
};

struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
void printlist( struct ListNode *L )
{
struct ListNode *p = L;
while § {
printf("%d “, p->data);
p = p->next;
}
printf(”\n");
}

int main()
{
int m;
struct ListNode *L = readlist();
scanf("%d", &m);
L = deletem(L, m);
printlist(L);

return 0;

}

/* 你的代码将被嵌在这里 */

输入样例:
10 11 10 12 10 -1
10

输出样例:
11 12

/* 你的代码将被嵌在这里 */
struct ListNode *readlist()
{
		struct ListNode *head,*pNew,*r;
	head=(struct ListNode *)malloc(sizeof(struct ListNode));
	head->next =NULL;
	r=head;
	int x;
		scanf("%d",&x);
	while(x!=-1)
	{
		pNew=(struct ListNode *)malloc(sizeof(struct ListNode));
		pNew->data =x;
		r->next =pNew;
		r =pNew;
		scanf("%d",&x);
	}
	r->next =NULL;
	//head=head->next ;
	return head; 
}
struct ListNode *deletem( struct ListNode *L, int m )
{
	struct ListNode	*head,*p,*q,*pre;
	pre=head=p=L;
	p=p->next ;
	while(p)
	{
		q=p->next ;
		if(m==p->data )
		{
			pre->next =q;
			p=q ;
		}
		else
		{
			pre=p;
			p=q;
		}
	}
	head=head->next ;
	return head;
}

6-7 链表逆置 (20分)

本题要求实现一个函数,将给定单向链表逆置,即表头置为表尾,表尾置为表头。链表结点定义如下:

struct ListNode {
int data;
struct ListNode *next;
};

函数接口定义:
struct ListNode *reverse( struct ListNode *head );

其中head是用户传入的链表的头指针;函数reverse将链表head逆置,并返回结果链表的头指针。

裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>

struct ListNode {
int data;
struct ListNode *next;
};

struct ListNode *createlist(); /裁判实现,细节不表/
struct ListNode *reverse( struct ListNode *head );
void printlist( struct ListNode *head )
{
struct ListNode *p = head;
while § {
printf("%d “, p->data);
p = p->next;
}
printf(”\n");
}

int main()
{
struct ListNode *head;

head = createlist();
head = reverse(head);
printlist(head);

return 0;

}

/* 你的代码将被嵌在这里 */

输入样例:
1 2 3 4 5 6 -1

输出样例:
6 5 4 3 2 1

struct ListNode *reverse( struct ListNode *head )
{
	struct ListNode *L,*p,*q;
    if(head==NULL)
        return NULL;
    if(head->next==NULL)
        return head;
	L=(struct ListNode *)malloc(sizeof(struct ListNode ));
	L->next =NULL;
	p=head;
	while(p)
	{
        q=p->next;
		p->next =L->next ;
		L->next =p;
		p=q;	
	}
	L=L->next ;
	return L;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值