| |

VerySource

 Forgot password?
 Register
Search
View: 17|Reply: 15

A data structure problem (data written test questions)

[Copy link]

2

Threads

3

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-1-21 21:40:01
| Show all posts |Read mode
struct node {
char * name;
struct node * next;
};
struct node * Head;
Head is the head of the one-way linked list, and is arranged according to the character order of its member variable name.
Write functions to add and remove nodes of type struct node.

I hope that you will give pointers in detail, the data structure I learned very well!
Reply

Use magic Report

0

Threads

3

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-2-7 08:30:01
| Show all posts
struct node insert (struct node * newnode)
{
struct node * p, * q;
p = head;
while (p-> next-> name <newnode-> name) {p = p-> next; q = p;}
q-> next = newnode; newnode-> next = p;
return newnode;
}
Reply

Use magic Report

0

Threads

3

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-2-7 12:00:01
| Show all posts
struct node * delete (struct node * newnode)
{
struct node * p, * q;
p = head;
while (! p&&(p-> next-> name! = newnode-> name)) p = p-> next;
if (! p) {printf ("No such node in the list"); return NULL;}
else {return newnode;}
}
Reply

Use magic Report

0

Threads

3

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-2-7 14:15:02
| Show all posts
Some errors, roughly like this.
Reply

Use magic Report

2

Threads

3

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

 Author| Post time: 2020-2-12 22:15:01
| Show all posts
Thanks everyone!
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-3-2 12:15:01
| Show all posts
typedef struct node * node_ptr;

struct node
{
    element_type element;
    node_ptr next;
};

void sort_list (node_ptr L)
{
    node_ptr p, tmp, min;
    
    for (p = L-> next; p! = NULL; p = p-> next)
    {
        for (tmp = p-> next; tmp! = NULL; tmp = tmp-> next)
        {
            min = NULL;
            if (tmp-> element <p-> element)
            {
                min = tmp;
            }
        }
        
        if (min! = NULL)
        {
            swap (&min-> element,&p-> element);
        }
    }
}
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-3-12 18:00:02
| Show all posts
while (p-> next-> name <newnode-> name)

How can strings be compared like this?

To use strcmp ()
Reply

Use magic Report

0

Threads

13

Posts

12

Credits

Newbie

Rank: 1

Credits
12

 Unknown

Post time: 2020-3-14 13:30:01
| Show all posts
Linked lists are best sorted when inserted, rather than waiting to be sorted together after insertion.
Reply

Use magic Report

0

Threads

7

Posts

8

Credits

Newbie

Rank: 1

Credits
8

 China

Post time: 2020-3-18 18:15:01
| Show all posts
Create a new linked list
Iterate through the current linked list and insert into the new linked list.
Reply

Use magic Report

0

Threads

1

Posts

36

Credits

Newbie

Rank: 1

Credits
36

 China

Post time: 2020-3-18 22:36:16
| Show all posts

Linked lists are best sorted when inserted, rather than waiting to be sorted together after insertion.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list