1. Single-Link list code: includes the tail interpolation method, insert, delete operation.
A single linked list with a head node is also easy to insert and delete in the first position, and does not require additional discussion.
#include <stdio.h>
#include <stdlib.h>
typedef struct LINKLIST
{
int key;
Linklist *next;
}linklist;
linklist* Create_end ()
{
Linklist *head= (linklist *) malloc (sizeof (linklist));
Linklist *e,*p;
E=head;
printf ("Input value? ends with # \ n");
int ch;
while ((scanf ("%d", &ch)) ==1)
{
p= (linklist *) malloc (sizeof (linklist));
p->key=ch;
e->next=p;
E=p;
}
e->next=null;
printf ("done\n");
Return head;//This returns the head node instead of the Head->next, which is intended to be inserted and deleted at the first position for subsequent insertions and deletions
}
void Add (linklist* head)
{
Linklist *q=head;
Linklist *p= (linklist*) malloc (sizeof (linklist));
int ch;
printf ("Where to insert:");
Fflush (stdin);
scanf ("%d", &ch);
int i=1;//i to start from 1
while (I!=CH)
{
q=q->next;
i++;
}
p->next=q->next;
q->next=p;
printf ("What is the data to insert:");
Fflush (stdin);
scanf ("%d", &ch);
p->key=ch;
}
void delete_s (linklist* head)
{
printf ("To delete the first few locations:");
int ch;
Fflush (stdin);
scanf ("%d", &ch);
Linklist *p=head;
Linklist *q=p->next;
int i=1;
while (I!=CH)
{
q=q->next;
p=p->next;
i++;
}
p->next=q->next;
printf ("Deleted%d", Q->key);
Delete q;
}
void Show_linklist (linklist* head)
{
linklist* p=head->next;//In order not to output the value of the head node to start from Head->next.
while (P->next!=null)
{
printf ("%d->", P->key);
p=p->next;
}
printf ("%d", p->key);
}
int main (void)
{
Linklist *head=new linklist;
Head=create_end ();
Add (head);
Show_linklist (head);
Delete_s (head);
Show_linklist (head);
return 0;
}
Results show:
2. Two-way loop linked list: include tail plug, insert, delete
In this article, the Loop link list contains the head node. The advantage of this is that it is easy to insert and delete in the first place
Insert operation:
Note: always use one of the nodes behind the head node as the benchmark pointer.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct LINKLIST
{
int key;
Linklist *prior;
Linklist *next;
}linklist;
linklist* Create_end ()
{
Linklist *head= (linklist *) malloc (sizeof (linklist));
head->key=0;
Linklist *e,*p;
E=head;
printf ("Input value? ends with # \ n");
int ch;
while ((scanf ("%d", &ch)) ==1)
{
p= (linklist *) malloc (sizeof (linklist));
p->key=ch;
e->next=p;
p->prior=e;
E=p;
}
e->next=head;
head->prior=e;
printf ("done\n");
return head;
}
void Add (linklist* head)
{
linklist *p=head->next;
Linklist *r= (linklist*) malloc (sizeof (linklist));
int ch;
printf ("Where to insert:");
Fflush (stdin);
scanf ("%d", &ch);
int i=1;
while (I!=CH)
{
p=p->next;
i++;
}
printf ("What is the data to insert:");
scanf ("%d", &ch);
r->key=ch;
r->next=p;//, note the wording here.
r->prior=p->prior;
r->prior->next=r;
p->prior=r;
}
void delete_s (linklist* head)
{
Linklist *p=head->next;//here to have next
if (P->next==head)
{
printf ("Cannot delete, already empty!\n");
}
printf ("To delete the first few locations:");
int ch;
Fflush (stdin);
scanf ("%d", &ch);
int i=1;
while (I!=CH)
{
p=p->next;
i++;
}
p->prior->next=p->next;
p->next->prior=p->prior;
printf ("Deleted%d\n", P->key);
Delete p;
}
void Show_linklist (linklist* head)
{
Linklist *p=head->next;
while (P->next!=head)
{
printf ("%d->", P->key);
p=p->next;
}
printf ("%d", p->key);
}
int main (void)
{
Linklist *head=new linklist;
Head=create_end ();
Add (head);
Show_linklist (head);
Delete_s (head);
Show_linklist (head);
return 0;
}
"Algorithm design-linked list" single linked list and two-way circular linked list