Create, add, and delete a single-chain table

Source: Internet
Author: User

# Include <unistd. h>
# Include <stdlib. h>
# Include <stdio. h>

Typedef struct lnode
{
Int data;
Struct lnode * next;
} * Lnode;

// Init the single linked list
Void init_l (struct lnode * P)
{
Int N;
Int I = 0;
Lnode Q;
Lnode linklist;
// P = (lnode) malloc (sizeof (lnode ));
If (P = NULL)
{
Puts ("error! ");
Exit (0 );
}
Linklist = P;
While (P)
{
// I ++;
Scanf ("% d", & N );
If (n =-1)
Break;
Q = (lnode) malloc (sizeof (lnode ));
P-> DATA = N;
P-> next = Q;
P = Q;
I ++;
}
Q-> next = NULL;
For (I; I> 0; I --)
{
Printf ("/n % d/N", linklist-> data );
Linklist = linklist-> next;
}
}

// Insert the single linked list
Insert_l (struct lnode * P, int I, int N)
{
Int length = 0;
Int J = 1;
Struct lnode * linklist;
Lnode P1;
Lnode Q;
Linklist = P;
P1 = P;
While (p-> next! = NULL)
{
P = p-> next;
Length ++;
}
Printf ("Length: % d/N", length );
If (length <I) | (I <1 ))
Puts ("insert postion is error ");
Q = (lnode) malloc (sizeof (lnode ));
If (! Q)
{
Puts ("malloc errored! ");
Exit (0 );
}
While (P1-> next! = NULL) & (j <I-1 ))
{
P1 = p1-> next;
J ++;
}
If (P1-> next) = NULL | (j> I ))
{
Puts ("insert error! ");
Exit (0 );
}
Q-> DATA = N;
Q-> next = p1-> next;
P1-> next = Q;
While (linklist-> next! = NULL)
{
Printf ("% d/N", linklist-> data );
Linklist = linklist-> next;
}
}

// Delete from linklist
Delete_l (struct lnode * P, int I)
{
Lnode q = P;
Lnode Q1 = P;
Int J = 0;
/*
While (p-> next! = NULL)
{
Printf ("% d/N", p-> data );
P = p-> next;
}
Exit (0 );
*/
While (P-> next! = NULL) & (j <I-1 ))
{
Q1 = P;
P = p-> next;
J ++;
}
If (P-> next = NULL) | (j> I ))
{
Puts ("delete error! ");
Exit (0 );
}
Q1-> next = p-> next;
While (Q-> next! = NULL)
{
Printf ("% d/N", Q-> data );
Q = Q-> next;
}
}

Int main (INT argc, char ** argv)
{
Struct lnode * ll;
Int I;
Int N;
LL = (struct lnode *) malloc (sizeof (struct lnode ));
Init_l (LL );
Puts ("Please input the location and value of insert :");
Scanf ("% d", & I, & N );
Insert_l (LL, I, n );
Puts ("Please input the location of delete :");
Scanf ("% d", & I );
Delete_l (LL, I );
Return 0;
}

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.