| |

VerySource

 Forgot password?
 Register
Search
Author: hxf1982

A data structure problem (data written test questions)

[Copy link]

0

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-4-7 00:00:01
| Show all posts
struct node
{
char * name;
struct node * next;
};

struct node * Head;

Head-> name = NULL;
Head-> next = NULL;

void insert (struct node * newnode)
{
struct node * p, * q;
p = Head;
q = p;
if (p-> next == NULL)
{
p-> next = newnode;
newnode-> next = NULL;
}
else
{
while (p-> next! = NULL || strcmp (p-> name, newnode-> name) <0)
{
q = p;
p = p-> next;
}
q-> next = newnod;
newnod-> next = q;
}
return;
}

void del (struct node * oldnode)
{
struct node * p, * q;
p = Head;
if (p-> next == NULL)
{
printf ("there is no data\n");
exit (1);
}
else
{
while (p-> next! = NULL || strcmp (p-> name, oldnode-> name)! = 0)
{
q = p;
p = p-> next;
}
q-> next = p-> next;
delete (p);
}
return;
}

The brother who just wrote helps find errors

Is there a good algorithm for sequential search?
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-4-11 21:00:01
| Show all posts
Upstairs, in your insert (), you don't seem to consider the case where Head is NULL?
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-4-12 12:15:01
| Show all posts
Looks like too many errors :)
Don't think about a good algorithm, the linked list can only be searched sequentially
Reply

Use magic Report

0

Threads

3

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-4-14 12:30:01
| Show all posts
mark
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-4-15 13:00:02
| Show all posts
Is it very simple, first find the node before the position to be inserted, and then insert or delete it, as long as you carefully consider the special circumstances of the head and tail, it is ok
Reply

Use magic Report

0

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-4-16 18:00:02
| Show all posts
There are really a lot of problems, I still have to debug it all at once.

1.Head-> name = NULL;
  Head-> next = NULL;
2.q-> next = newnod;
  newnod-> next = q;
3.delete (p);

It's been a long time since I used C language ~
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