第11讲 链表 单元作业

发布时间 2023-12-21 08:33:50作者: YANTARES
1. 

编写函数deln,具有删除链表中第n个结点的功能。再编写主函数,按输入顺序建立不带头结点的职工信息单链表,然后调用del函数删除某个职工的信息,并输出删除后的职工信息表中的职工信息。假设链表结构如下:

struct staff

{

  char    num[6];      //职工工号

  char    name[20];    //职工姓名

  double  wage;        //职工工资

};

yzy's version:

 

 1 #include "iostream"
 2 #define N 256
 3 using namespace std;
 4 struct node
 5 {
 6     char num[6];      //职工工号
 7     char name[20];    //职工姓名
 8     double wage;        //职工工资
 9     struct node* next;
10 };
11 struct node* create(int n)  //创建
12 {
13     struct node* head = NULL;
14     struct node* tail=nullptr, * newnode;
15     for (int i = 0; i < n; i++)
16     {
17         newnode=new node;
18         cin >> newnode->num >> newnode->name >> newnode->wage;
19         if (head == NULL)
20             head = newnode;
21         else
22             tail->next = newnode;
23         tail = newnode;
24     }
25     tail->next = NULL;
26     return(head);
27 }
28 struct node* deln(node *head,int x)  //删除
29 {
30     struct node* p, * q=nullptr;
31     p = head;
32     if (head == NULL)
33         cout << "The list is null!\n";
34     else
35     {
36         while (p != NULL && (x--)-1)
37         {
38             q = p;
39             p = p->next;
40         }
41         if (p == head)//
42         {
43             head = p->next;
44             delete p;
45         }
46         else if (p != NULL)//
47         {
48             q->next = p->next;
49             delete p;
50         }
51         else//
52             cout << x << " does not exist in the list!";
53     }
54     return head;
55 }
56 void print(struct node* head)  //遍历输出
57 {
58     struct node* p = head;
59     while (p != NULL)
60     {
61         cout << p->num<<' '<<p->name<<' '<<p->wage << '\t';
62         p = p->next;
63     }
64 }
65 
66 int main()
67 {
68     int n,x;
69     node* person,*person1;
70     cout << "请输入职工数:" << endl;;
71     cin >> n;
72     cout << "请输入各职工工号、姓名、工资:" << endl;
73     person=create(n);
74     cout << "删除前:" << endl;
75     print(person);
76     cout << "想开除第几个职工?"<<endl;
77     cin >> x; 
78     person1=deln(person, x);
79     cout << "删除后:" << endl;
80     print(person1);
81     system("pause");
82     return 0;
83 }
View Code

 

good version:

 

#include <iostream>

#include <cstring>

using namespace std;

 

struct staff {

    char num[6]; // 职工工号

    char name[20]; // 职工姓名

    double wage; // 职工工资

    staff* next; // 指向下一个结点的指针

};

 

// 删除链表中的第n个结点

void deln(staff*& head, int n) {

    if (n < 1 || head == nullptr)return;// 检查n的有效性和链表是否为空

    staff* current = head;

    staff* prev = nullptr;

    int count = 1;

    // 如果要删除的是第一个结点

    if (n == 1) {

        head = current->next;

        delete current;

        return;

    }

    // 遍历链表,寻找第n个结点

    while (current != nullptr && count < n) {

        prev = current;

        current = current->next;

        count++;

    }

    //如果找到第n个结点

    if (current != nullptr) {

        prev->next = current->next;

        delete current;

    }

}

// 创建链表

staff* createList() {

    staff* head = nullptr;

    staff* tail = nullptr;

    int count;

    cout << "输入职工数量: ";

    cin >> count;

 

    for (int i = 0; i < count; ++i) {

        staff* newNode = new staff;

        cout << "输入职工" << (i + 1) << "的信息 (工号 姓名 工资): ";

        cin >> newNode->num >> newNode->name >> newNode->wage;

        newNode->next = nullptr;

 

        if (head == nullptr) {

            head = newNode;

        }

        else {

            tail->next = newNode;

        }

        tail = newNode;

    }

    return head;

}

 

// 打印链表

void printList(staff* head) {

    staff* current = head;

    while (current != nullptr) {

        cout << "工号: " << current->num << ", 姓名: " << current->name << ", 工资: " << current->wage << endl;

        current = current->next;

    }

}

 

int main() {

    staff* head = createList();

    int n;

    cout << "输入要删除的职工的序号: ";

    cin >> n;

    deln(head, n);

 

    cout << "删除后的职工信息:\n";

    printList(head);

 

    // 释放内存

    while (head != nullptr) {

        staff* temp = head;

        head = head->next;

        delete temp;

    }

 

    return 0;

}
View Code

 

 

2.

 在上一题目建立的链表中增加一个按工号num修改该职工工资wage的函数。再编写主函数,对某一具体职工的信息进行更改。

