- #include<stdio.h>
- #include<stdlib.h>
- struct Node{
- int data;// luu thong tin
- struct Node *pNext;// tro toi ptu tiep theo
- };
- void creNode(Node *&pHead){
- int repeat;
- Node *p = pHead;
- do{
- if(pHead == NULL){
- pHead = (Node*)malloc(sizeof(Node));
- p = pHead;
- }else{
- p = pHead;
- while(p->pNext != NULL) p = p->pNext;
- p->pNext = (Node*)malloc(sizeof(Node));
- p = p->pNext;
- }
- printf("Nhap du lieu cho danh sach : \n");
- scanf("%d",&(p->data));
- p->pNext = NULL;
- printf("1.Nhap tiep: \n0.Thoat\n ");
- scanf("%d",&repeat);
- }while(repeat != 0);
- }
- void inDS(Node *&pHead){
- Node *p = pHead;
- if(pHead == NULL) return;
- while(p != NULL){
- printf("%d\t",p->data);
- p = p->pNext;
- }
- }
- int soPT(Node *&pHead){
- Node *p = pHead;
- int count = 0;
- while(p != NULL){
- count++;
- p=p->pNext;
- }
- return count;
- }
- Node *make_Node(Node *p, int x){
- p = (Node*)malloc(sizeof(Node));
- p->pNext = NULL;
- p->data = x;
- return p;
- }
- bool search(Node *pHead, int x){
- Node *p = pHead;
- while(p != NULL && p->data != x){
- p = p->pNext;
- }
- if(p != NULL) return true;
- else return false;
- }
- void insertFirst(Node *&pHead,int x){
- Node *p;
- p = make_Node(p,x);
- p->pNext = pHead;
- pHead = p;
- }
- void insertX(Node *&pHead, int x,int vitri){
- Node *p, *q = pHead;
- p = make_Node(p,x);
- int i = 1;
- if(vitri < 1 || vitri > soPT(pHead) + 1){
- printf("Vi tri khong hop le");
- }else{
- p = make_Node(p,x);
- if(vitri == 1){
- insertFirst(pHead,x);
- }else{
- while(q != NULL && i != vitri-1){
- i++;
- q = q->pNext;
- }
- p->pNext = q->pNext;
- q->pNext = p;
- }
- }
- }
- Node *insertAfter(Node *pHead,int x){
- Node *p,*q;
- p = make_Node(p,x);
- if(pHead == NULL){
- p->pNext = NULL;
- pHead = p;
- }else{
- q = pHead;
- while(q->pNext != NULL){
- q = q->pNext;
- }
- q->pNext = p;
- }
- return pHead;
- }
- Node *delete_First(Node *&pHead){
- if(pHead == NULL){
- printf("DS rong");
- }else{
- pHead = pHead->pNext;
- }
- return pHead;
- }
- Node *delete_After(Node *&pHead){
- Node *p = pHead;
- if(pHead == NULL){
- printf("DS rong");
- }else{
- while(p->pNext->pNext != NULL){
- p = p->pNext;
- }
- p->pNext = NULL;
- }
- }
- void delete_X(Node *&pHead, int vitri){
- Node *p = pHead;
- int i = 1;
- if(vitri == 1 || pHead == NULL || pHead->pNext == NULL){
- delete_First(pHead);
- }else{
- while(p->pNext->pNext != NULL && i != vitri-1){
- p = p->pNext;
- ++i;
- }
- p->pNext = p->pNext->pNext;
- }
- // return pHead;
- }
- int main(){
- Node *pHead = NULL;
- creNode(pHead);
- inDS(pHead);
- printf("\n");
- int luachon;
- do{
- printf("\n\nMoi ban chon: ");
- printf("\n1.Them phan tu vao dau DS\n2.Them phan tu vao vi tri bat ki cua DS\n3.Them phan tu vao cuoi DS\n4.Xoa phan tu dau DS\n5.Xoa phan tu o vi tri bat ki cua DS\n6.Xoa phan tu cuoi DS\n0.Thoat\n");
- scanf("%d",&luachon);
- switch(luachon){
- case 1:{
- int x;
- printf("\nNhap phan tu can them: ");
- scanf("%d",&x);
- insertFirst(pHead,x);
- printf("\n");
- inDS(pHead);
- break;
- }
- case 2:{
- int x;
- int vitri;
- printf("\nNhap vi tri: ");
- scanf("%d",&vitri);
- printf("\nNhap phan tu can them: ");
- scanf("%d",&x);
- insertX(pHead,x,vitri);
- printf("\n");
- inDS(pHead);
- break;
- }
- case 3:{
- int x;
- printf("\nNhap phan tu can them: ");
- scanf("%d",&x);
- insertAfter(pHead,x);
- printf("\n");
- inDS(pHead);
- break;
- }
- case 4:{
- delete_First(pHead);
- printf("\n");
- inDS(pHead);
- break;
- }
- case 5:{
- int x;
- printf("\nNhap vi tri can xoa: ");
- scanf("%d",&x);
- delete_X(pHead,x);
- printf("\n");
- inDS(pHead);
- break;
- }
- case 6:{
- delete_After(pHead);
- printf("\n");
- inDS(pHead);
- break;
- }
- default: printf("Chon lai !!!");
- }
- }while(luachon != 0);
- return 0;
- }
[text] 1
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- 1
- Paste Code
- 25 Feb-2021
- 3.85 Kb
You can Share it: