[Singly linked list] Headless single item without loop (2)

Table of Contents

Test.c main function

test5

test6

test7

test8

test9

Test.c total code

SList.h header file & function declaration

head File

function declaration

SList.h total code

SList.c function implementation

Query SLFind

Insert in front of pos

Insert after pos

Delete after pos

posdelete

space release

SList.c total code


Today’s linked list.

Test.c main function

#include"SList.h"
int main()
{
SLNode* phead = NULL;//The structure pointer variable stores the address of the structure head node
test5( & amp;phead);//Test search
test6( & amp;phead);//The test is inserted in front of pos
test7( & amp;phead);//The test is inserted after pos
test8(& amp;phead);//Test to delete the element after pos
test9(& amp;phead);//Test to delete the pos element
return 0;
}

test5

void test5(SLNode** pphead)//Test search
{
SLNode*ret=SLFind(*pphead, 77);
if (ret != NULL)
{
printf("Found: %d\
", ret->val);
}
else
{
printf("Not found\
");
}
}

test6

void test6(SLNode** pphead)//Test the element inserted in front of pos
{
SLNode* ret = SLFind(*pphead, 77);
SLInsert(pphead, ret, 34);
SLNode* pos = SLFind(*pphead, 34);
SLInsert(pphead, pos, 78);
SLPrint(*pphead);
}

test7

void test7(SLNode** pphead)//Test the element inserted after pos
{
SLNode* ret = SLFind(*pphead, 77);
SLInsertAfter(pphead, ret, 99);
SLPrint(*pphead);
}

test8

void test8(SLNode** pphead)//Test to delete the element after pos
{
SLNode* ret = SLFind(*pphead, 77);
SLEraseAfter(pphead, ret);//99
SLPrint(*pphead);
}

test9

void test9(SLNode** pphead)//Test to delete the pos element
{
SLNode* ret = SLFind(*pphead, 78);
SLErase(pphead, ret);
SLPrint(*pphead);
}

Test.c total code

#include"SList.h"
void test1(SLNode** pphead)//Test tail insertion
{
SLPushBack(pphead, 10);
SLPushBack(pphead, 20);
SLPushBack(pphead, 30);
SLPushBack(pphead, 40);
SLPrint(*pphead);
}

void test2(SLNode** pphead)//Test head plug
{
SLPushFront(pphead, 77);
SLPushFront(pphead, 66);
SLPushFront(pphead, 55);
SLPushFront(pphead, 33);
SLPrint(*pphead);
}

//

void test3(SLNode** pphead)//Delete test header
{
SLPopFront(pphead);
SLPopFront(pphead);
SLPopFront(pphead);
SLPrint(*pphead);
}

void test4(SLNode** pphead)//Test tail deletion
{
SLPopBack(pphead);
SLPopBack(pphead);
SLPrint(*pphead);
}

void test5(SLNode** pphead)//Test search
{
SLNode*ret=SLFind(*pphead, 77);
if (ret != NULL)
{
printf("Found: %d\
", ret->val);
}
else
{
printf("Not found\
");
}
}

void test6(SLNode** pphead)//Test the element inserted in front of pos
{
SLNode* ret = SLFind(*pphead, 77);
SLInsert(pphead, ret, 34);
SLNode* pos = SLFind(*pphead, 34);
SLInsert(pphead, pos, 78);
SLPrint(*pphead);
}


void test7(SLNode** pphead)//Test the element inserted after pos
{
SLNode* ret = SLFind(*pphead, 77);
SLInsertAfter(pphead, ret, 99);
SLPrint(*pphead);
}


void test8(SLNode** pphead)//Test to delete the elements after pos
{
SLNode* ret = SLFind(*pphead, 77);
SLEraseAfter(pphead, ret);//99
SLPrint(*pphead);
}

void test9(SLNode** pphead)//Test to delete the pos element
{
SLNode* ret = SLFind(*pphead, 78);
SLErase(pphead, ret);
SLPrint(*pphead);
}

int main()
{
SLNode* phead = NULL;//The structure pointer variable stores the address of the structure head node
test1( & amp;phead);//Test tail insertion
test2( & amp;phead);//Test head plug
test3( & amp;phead);//Test tail deletion
    test4( & amp;phead);//Delete test head
test5( & amp;phead);//Test search
test6( & amp;phead);//The test is inserted in front of pos
test7( & amp;phead);//The test is inserted after pos
test8(& amp;phead);//Test to delete the element after pos
test9(& amp;phead);//Test to delete the pos element
return 0;
}

SList.h header file & function declaration

Header file

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

Function declaration

  • Single linked list element query
//Find a certain value in a singly linked list
SLNode* SLFind(SLNode* phead, SLNDataType x);
  • Insert element before pos
//Insert in front of pos
void SLInsert(SLNode** pphead, SLNode*pos,SLNDataType x);
  • Insert element after pos
//Insert after pos
void SLInsertAfter(SLNode** pphead, SLNode* pos, SLNDataType x);
  • Delete the element at the position after pos
//Delete the position after pos
void SLEraseAfter(SLNode** pphead, SLNode* pos);
  • Delete the element at pos position
//Delete pos position
void SLErase(SLNode** pphead, SLNode* pos);
  • space release
//Space release
void SLDestroy(SLNode** pphead);

SList.h total code

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

//Create a singly linked list
typedef int SLNDataType;//single linked list node data type

typedef struct SListNode//Create node
{
SLNDataType val;
struct SListNode* next;
}SLNode;

//Print data
void SLPrint(SLNode* phead);

//Tail insertion
void SLPushBack(SLNode** pphead, SLNDataType x);

//Head plug
void SLPushFront(SLNode** pphead, SLNDataType x);

//Delete header
void SLPopFront(SLNode** pphead);

//Delete tail
void SLPopBack(SLNode** pphead);

//Find a certain value in a singly linked list
SLNode* SLFind(SLNode* phead, SLNDataType x);

//Insert in front of pos
void SLInsert(SLNode** pphead, SLNode*pos,SLNDataType x);

//Insert after pos
void SLInsertAfter(SLNode** pphead, SLNode* pos, SLNDataType x);

//Delete the position behind pos
void SLEraseAfter(SLNode** pphead, SLNode* pos);

//Delete pos position
void SLErase(SLNode** pphead, SLNode* pos);

//Space release
void SLDestroy(SLNode** pphead);

SList.c function implementation

Query SLFind

