Skip to content

Instantly share code, notes, and snippets.

@toanalien
Created June 6, 2015 03:09
Show Gist options
  • Save toanalien/eda2c8390ad3b9c9d2e4 to your computer and use it in GitHub Desktop.
Save toanalien/eda2c8390ad3b9c9d2e4 to your computer and use it in GitHub Desktop.
linked list
#include <stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node *next;
};
typedef struct node *listnode;
void Insert_End(listnode *p, int x)
{
listnode q, r;
q = (listnode)malloc(sizeof(struct node));
q->item = x;
q->next = NULL;
if (*p == NULL)
*p = q;
else
{
r = *p;
while (r->next != NULL)
r = r->next;
r->next = q;
}
}
void Insert_Begin(listnode *p, int x)
{
listnode q;
q = (listnode)malloc(sizeof(struct node));
q->item = x;
q->next = *p;
*p = q;
}
void Insert_Middle(listnode *p, int position, int x)
{
int count = 1, found = 0;
listnode q, r;
r = *p;
while ((r != NULL) && (found==0))
{
if (count ==position)
{
q = (listnode)malloc(sizeof(struct node));
q->item = x;
q->next = r->next;
r->next = q;
found = 1;
}
count++;
r = r->next;
}
if (found == 0)
printf("Khong tim thay vi tri can chen ! ");
}
void Del_Begin(listnode *p)
{
listnode q;
if (p == NULL) return;
q = *p;
*p = (*p)->next;
q->next = NULL;
free(q);
}
void Del_End(listnode *p)
{
listnode q=NULL, r=NULL;
if (*p == NULL) return;
if ((*p)->next == NULL)
{
Del_Begin(p);
return;
}
r = *p;
while (r->next != NULL)
{
q = r;
r = r->next;
}
q->next = NULL;
free(r);
}
void Remove_Middle(listnode *p, int position)
{
int count = 1, found = 0;
listnode q, r;
r = *p;
while ((r != NULL) && (found==0))
{
if (count==position)
{
q = r->next;
r->next = q->next;
q->next = NULL;
free(q);
found = 1;
}
count++;
r = r->next;
}
if (found == 0)
printf("Khong tim thay phan tu de xoa !");
}
int main()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment