You are on page 1of 5

#include <stdio.

h>

#include <stdlib.h>

struct Node

int info;

struct Node* next;

};

typedef struct Node* node;

//khoi tao

void init(node& phead)

phead = NULL;

//kiem tra rong

int isEmpty(node phead)

return(phead == NULL);

//tao node

node createNode(int x)

node p = (node)malloc(sizeof(node));

p->info = x;

p->next = NULL;

return p;

// chen dau

void insertFirst(node& phead, int x)

node p = createNode(x);

if (isEmpty(phead))
phead = p;

else {

p->next = phead;

phead = p;

//Chen cuoi

void insertLast(node& phead, int x)

node tmp, p;

tmp = createNode(x);

if (isEmpty(phead))

phead = tmp;

else

p = phead;

while (p->next != NULL)

p = p->next;

p->next = tmp;

//chen vao vi tri chi dinh

void insertAfter(node& phead, int x, int position)

node tmp, p;

if (position == 0 || isEmpty(phead))

{
insertFirst(phead, x);

else

int k = 1;

p = phead;

while (p->next != NULL && k != position)

p = p->next;

k++;

if (k != position)

insertLast(phead, x);

else

tmp = createNode(x);

tmp->next = p->next;

p->next = tmp;

//Nhal danh sach lien ket

void input(node &phead)

init(phead);

int x, n;

printf("\nSo luong phan tu: ");

scanf("%d", &n);

for (int i = 0; i < n; i++)


{

printf("Phan tu thu %d: ", i);

scanf("%d", &x);

insertFirst(phead, x);

//hien thi danh sach lien ket

void show(node phead)

for (node p = phead; p != NULL; p = p->next)

printf("%d ", p->info);

int main()

node phead;

input(phead);

//insertAfter(phead, 200, 2);

show(phead);

return 0;

You might also like