//Find a number in a singly linked list
// Found the address that returns this linked list
//Not found returns NULL
SLNode* SLFind(SLNode* phead, SLNDataType x)
{
SLNode* cur = phead;
while(cur)
{
if (cur->val == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}

insert before pos

//Insert in front of pos
void SLInsert(SLNode** pphead, SLNode*pos,SLNDataType x)
{
//Strictly limit the singly linked list to have a valid node
assert(pphead);//No one should be drunk, right?
assert(*pphead);
assert(pos);
SLNode* newnode = CreateNode(x);
SLNode* cur = *pphead;
SLNode* prve = NULL;
//Suitable for middle and tail
if(prve)
{
while (cur != pos)
{
prve = cur;
cur = cur->next;
}
prve->next = newnode;
newnode->next = cur;
}
else
{
//Head plug
SLPushFront(pphead, x);
}
}

insert after pos

//Insert after pos
void SLInsertAfter(SLNode** pphead, SLNode* pos, SLNDataType x)
{
//Strictly limit the singly linked list to have a valid node
assert(pphead);//No one should be drunk, right?
assert(*pphead);
assert(pos);
SLNode* newnode = CreateNode(x);
newnode->next = pos->next;
pos->next = newnode;
//No need to process the head and tail.
}

Delete after pos

//Delete the position after pos
void SLEraseAfter(SLNode** pphead, SLNode* pos)
{
//pos is not allowed in the last one
assert(pos->next);
assert(pos);
SLNode* tmp = pos->next;
pos->next = pos->next->next;
free(tmp);
tmp = NULL;
}

pos delete

//Delete pos position
void SLErase(SLNode** pphead, SLNode* pos)
{
assert(pos);
SLNode* cur = *pphead;
SLNode* prve = NULL;
if(prve)
{
while (cur != pos)
{
prve = cur;
cur = cur->next;
}
prve->next = cur->next;
free(pos);
pos = NULL;
}
else
{
*pphead = pos->next;
free(pos);
pos = NULL;
}
}

Space Release

//Space release
void SLDestroy(SLNode** pphead)
{
assert(*pphead);
SLNode* cur = *pphead;
while (cur)
{
SLNode* tmp = cur->next;
free(cur);
cur = tmp;//cur=cur->next
}
}

SList.c total code

#include"SList.h"
void SLPrint(SLNode*phead)
{
assert(phead);
SLNode* tail = phead;
printf("phead->");
while (tail->next != NULL)
{
printf("%d->", tail->val);
tail = tail->next;
}
printf("NULL");
printf("\
");
}


//Create the node of the linked list---structure
SLNode* CreateNode(SLNDataType x)
{
SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
if (newnode == NULL)
{
perror("malloc");
return;
}
newnode->val = x;
newnode->next = NULL;
return newnode;
}

//Test tail insertion
void SLPushBack(SLNode** pphead, SLNDataType x)
{
//assert(*pphead);
SLNode* newnode = CreateNode(x);
//no node
if (*pphead == NULL)
{
*pphead = newnode;
}
//Multiple nodes
else
{
SLNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}

}


//Head plug
void SLPushFront(SLNode** pphead, SLNDataType x)
{
//assert(*pphead);
SLNode* newnode = CreateNode(x);
    newnode->next = *pphead;
    *pphead = newnode;
}


//---Deletion involves the release of space---Assertion (overdeletion)
//Delete header
void SLPopFront(SLNode** pphead)
{
assert(*pphead);
SLNode* tail = *pphead;
*pphead = (*pphead)->next;
free(tail);
tail = NULL;
}


//Delete tail
void SLPopBack(SLNode** pphead)
{
assert(*pphead);
//a node
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLNode* tail = *pphead;
SLNode* prve = NULL;//Although setting prve to NULL and tail are the same here, errors will occur in OJ questions.
while (tail->next != NULL)
{
prve = tail;
tail = tail->next;
}
prve->next = NULL;
free(tail);
tail = NULL;
}
}



//Find a number in a singly linked list
// Found the address that returns this linked list
//Not found returns NULL
SLNode* SLFind(SLNode* phead, SLNDataType x)
{
SLNode* cur = phead;
while(cur)
{
if (cur->val == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}




//Insert in front of pos
void SLInsert(SLNode** pphead, SLNode*pos,SLNDataType x)
{
//Strictly limit the singly linked list to have a valid node
assert(pphead);//No one should be drunk, right?
assert(*pphead);
assert(pos);
SLNode* newnode = CreateNode(x);
SLNode* cur = *pphead;
SLNode* prve = NULL;
//Suitable for middle and tail
if(prve)
{
while (cur != pos)
{
prve = cur;
cur = cur->next;
}
prve->next = newnode;
newnode->next = cur;
}
else
{
//Head plug
SLPushFront(pphead, x);
}
}



//Insert after pos
void SLInsertAfter(SLNode** pphead, SLNode* pos, SLNDataType x)
{
//Strictly limit the singly linked list to have a valid node
assert(pphead);//No one should be drunk, right?
assert(*pphead);
assert(pos);
SLNode* newnode = CreateNode(x);
newnode->next = pos->next;
pos->next = newnode;
//No need to process the head and tail.
}



//Delete the position behind pos
void SLEraseAfter(SLNode** pphead, SLNode* pos)
{
//pos is not allowed in the last one
assert(pos->next);
assert(pos);
SLNode* tmp = pos->next;
pos->next = pos->next->next;
free(tmp);
tmp = NULL;
}


//Delete pos position
void SLErase(SLNode** pphead, SLNode* pos)
{
assert(pos);
SLNode* cur = *pphead;
SLNode* prve = NULL;
if(prve)
{
while (cur != pos)
{
prve = cur;
cur = cur->next;
}
prve->next = cur->next;
free(pos);
pos = NULL;
}
else
{
*pphead = pos->next;
free(pos);
pos = NULL;
}
}



//Space release
void SLDestroy(SLNode** pphead)
{
assert(*pphead);
SLNode* cur = *pphead;
while (cur)
{
SLNode* tmp = cur->next;
free(cur);
cur = tmp;//cur=cur->next
}
}

The recent bug fixes have made me want to smash my computer, so stay calm. In the next blog we will continue with other types of linked lists. study hard, improve every day.

Code———→[Tang Didi (TSQXG) – Gitee.com]

Contact———→[Email: [email protected]]

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57379 people are learning the system