Home > Back-end >  Ask for help...
Ask for help...

Time:09-22

Data structure, the realization of the singly linked list
Written in C language program, complete the following functions:
(1) the runtime input data, create a singly linked list
(2) can be in any position of the singly linked list to insert the new node
(3) can remove singly linked lists of any one node
(4) in a singly linked list to find the node
(5) output singly linked lists

CodePudding user response:

#include
#include
/*
1. The run time input data
2. Insert a new node at any position
3. Remove any node
4. To find the node
5. The output list
*/
Typedef struct _NODE_
{
int data;
_NODE_ * next;

} NODE, * PNODE;
PNODE head=NULL;
Void print_menu ()
{
Printf (" * * * * * * * * input the corresponding number, then press enter \ n ");
Printf (" * * * * * * * * 1. Insert the node \ n ");
Printf (" * * * * * * * * 2. Delete the node \ n ");
Printf (" * * * * * * * * 3. Find nodes \ n ");
Printf (" * * * * * * * * 4. Of all the node \ n ");
Printf (" * * * * * * * * q. Exit \ n ");

}
Int node_num ()
{
int num=0;
if(! The head)
return num;
PNODE node=head;
While (node)
{
num++;
The node=node - & gt; next;
}
return num;
}
Void insert_node ()
{
int data;
Int num=node_num ();
Int pos=0;
Printf (" please enter to insert data location, and then press enter (% d, % d) : \ n ", 1, num + 1);
The scanf (" % d ", & amp; Pos);
If (pos & gt; Num=+ 1)
Pos=num + 1;
Printf (" please enter to insert data (positive integer), then press enter: \ n ");
The scanf (" % d ", & amp; data);
PNODE node=(PNODE) malloc (sizeof (node));
Node - & gt; Data=https://bbs.csdn.net/topics/data;
Pos -;
If (pos & lt; 0)
pos=0;
PNODE n=head;

PNODE prev=head;
int i=0;
if(! N | | pos==0)
{
Node - & gt; Next=head;
The head=node;
}
The else
{
While (n)
{
i++;
Prev=n;
N=n - & gt; next;
If (I==pos)
{
Prev - & gt; Next=node;
Node - & gt; Next=n;
break;
}
}
}

}
Void find_node ()
{
Printf (" please input node to delete the data values, and then press enter \ n ");
The int data=https://bbs.csdn.net/topics/0;
The scanf (" % d ", & amp; data);
PNODE node=head;
Bool found=false;
While (node)
{
If (node - & gt; Data=data https://bbs.csdn.net/topics/=
{
Printf (" find node values for % d \ n ", data);
Found=true;
break;
}
The node=node - & gt; next;
}
if(! Found)
{
Printf (" found no node values for % d \ n ", data);
}
}
Void delete_node ()
{
Printf (" please input node to delete the data values, and then press enter \ n ");
The int data=https://bbs.csdn.net/topics/0;
The scanf (" % d ", & amp; data);
PNODE node=head;
PNODE prev=head;
While (node)
{
If (node - & gt; Data=data https://bbs.csdn.net/topics/=
{
If (prev)
{
Prev - & gt; Next=node - & gt; next;
Free (node);
}
break;
}
Prev=node;
The node=node - & gt; next;
}
}
Void view_node ()
{
PNODE node=head;
While (node)
{
Printf (" \ n Data: % d ", node - & gt; data);
The node=node - & gt; next;
}
}
Int main ()
{
The head=NULL;
Print_menu ();

While (true)
{
Char c=getchar ();
The switch (c)
{
Case '1' :
Insert_node ();
break;
Case '2' :
Delete_node ();
break;
Case '3' :
Find_node ();
break;
Case '4' :
View_node ();
break;
Case 'q:
return 0;
Default:
break;
}
}

return 0;
}
  • Related