Basic linked list operation (c)

Source: Internet
Author: User

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

 

Typedef struct _ list
{
Int data;
Struct _ list * next;
} List, * plist;

Plist create_list (int * a, int Len)
{
Plist head = NULL;
Plist PTR = NULL;
Plist newnode = NULL;
Head = (plist) malloc (sizeof (list ));
Head-> DATA = A [0];
Head-> next = NULL;
PTR = head;
For (INT I = 1; I <10; I ++)
{
Newnode = (plist) malloc (sizeof (list ));
Newnode-> DATA = A [I];
Newnode-> next = NULL;
PTR-> next = newnode;
PTR = PTR-> next;
}
Return head;
}

Plist insert_node (plist head, int data, int POS)
{
Plist node, PTR;
PTR = head;
Node = (plist) malloc (sizeof (list ));
Node-> DATA = data;
Node-> next = NULL;
If (Pos = 0)
{
Node-> next = head;
Return node;
}
Else
{
For (INT I = 0; I <POS & PTR-> next! = NULL; I ++)
{
PTR = PTR-> next;
}
Node-> next = PTR-> next;
PTR-> next = node;
Return head;
}
}

Void print_list (plist head)
{
Plist PTR = head;
While (PTR! = NULL)
{
Printf ("% d", PTR-> data );
PTR = PTR-> next;
}
Printf ("\ n ");
}

Plist delete_node (plist head, int POS)
{
Plist PTR = head;
Plist temp = NULL;
If (Pos = 0)
{
PTR = head-> next;
Free (head );
Return PTR;
}
Else
{
For (INT I = 1; I <pos-1 & PTR-> next! = NULL; I ++)
{
PTR = PTR-> next;
}
If (PTR-> next! = NULL)
{
Temp = PTR-> next;
PTR-> next = temp-> next;
Free (temp );
Return head;
}
Else
{
Free (PTR-> next );
PTR-> next = NULL;
}
}
}

Plist reverse_list (plist head)
{
Plist A, B, C;
A = NULL;
B = head;
C = head-> next;

While (B-> next! = NULL)
{
B-> next =;
A = B;
B = C;
C = C-> next;
}
B-> next =;
Return B;
}

Int main (void)
{
Int A [10];

For (INT I = 0; I <10; I ++)
{
A [I] = I;
}
Plist head = NULL;
Head = create_list (A, 10 );
Print_list (head );

Head = insert_node (Head, 4, 10 );
Print_list (head );

Head = delete_node (Head, 12 );
Print_list (head );

Head = reverse_list (head );
Print_list (head );
}

 

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.