6-1 建立学生信息链表
本题要求实现一个将输入的学生成绩组织成单向链表的简单函数。
参考代码:
void input() {
int num,score;
head = (struct stud_node*)malloc(sizeof(struct stud_node));
struct stud_node *p = head;
//注意分开读取数据
while(scanf("%d",&num)!=EOF&&num!=0) {
tail = (struct stud_node*)malloc(sizeof(struct stud_node));
scanf("%s%d", tail->name, &tail->score);
tail->num = num;
p->next = tail;
p = p->next;
}
head = head->next;
}
6-2 学生成绩链表处理
本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。
参考代码:
struct stud_node *createlist(){
int num,score;
struct stud_node *head = (struct stud_node*)malloc(sizeof(struct stud_node));
struct stud_node *p = head, *tail;
while(scanf("%d",&num)!=EOF&&num) {
tail = (struct stud_node*)malloc(sizeof(struct stud_node));
scanf("%s%d", tail->name, &tail->score);
tail->num = num;
tail->next = NULL;
p->next = tail;
p = p->next;
}
head = head->next;
return head;
}
struct stud_node *deletelist(struct stud_node *head, int min_score) {
struct stud_node *p, *q;
p = head;
while(head&&head->score<min_score) {
p = head->next;
free(head);
head = p;
}
if(NULL == head) return NULL;
while(p->next) {
if(p->next->score<min_score) {
q = p->next;
p->next=q->next;
free(q);
}else {
p = p->next;
}
}
return head;
};
6-3 逆序数据建立链表
本题要求实现一个函数,按输入数据的逆序建立一个链表。
参考代码:
struct ListNode *createlist() {
struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode)), *p, *tail=NULL;
int num = 0;
while(scanf("%d",&num)!=EOF&&num!=-1) {
//每次将读取到的元素插入到头节点的下一位,如果下一位村
p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->data = num;
if(NULL==tail) {
tail = p;
tail->next = NULL;
head->next = tail;
}
else {
head->next = p;
p->next = tail;
tail = p;
}
}
return head->next;
};
6-4 链表拼接
本题要求实现一个合并两个有序链表的简单函数。
参考代码及思路
//可利用归并排序的merge操作思想,融合两个有序链表
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2){
struct ListNode *p1=list1, *p2=list2, *q;
struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode)), *head_p=head;
while(p1&&p2) {
if(p1->data<p2->data) {
head_p->next = p1;
head_p = head_p->next;
p1 = p1->next;
}
else {
head_p->next = p2;
head_p = head_p->next;
p2 = p2->next;
}
}
while(p1) {
head_p->next = p1;
head_p = head_p->next;
p1 = p1->next;
}
while(p2) {
head_p->next = p2;
head_p = head_p->next;
p2 = p2->next;
}
return head->next;
}
6-5 奇数值结点链表
本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中奇数值的结点重新组成一个新的链表。
struct ListNode *readlist() {
int data;
struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode)), *q = head;
while(scanf("%d",&data)!=EOF&&data!=-1) {
struct ListNode *p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->data = data;
p->next = NULL;
q->next = p;
q = q->next;
}
return head->next;
}
struct ListNode *getodd( struct ListNode **L ) {
struct ListNode *p = *L, *odd_head, *odd_p, *odd_q;
odd_head = (struct ListNode*)malloc(sizeof(struct ListNode));
odd_p = odd_head;
while(*L&&(*L)->data&1) {
odd_p->next = *L;
odd_p = odd_p->next;
*L = (*L)->next;
}
if((*L)==NULL) return odd_head->next;
p = *L;
while(p->next) {
if(p->next->data&1) {
odd_p->next = p->next;
odd_p = odd_p->next;
p->next = odd_p->next;
odd_p->next = NULL;
}
else {
p = p->next;
}
}
return odd_head->next;
}
6-6 单链表结点删除
本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中所有存储了某给定值的结点删除。
struct ListNode *readlist() {
int data;
struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode)), *q = head;
while(scanf("%d",&data)!=EOF&&data!=-1) {
struct ListNode *p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->data = data;
p->next = NULL;
q->next = p;
q = q->next;
}
return head->next;
}
struct ListNode *deletem( struct ListNode *L, int m ) {
struct ListNode *p = L,*q;
while(p&&(p->data==m)) {
q = p;
p = q->next;
free(q);
}
struct ListNode *head = p;
if(head == NULL) return NULL;
while(p->next) {
if(p->next->data==m) {
q = p->next;
p->next = q->next;
free(q);
}
else p = p->next;
}
return head;
}
6-7 链表逆置
本题要求实现一个函数,将给定单向链表逆置,即表头置为表尾,表尾置为表头。
struct ListNode *reverse( struct ListNode *head ) {
struct ListNode *p,*q=NULL,*r=NULL;
if(head==NULL) {
return NULL;
}
if(head->next==NULL) {
return head;
}
p=head;
while(p) {
q = p->next; //用q指向p后面的一个节点
p->next = r; //把q指向的那个节点想在转换成指向它前面的那个节点,这个时候就实现了逆序,而且是就地逆序
r = p; //r向后移动到p的位置
p = q; //sp向后移动到q的位置,这时候完成了第一步的置序,后置程序依次循环,直到p到达结尾
}
return r;
}
6-8 统计专业人数
本题要求实现一个函数,统计学生学号链表中专业为计算机的学生人数。
int countcs( struct ListNode *head ){
int cnt = 0;
struct ListNode *p = head;
while(p) {
if(p->code[1]=='0'&&p->code[2]=='2')
cnt++;
p = p->next;
}
return cnt;
}
6-9 删除单链表偶数节点
本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。
struct ListNode *createlist() {
int data;
struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode)), *q = head;
while(scanf("%d",&data)!=EOF&&data!=-1) {
struct ListNode *p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->data = data;
p->next = NULL;
q->next = p;
q = q->next;
}
return head->next;
}
struct ListNode *deleteeven( struct ListNode *head ) {
struct ListNode *p = head,*q;
while(p&&!(p->data&1)) {
q = p;
p = q->next;
free(q);
}
head = p;
if(head == NULL) return NULL;
while(p->next) {
if(!(p->next->data&1)) {
q = p->next;
p->next = q->next;
free(q);
}
else p = p->next;
}
return head;
}