سلام میخوام تابعی بسازم که گره ای که از ورودی اطلاعاتشوگرفته حذف کنه - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

سلام میخوام تابعی بسازم که گره ای که از ورودی اطلاعاتشوگرفته حذف کنه

0 امتیاز
//linked list inserting a node in the begginging;

#include <iostream>
using namespace std;

struct node
{
char esm[30];
node* next;

};

void print(node* head)
{
for (; head; head = head->next)
{
cout << head->esm << endl;
}
}

node* ezafekardan_niro(node* head)
{
node* niro = new node;
niro->next = NULL;
cout << "esm"<<endl;
cin >> niro->esm;
niro->next = head;
head = niro;
return head;

}

node *hazf_niro(node *head)

int main()
{

node* head = NULL;

unsigned short tedad_karkonan = 0;


cin >> tedad_karkonan;

cout << endl;

for (unsigned short i = 0; i < tedad_karkonan; i++)
{
head = ezafekardan_niro(head);

cout << endl;
}

cout << "LIST afrad: " << endl;
cout << endl;

print(head);





return 0;
}

 

 

سوال شده آذر 16, 1399  بوسیله ی yasi (امتیاز 28)   1 4 6
ویرایش شده آذر 21, 1399 بوسیله ی farnoosh

1 پاسخ

+1 امتیاز
 
بهترین پاسخ
//define struct student by name by linked list
struct student
{
    char name[20];
    int age;
    struct student *next;
};
//define function to add student
void add_student(struct student **head, char name[], int age)
{
    struct student *new_student = (struct student *)malloc(sizeof(struct student));
    strcpy(new_student->name, name);
    new_student->age = age;
    new_student->next = *head;
    *head = new_student;
}
//define function to delete student
void delete_student(struct student **head, char name[])
{
    struct student *temp = *head;
    struct student *prev = NULL;
    while (temp != NULL)
    {
        if (strcmp(temp->name, name) == 0)
        {
            if (prev == NULL)
            {
                *head = temp->next;
            }
            else
            {
                prev->next = temp->next;
            }
            free(temp);
            break;
        }
        prev = temp;
        temp = temp->next;
    }
}

//add some students to linked list and delete some students
void main(){
    struct student *head = NULL;
    add_student(&head, "John", 20);
    add_student(&head, "Jane", 21);
    add_student(&head, "Jack", 22);
    add_student(&head, "Jill", 23);
    add_student(&head, "Joe", 24);
    delete_student(&head, "Jane");
    struct student *temp = head;
    while (temp != NULL)
    {
        printf("%s %d\n", temp->name, temp->age);
        temp = temp->next;
    }
}

 

پاسخ داده شده تیر 19, 1401 بوسیله ی copilot (امتیاز 1,549)   1 3 6
انتخاب شد تیر 24, 1401 بوسیله ی مصطفی ساتکی
...