yzy's version:

 

  1 #include "iostream"
  2 #define N 256
  3 using namespace std;
  4 struct node
  5 {
  6     char num[6];      //职工工号
  7     char name[20];    //职工姓名
  8     double wage;        //职工工资
  9     struct node* next;
 10 };
 11 struct node* create(int n)  //创建
 12 {
 13     struct node* head = NULL;
 14     struct node* tail = nullptr, * newnode;
 15     for (int i = 0; i < n; i++)
 16     {
 17         newnode = new node;
 18         cin >> newnode->num >> newnode->name >> newnode->wage;
 19         if (head == NULL)
 20             head = newnode;
 21         else
 22             tail->next = newnode;
 23         tail = newnode;
 24     }
 25     tail->next = NULL;
 26     return(head);
 27 }
 28 struct node* deln(node* head, int x)  //删除
 29 {
 30     struct node* p, * q = nullptr;
 31     p = head;
 32     if (head == NULL)
 33         cout << "The list is null!\n";
 34     else
 35     {
 36         while (p != NULL && (x--) - 1)
 37         {
 38             q = p;
 39             p = p->next;
 40         }
 41         if (p == head)//
 42         {
 43             head = p->next;
 44             delete p;
 45         }
 46         else if (p != NULL)//
 47         {
 48             q->next = p->next;
 49             delete p;
 50         }
 51         else//
 52             cout << x << " does not exist in the list!";
 53     }
 54     return head;
 55 }
 56 void print(struct node* head)  //遍历输出
 57 {
 58     struct node* p = head;
 59     while (p != NULL)
 60     {
 61         cout << p->num << ' ' << p->name << ' ' << p->wage << '\t';
 62         p = p->next;
 63     }
 64 }
 65 struct node* fix(struct node* head, char y[], double nw)//更改
 66 {
 67     struct node* p, * q = nullptr;
 68     p = head;
 69     if (head == NULL)
 70         cout << "The list is null!\n";
 71     else
 72     {
 73         while (p != NULL && strcmp(p->num,y)!=0)
 74         {
 75             q = p;
 76             p = p->next;
 77         }
 78         if (p != NULL)
 79         {
 80             p->wage = nw;
 81         }
 82         else//
 83             cout << y << " does not exist in the list!";
 84     }
 85     return head;
 86 }
 87 
 88 int main()
 89 {
 90     int n;
 91     char y[N];
 92     double nw;
 93     node* person, * person1;
 94     cout << "请输入职工数:" << endl;;
 95     cin >> n;
 96     cout << "请输入各职工工号、姓名、工资:" << endl;
 97     person = create(n);
 98     cout << "更改前:" << endl;
 99     print(person);
100     cout << endl;
101     cout << "依次输入工号和新工资:" << endl;
102     cin >> y >> nw;
103     //char* z = &y;
104     person1 = fix(person, y, nw);
105     cout << "更改后:" << endl;
106     print(person1);
107     system("pause");
108     return 0;
109 }
View Code

 

good version:

 

 

3.  

从键盘输入一个字符串,要求将该字符串的内容按输入的相反顺序组织到一个不带表头结点的单链表中。假设输入的字符串为"abcdefg",则组织到链表中的内容为"gfedcba"

yzy's version:

 

 1 #include "iostream"
 2 #define N 256
 3 using namespace std;
 4 struct node
 5 {
 6     char x;
 7     struct node* next;
 8 };
 9 struct node* contr(char s[N], int n)  //反序输入创建
10 {
11     int i;
12     struct node* head = NULL;
13     struct node* tail=nullptr, * newnode;
14     for (i = 0; i < n; i++)
15     {
16         newnode=new node;
17         newnode->x = s[n-i-1];
18         if (head == NULL)
19             head = newnode;
20         else
21             tail->next = newnode;
22         tail = newnode;
23     }
24     tail->next = NULL;
25     return(head);
26 }
27 
28 void print(struct node* head)  //遍历输出
29 {
30     struct node* p = head;
31     while (p != NULL)
32     {
33         cout << p->x;
34         p = p->next;
35     }
36 }
37 int main()
38 {
39     char s[N];
40     cout << "输入一个字符串:" << endl;
41     gets_s(s);
42     int n = strlen(s);
43     struct node* p = contr(s, n);
44     cout << "反序输出:" << endl;
45     print(p);
46     system("pause");
47     return 0;
48 }
View Code

 

good version:

 

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5  
  6 
  7 // 职工信息结构体
  8 
  9 struct Node {
 10 
 11     char data;
 12 
 13     Node* next;
 14 
 15 };
 16 
 17  
 18 
 19 // 创建链表的函数
 20 
 21 Node* createList(const char* str, int length) {
 22 
 23     Node* head = nullptr;
 24 
 25     Node* tail = nullptr;
 26 
 27  
 28 
 29     for (int i = length - 1; i >= 0; --i) {
 30 
 31         Node* newNode = new Node{ str[i], nullptr };
 32 
 33         if (head == nullptr) {
 34 
 35             head = tail = newNode;
 36 
 37         }
 38 
 39         else {
 40 
 41             tail->next = newNode;
 42 
 43             tail = newNode;
 44 
 45         }
 46 
 47     }
 48 
 49  
 50 
 51     return head;
 52 
 53 }
 54 
 55  
 56 
 57  
 58 
 59 // 打印链表的函数
 60 
 61 void printList(Node* head) {
 62 
 63     while (head != nullptr) {
 64 
 65         cout << head->data;
 66 
 67         head = head->next;
 68 
 69     }
 70 
 71 }
 72 
 73 int main() {
 74 
 75     cout << "请输入一个字符串: ";
 76 
 77     const int bufferSize = 256;
 78 
 79     char inputStr[bufferSize];
 80 
 81  
 82 
 83     // 使用cin.getline读取输入字符串
 84 
 85     cin.getline(inputStr, bufferSize);
 86 
 87  
 88 
 89     // 获取实际字符串长度
 90 
 91     int length = cin.gcount() - 1; // 减去末尾的换行符
 92 
 93  
 94 
 95     // 调用函数创建链表
 96 
 97     Node* head = createList(inputStr, length);
 98 
 99  
100 
101     cout << "链表内容(相反顺序): ";
102 
103     // 调用函数打印链表
104 
105     printList(head);
106 
107  
108 
109     // 释放链表内存
110 
111     while (head != nullptr) {
112 
113         Node* temp = head;
114 
115         head = head->next;
116 
117         delete temp;
118 
119     }
120 
121  
122 
123     return 0;
124 
125 }
View Code