Data structure Note 5 lead node single link list

Source: Internet
Author: User

/*
This operation is to take the lead node single linked list operation including Delete insert empty build
Include delete Insert function display function
The purpose is to make a summary of the basic operation of a single-linked list----1
*/
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#define OK 1
#define ERROR-1
#define OVERFLOW-2
#define ENDFLAG 0
typedef struct lnode{
int data;
struct Lnode *next;
}lnode,*linklist;//Defining linked Table nodes
Initialize the list of links
int initsqlist (linklist *head) {
*head= (linklist) malloc (sizeof (Lnode));//define head nodes
if (Null==*head)
Exit (ERROR);//-1 initialization failed
(*head)->next=null;//initialization succeeded
Return OK;//1
}
Empty sentence
int Listempty (linklist head) {
if (Null==head->next)
return OK;
return endflag;//not empty
}
Input i.e. Assignment head interpolation
int Create_list_head (linklist head,int N) {
int i;
Lnode *p=null;
int temp;
if (n<=0)
Return error;//argument not valid, creation failed-1
for (i=n;i>=1;i--) {
p= (Lnode *) malloc (sizeof (Lnode));
if (null==p)
Return overflow;//-2 memory allocation failure causes creation to fail
printf ("Please enter your%d data: \ n", i);
scanf ("%d", &temp);
p->data=temp;
p->next=head->next;//the original first element node becomes the direct successor of the new node
head->next=p;//new node becomes the first element node.
}
Return OK;//1
}
/*
Insertion by tail interpolation
int Create_list_tail (linklist head,int N) {
int i,temp;
Lnode *p=null,*q=head;//Auxiliary Pointer q always points to footer
if (n<=0)
Return error;//-1
for (i=1;i<n;i++) {
p= (Lnode *) malloc (sizeof (Lnode));
if (null==p)
Return overflow;//-2
printf ("Please enter your%d data: \ n", i);
scanf ("%d", &temp);
p->data=temp;
p->next=null;
q->next=p;//inserting new nodes into the footer
q=p;//Auxiliary pointer q points to the new footer
}
Return ok;//created successfully
}
*/
Request length
int Linklist_length (linklist head) {
int len=0;
Lnode *p=head->next;
while (null!=p) {
len++;p =p->next;
}
return Len;
}
Find the location of a data element
int Locateelem (linklist head,int e) {
int count=1;
Lnode *p=head->next;
while (null!=p&&p->data!=e) {
p=p->next;count++;
}
if (null==p)
Return endflag;//0
return count;
}
Insert
int Listinsert (linklist head,int i,int e) {
int Count=0;//count is always the bit order of the P-node
Lnode *p=head,*q=null;
while (count<i+1&&null!=p) {//Search for I-1 nodes
p=p->next;count++;
}
if (count>i-1| | NULL==P)
Return error;//-1
q= (Lnode *) malloc (sizeof (Lnode));
if (null==q)
Return overflow;//-2 insert operation failed due to storage allocation failure
q->data=e;
q->next=p->next;
p->next=q;
Return OK;//1
Insert Successful
}
Delete
int deletlinklist (linklist head,int i,int *e) {
int Count=0;//count is always a bit sequence of p nodes
Lnode *p=head,*q=null;
if (1==listempty (head))//Empty
Return error;//-1
while (count<i-1&&null!=p) {//Find the I-1 node and guarantee the presence of the first node.
p=p->next;count++;
}
if (count>i-1| | NULL==P)
Return overflow;//-2
q=p->next;//let pointer Q point to the first node to be deleted
*e=p->data;
p->next=q->next;//Delete
Free (P);//Release space
Return endflag;//0
Delete Succeeded
}
Show
int Showlinklist (linklist head) {
Lnode *p=head->next;
while (null!=p) {
printf ("%d", p->data);
p=p->next;
}
printf ("\ n");
}
int main () {
Linklist L;
int i,e,l;
if (! Initsqlist (&l)) {
printf ("Initialization failed, press any key to exit:");
Getch ();
Exit (0);
}
if (! Create_list_head) {
printf ("Assignment fails to press any key to exit:");
Getch ();
Exit (0);
}
Showlinklist (L);
printf ("Do you want to perform an insert operation?" 1-yes ");
int j,k;
scanf ("%d", &j);
if (j==1)
Goto Insert;
Else
Goto Delet;
Insert
printf ("Please enter the location you want to insert: \ n");
scanf ("%d", &i);
printf ("Please enter the number you want to insert: \ n");
scanf ("%d", &e);
if (! Listinsert (&l,i,e)) {
printf ("Insert failure Press any key to exit:");
Getch ();
Exit (0);
}
Showlinklist (L);
Delet:
printf ("\ nthe delete operation is implemented?") 1-yes ");
scanf ("%d", &k);
if (k==1)
Goto Yes;
Else
Exit (0);
Yes
printf ("Please enter the location of the number you want to delete: \ n");
scanf ("%d", &l);
if (! Deletlinklist (&l,l)) {
printf ("Delete failed Press any key to exit: \ n");
Getch ();
Exit (0);
}
Showlinklist (L);
printf ("Link list basic operation test has ended, press any key to exit: \ n");
Getch ();
return 0;
}

Data structure Note 5 lead node single link list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.