大众信息网

关于C语言单向链表,编写一个主函数,要求用函数实现如下功能:

答案:2  mip版
解决时间 2021-01-17 00:45
  • 提问者网友:童話被染上了傷
  • 2021-01-16 18:52
(1) 随机产生或键盘输入一组元素序列,建立一个带头结点的单向链表(无序);
(2) 遍历单向链表(即显示顺序表);
(3) 把单向链表中元素逆置(不允许申请新的结点空间);
(4) 在单向链表中删除所有的偶数元素结点;
最佳答案
  • 二级知识专家网友:淡似春风
  • 2021-01-16 19:32
#include
using namespace std;
class Node {
public:
    int data;
    Node* next;
    Node(int _data) {
        data = _data;
        next = NULL;
    }
};
class LinkList {
private:
    Node* head;
public:
    LinkList() {
        head = NULL;
    }
    void insert(Node *node, int index) {
        if (head == NULL) {
            head = node;
            return;
        }
        if (index == 0) {
            node->next = head;
            head = node;
            return;
        }
        Node *current_node = head;
        int count = 0;
        while (current_node->next != NULL && count < index - 1) {
            current_node = current_node->next;
            count++;
        }
        if (count == index - 1) {
            node->next = current_node->next;
            current_node->next = node;
        }
    }
    void output() {
        if (head == NULL) {
            return;
        }
        Node *current_node = head;
        while (current_node != NULL) {
            cout << current_node->data << " ";
            current_node = current_node->next;
        }
        cout << endl;
    }
    void delete_node(int index) {
        if (head == NULL) {
            return;
        }
        Node *current_node = head;
        int count = 0;
        if (index == 0) {
            head = head->next;
            delete current_node;
            return;
        }
        while (current_node->next != NULL && count < index -1) {
            current_node = current_node->next;
            count++;
        }
        if (count == index - 1 && current_node->next != NULL) {
            Node *delete_node = current_node->next;
            current_node->next = delete_node->next;
            delete delete_node;
        }
    }
    void reverse(){
        if(head == NULL){
            return;
        }

        Node *next_node,*current_node;
        current_node = head->next;
        head->next = NULL;
        while(current_node != NULL){
            next_node = current_node->next;
            current_node->next = head;
            head = current_node;
            current_node = next_node;

        }

    }

};
int main() {
    LinkList linklist;
    for (int i = 1; i <= 10; i++) {
        Node *node = new Node(i);
        linklist.insert(node, i - 1);
    }
    linklist.output();
    linklist.delete_node(3);
    linklist.output();
    linklist.reverse();
    linklist.output();
    return 0;
}
全部回答
  • 1楼网友:有你才幸福
  • 2021-01-16 19:46

#include <stdio.h> #include <malloc.h>

#define n 5 typedef struct lnode {  double data;  long number;  struct lnode *next; }lnode,*linklist;

linklist fun(linklist l,double *aver) {  linklist p,q,h;  double sum=0;  p=l->next;  while(p!=null)  {   sum+=p->data;   p=p->next;  }  sum=sum/n;  *aver = sum;  printf("aver=%.2f\n",*aver);  p=l->next;  h=(linklist)malloc(sizeof(lnode));  h->next = null;  while(p!=null)  {   if(p->data>=sum)   {    q=(linklist)malloc(sizeof(lnode));    q->number = p->number;    q->data=p->data;    q->next = h->next;    h->next = q;      }   p=p->next;  }  return h; } void main() {  linklist l,p,h;  int i;  double aver;  l= (linklist)malloc(sizeof(lnode));  l->next = null;  printf("创建链表...\n输入学生的学号和成绩:\n");  for( i=0;i<n;i++)//逆位序输入n个元素的值,建立带头结点的单链表l  {   p=(linklist)malloc(sizeof(lnode));   scanf("%d %lf",&p->number,&p->data);   p->next=l->next;   l->next=p;  }  h=fun(l,&aver);  printf("平均成绩为:%.2f\n",aver);  p=h->next;  printf("大于或等于平均成绩的学生信息...\n");  printf("学号    成绩\n");  while(p!=null)  {   printf("%-12d  %-3.2f\n",p->number,p->data);   p=p->next;  }  printf("\n"); }

我要举报
如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
点此我要举报以上问答信息
推荐资讯