/*-------------------------include header file------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
int count=0;
/*-------------------------Structure Definition part------------------------------*/
typedef struct NODE
{
Char name[10];
int score;
struct Node *next;
} ListNode;
/*----------------------------function Declaration Part------------------------------*/
/*---------------------------Function Implementation part-------------------------------*/
/*-----------------------------Create a linked list---------------------------------*/
/* Insert a new node at the end of the list to create a linked list */
ListNode *createlist ()
{
ListNode *head;//point Pointer to head node
ListNode *p,*pre;
Head= (ListNode *) malloc (sizeof (ListNode));//Allocate memory space for head node
head->next=null;//Empty the pointer field of the head node
pre=head;//first address of the first node to the intermediate variable pre.
while (1)
{
if ((p= (ListNode *) malloc (sizeof (ListNode))) ==null)
{
printf ("Insufficient memory space!!! \ n ");
Break
}
count++;
printf ("Input name of the%d student (input \" q\ "to quit):", count);//print out the name of the first few people
Memory space P points to the first address of the newly inserted node.
scanf ("%s", &p->name);//Enter Name
if (strcmp (p->name, "Q") ==0)
Break
printf ("Input score of the%d student:", count);
scanf ("%d", &p->score);//input fraction
pre->next=p;//points the p to the new node insert list that is the head node pointer field pointing
Next knot.
The first node is P pointing, because the header content is empty.
pre=p;//this role in pointing to the next junction.
pre->next=null;
}
Return head;//returns the first address of the list
}
/*-------------------------Output list-----------------------------------*/
void Printlist (ListNode *h)
{
ListNode *p;
p=h->next;
while (p)
{
printf ("%s,%d", P->name,p->score);
p=p->next;
printf ("\ n");
}
}
/*----------------------insert a linked table node--------------------------*/
/*--------------------------------------------------------------------
Function name: insertlist (listnode *h,int i,char name[],int e,int N)
function function: Inserting a linked list node
Entry parameter: H: Head node address I: Insert to the first node name: Insert node name e: Insert node's score N: Number of nodes in the list except the head node
Export parameters:
--------------------------------------------------------------------*/
void Insertlist (ListNode *h,int i,char name[],int e,int N)
{
ListNode *p,*q; First, define 2 pointers to a node.
if (i<1| | I>N+1)
printf ("Error!!! \ n ");
Else
{
int j=1;
p=h;//pointing the pointer p to the head node of the list to be linked.
while (J<i)
{
p=p->next;
j + +;
}
if ((q= (ListNode *) malloc (sizeof (ListNode)))/* * for the inserted
node Allocation memory space */
printf ("Insufficient memory space!!! \ n ");
Else
{
count++;
strcpy (q->name,name);//Copy the name to the node you want to insert
q->score=e;//the fractional assignment in the node that will be inserted
q->next=p->next;/* this is to point to the newly inserted node pointer field
The node address pointed to by the last node pointer field is p->next*/.
p->next=q;/* the node pointer field that will be inserted before the node position
Point to the first address of the node that is now inserted */
}
}
}
/*--------------------------------------------------------------------
Function name: DeleteList (listnode *h, int i, int n)
function function: Delete linked list node
Entry parameter: H: Head node Address I: the node to be deleted location N: The number of nodes in the list except the first node
Export parameters:
--------------------------------------------------------------------*/
void DeleteList (ListNode *h, int i, int n)
{
ListNode *p,*q;//first defines 2 pointers to node-type structures
Char name[10];
int score;
if (i<1| | I>COUNT+1)
printf ("Error!!! \ n ");
Else
{
int j=1;
p=h;//Pointer to the first address of the list's head node
while (J<i)
{
p=p->next;
j + +;
}
Q=p->next;/*q points to the node before the location you want to delete.
The node that the address Q points to is the node to be deleted */
p->next=q->next;/* This is the node in front of the node that will be deleted.
Pointer field to the next node stored in the node pointer field to be deleted.
The first address thus achieves the deletion of the first node.
strcpy (Name,q->name);
score=q->score;
Free (q);//Release Q-point node
printf ("name:%s\tscore:%d has been deleted!!! \ n ", Name,score);
}
}
/*--------------------------Main function-------------------------------*/
int main ()
{
ListNode *h;//h point to struct node
int i = 1, n, score;
char name [10];
while (i)
{
/* Enter a message */
printf ("Create a new link list \ n");
printf ("2--add element \ n");
printf ("3--delete element \ n");
printf ("4--output the element in the current table \ n");
printf ("0--exit \ n");
scanf ("%d", &i);
Switch (i)
{
Case 1:
H=createlist ();/* Create a linked list */
printf ("list elements is: \ n");
Printlist (h);
Break
Case 2:
printf ("Input the position. Of insert element: ");
scanf ("%d", &i);
printf ("Input name of the student:");
scanf ("%s", name);
printf ("Input score of the student:");
scanf ("%d", &score);
Insertlist (H,i,name,score,count);
printf ("list elements is:\n");
Printlist (h);
Break
Case 3:
printf ("Input the position of the delete element:");
scanf ("%d", &i);
DeleteList (H,i,count);
printf ("list elements in: \ n");
Printlist (h);
Break
Case 4:
printf ("list element is: \ n");
Printlist (h);
Break
Case 0:
Return
Break
Default
printf ("error! Try again!\n ");
}
}
return 0;
}