| |

VerySource

 Forgot password?
 Register
Search
View: 730|Reply: 3

Ask you experts: the problem of data structure

[Copy link]

2

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-21 20:20: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.
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-31 21:54:01
| Show all posts
This question is a bit big ...
Reply

Use magic Report

0

Threads

24

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-2 12:27:02
| Show all posts
Add: Iterate node by node until the correct insertion position is found.
Deletion: Iterate node by node until you find the node that needs to be deleted.

These are some basic pointer operations. There is nothing complicated. Try it for yourself.
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-2-3 19:15:01
| Show all posts
// Linear list insertion and deletion

#include <stdio.h>
#include <malloc.h>
typedef struct LNode
 {int data;
  struct LNode * next;
  } LNode, * linklist;
int Init (linklist * L)
 (* L = (linklist) malloc (sizeof (LNode));
  (* L)-> next = NULL;
  return 1;
 }
int insert (linklist L, int i, int e)
 {linklist p, s;
  int j = 0;
   p = L;
  while (p&&j <i-1)
  {p = p-> next; j ++;}
   if (! p || j> i-1)
    return 0;
   s = (linklist) malloc (sizeof (LNode));
    s-> data = e;
    s-> next = p-> next;
    p-> next = s;
    return 1;
  }
int del (linklist L, int i, int * e)
 {linklist p, q;
  int j = 0;
  p = L;
  while (p-> next&&j <i-1)
  {p = p-> next; j ++;}
  if (! p-> next || j> i-1)
  return 0;
     q = p-> next;
      p-> next = q-> next;
      * e = q-> data;
      printf ("The deleted number:% d\n\n", * e);
   free (q);
   return 1;
 }
void print (linklist L)
{linklist p;
 for (p = L-> next; p; p = p-> next)
  printf ("% 4d", p-> data);
 printf ("\n");
}
void main ()
{linklist L, p;
 int i, e, j, t;
 t = Init (&L);
 printf ("% d\n\n", t);
 for (i = 1; i <= 5; i ++)
  {scanf ("% d",&e);
   insert (L, i, e);
  }
  print (L);
  printf ("\n");
  printf ("Enter the delete seat:");
  scanf ("% d",&j);
  printf ("\n");
  del (L, j,&e);
  print (L);
  printf ("\n\n");
  }
Reply

Use magic Report

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

Points Rules

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

Quick Reply To Top Return to the list