鏈表常見操作源代碼

 

人,如果不爲夢想搏一搏,那和鹹魚又有什麼區別呢?

 下面是一些鏈表的源碼,僅供參考使用:

//  頭插法:
#include<stdio.h>
#include<stdlib.h>
struct Book
{
    char title [128];
	char author [40];
	struct Book *next;
};
void getInput(struct Book *book )
{
	printf("please input book name:\n");
	scanf("%s",book->title);
	printf("please input anthor\n");
	scanf("%s",book->author);
}
void addBook (struct Book **library)
{
	struct Book *book,*temp;
	book=(struct Book *)malloc(sizeof(struct Book));
	if(book==NULL)
	{
		printf("Failed to allcoate memory.\n");
		exit(1);
	}
	getInput(book);
	if(*library!=NULL)
	{
		temp=*library;
		*library=book;
		book->next=temp;
	}
	else
	{
		*library=book;
		book->next=NULL;
	}
}
void printLibrary (struct Book *library)
{
	struct Book *book;
	int count=1;
	book=library;
	while(book!=NULL)
	{
		printf("Book : %d\n",count);
		printf("Book name : %s\n",book->title);
		printf("Book anthor : %s\n",book->author);
		book=book->next;
		count++;
	}
}
void releaseLibrary (struct Book *library)
{
	struct Book *temp;
	while(library!=NULL)
	{
	    temp=library->next;
		free(library);
		library=temp;
	}
}
int main()
{
	struct Book *library=NULL;
	char ch;
	while(1)
	{
		printf("please enter the information for the book (Y/N)\n");
		do
		{
			ch=getchar();
		}while(ch!='Y'&&ch!='N');
		if(ch=='Y')
		{
			addBook(&library);
		}
		else
		{
			break;
		}
	}
	printf("please print the information for the book (Y/N)\n");
	do
	{
		ch=getchar();
	}while(ch!='Y'&&ch!='N');
	if(ch=='Y')
	{
		printLibrary(library);
	}
    releaseLibrary(library);
    return 0;
}
// 輸出結果示例:
please enter the information for the book (Y/N)
Y
please input book name:
yrbiufjndkmsl
please input anthor
bvjnk
please enter the information for the book (Y/N)
Y
please input book name:
ybdjksl.viokms
please input anthor
vcb
please enter the information for the book (Y/N)
Y
please input book name:
vyubfh kj
please input anthor
please enter the information for the book (Y/N)
Y
please input book name:
yguvdbshjbj
please input anthor
bbvfhjn
please enter the information for the book (Y/N)
N
please print the information for the book (Y/N)
Y
Book : 1
Book name : yguvdbshjbj
Book anthor : bbvfhjn
Book : 2
Book name : vyubfh
Book anthor : kj
Book : 3
Book name : ybdjksl.viokms
Book anthor : vcb
Book : 4
Book name : yrbiufjndkmsl
Book anthor : bvjnk

Process returned 0 (0x0)   execution time : 59.930 s
Press any key to continue.

// 尾插法:
#include<stdio.h>
#include<stdlib.h>
struct Book
{
    char title [128];
	char author [40];
	struct Book *next;
};
void getInput(struct Book *book )
{
	printf("please input book name:\n");
	scanf("%s",book->title);
	printf("please input anthor\n");
	scanf("%s",book->author);
}
void addBook (struct Book **library)
{
	struct Book *book,*temp;
	book=(struct Book *)malloc(sizeof(struct Book));
	if(book==NULL)
	{
		printf("Failed to allcoate memory.\n");
		exit(1);
	}
	getInput(book);
	if(*library!=NULL)
	{
		temp=*library;
		while(temp->next!=NULL)
		{
		    temp=temp->next;
		}
		temp->next=book;
		book->next=NULL;
	}
	else
	{
		*library=book;
	  	book->next=NULL;

	}
}
void printLibrary (struct Book *library)
{
	struct Book *book;
	int count=1;
	book=library;
	while(book!=NULL)
	{
		printf("Book : %d\n",count);
		printf("Book name : %s\n",book->title);
		printf("Book anthor : %s\n",book->author);
		book=book->next;
		count++;
	}
}
void releaseLibrary (struct Book *library)
{
	struct Book *temp;
	while(library!=NULL)
	{
	    temp=library->next;
		free(library);
		library=temp;
	}
}
int main()
{
	struct Book *library=NULL;
	char ch;
	while(1)
	{
		printf("please enter the information for the book (Y/N)\n");
		do
		{
			ch=getchar();
		}while(ch!='Y'&&ch!='N');
		if(ch=='Y')
		{
			addBook(&library);
		}
		else
		{
			break;
		}
	}
	printf("please print the information for the book (Y/N)\n");
	do
	{
		ch=getchar();
	}while(ch!='Y'&&ch!='N');
	if(ch=='Y')
	{
		printLibrary(library);
	}
    releaseLibrary(library);
    return 0;
}
//   這種方法每次都要去遍歷一片鏈表,效率很低;

//   輸出結果:
please enter the information for the book (Y/N)
Y
please input book name:
gyeuwihojpkd
please input anthor
ygvhdun
please enter the information for the book (Y/N)
Y
please input book name:
f7fteybwiduvnioml,
please input anthor
fevydc
please enter the information for the book (Y/N)
Y
please input book name:
gyubdcius;l,lvbwuno
please input anthor
eyftwvsbd
please enter the information for the book (Y/N)
Y
please input book name:
tevuydbvijnkml
please input anthor
vgyg
please enter the information for the book (Y/N)
N
please print the information for the book (Y/N)
Y
Book : 1
Book name : gyeuwihojpkd
Book anthor : ygvhdun
Book : 2
Book name : f7fteybwiduvnioml,
Book anthor : fevydc
Book : 3
Book name : gyubdcius;l,lvbwuno
Book anthor : eyftwvsbd
Book : 4
Book name : tevuydbvijnkml
Book anthor : vgyg

Process returned 0 (0x0)   execution time : 42.575 s
Press any key to continue.


//  好,下面是一種改進的尾插法,可以大大的提高效率:
//  尾插法,該進版:
#include<stdio.h>
#include<stdlib.h>
struct Book
{
    char title [128];
	char author [40];
	struct Book *next;
};
void getInput(struct Book *book )
{
	printf("please input book name:\n");
	scanf("%s",book->title);
	printf("please input anthor\n");
	scanf("%s",book->author);
}
void addBook (struct Book **library)
{
	struct Book *book;
	static struct Book *tail;  // 靜態變量,每一次調用addBook函數時,tail指針的值不變
	book=(struct Book *)malloc(sizeof(struct Book));
	if(book==NULL)
	{
		printf("Failed to allcoate memory.\n");
		exit(1);
	}
	getInput(book);
	if(*library!=NULL)
	{
		tail->next=book;
		book->next=NULL;
	}
	else
	{
		*library=book;
	  	book->next=NULL;

	}
	tail=book;
}
void printLibrary (struct Book *library)
{
	struct Book *book;
	int count=1;
	book=library;
	while(book!=NULL)
	{
		printf("Book : %d\n",count);
		printf("Book name : %s\n",book->title);
		printf("Book anthor : %s\n",book->author);
		book=book->next;
		count++;
	}
}
void releaseLibrary (struct Book *library)
{
	struct Book *temp;
	while(library!=NULL)
	{
	    temp=library->next;
		free(library);
		library=temp;
	}
}
int main()
{
	struct Book *library=NULL;
	char ch;
	while(1)
	{
		printf("please enter the information for the book (Y/N)\n");
		do
		{
			ch=getchar();
		}while(ch!='Y'&&ch!='N');
		if(ch=='Y')
		{
			addBook(&library);
		}
		else
		{
			break;
		}
	}
	printf("please print the information for the book (Y/N)\n");
	do
	{
		ch=getchar();
	}while(ch!='Y'&&ch!='N');
	if(ch=='Y')
	{
		printLibrary(library);
	}
    releaseLibrary(library);
    return 0;
}


//  下面是插入操作源碼:
#include<stdio.h>
#include<stdlib.h>
struct Node{
    int value;
    struct Node *next;
};
void insertNode(struct Node **head,int value){
    struct Node *previous;
    struct Node *current;
    struct Node *newp;
    current=*head;
    previous=NULL;
    while(current!=NULL&&current->value<value){
        previous=current;
        current=current->next;
    }
    newp=(struct Node *)malloc(sizeof(struct Node));
    if(newp==NULL){
        printf("內存分配失敗\n");
        exit(1);
    }
    newp->value=value;
    newp->next=current;
    if(previous==NULL){
        *head=newp;
    }
    else{
        previous->next=newp;
    }
}
void printNode(struct Node *head){
    struct Node *current;
    current=head;
    while(current!=NULL){
        printf("%d ",current->value);
        current=current->next;
    }
    printf("\n");
}
int main()
{
    struct Node *head=NULL;
    int input;
    printf("開始輸入插入整數:\n");
    while(1){
        printf("請輸入一個整數(輸入-1代表結束):");
        scanf("%d",&input);
        if(input==-1){
            break;
        }
        insertNode (&head,input);
        printNode(head);
    }
    return 0;
}
//  輸出結果:
開始輸入插入整數:
請輸入一個整數(輸入-1代表結束):9
9
請輸入一個整數(輸入-1代表結束):4
4 9
請輸入一個整數(輸入-1代表結束):5
4 5 9
請輸入一個整數(輸入-1代表結束):2
2 4 5 9
請輸入一個整數(輸入-1代表結束):4
2 4 4 5 9
請輸入一個整數(輸入-1代表結束):1
1 2 4 4 5 9
請輸入一個整數(輸入-1代表結束):3
1 2 3 4 4 5 9
請輸入一個整數(輸入-1代表結束):
10
1 2 3 4 4 5 9 10
請輸入一個整數(輸入-1代表結束):-1

Process returned 0 (0x0)   execution time : 21.320 s
Press any key to continue.

// 下面是刪除操作源碼:
#include<stdio.h>
#include<stdlib.h>
struct Node{
    int value;
    struct Node *next;
};
void insertNode(struct Node **head,int value){
    struct Node *previous;
    struct Node *current;
    struct Node *newp;
    current=*head;
    previous=NULL;
    while(current!=NULL&&current->value<value){
        previous=current;
        current=current->next;
    }
    newp=(struct Node *)malloc(sizeof(struct Node));
    if(newp==NULL){
        printf("內存分配失敗\n");
        exit(1);
    }
    newp->value=value;
    newp->next=current;
    if(previous==NULL){
        *head=newp;
    }
    else{
        previous->next=newp;
    }
}
void deleteNode(struct Node **head,int value)
{
    struct Node *previous;
    struct Node *current;
    current=*head;
    previous=NULL;
    while(current!=NULL&&current->value!=value)
    {
        previous=current;
        current=current->next;
    }
    if(current==NULL)
    {
        printf("找不到匹配的節點\n");
        return ;
    }
    else{
        if(previous==NULL)
        {
            *head=current->next;
        }
        else{
            previous->next=current->next;
        }
        free(current);
    }
}
void printNode(struct Node *head){
    struct Node *current;
    current=head;
    while(current!=NULL){
        printf("%d ",current->value);
        current=current->next;
    }
    printf("\n");
}
int main()
{
    struct Node *head=NULL;
    int input;
    printf("開始輸入插入整數:\n");
    while(1){
        printf("請輸入一個整數(輸入-1代表結束):");
        scanf("%d",&input);
        if(input==-1){
            break;
        }
        insertNode (&head,input);
        printNode(head);
    }
    printf("開始輸入要刪除的整數:\n");
    while(1){
        printf("請輸入一個整數(輸入-1代表結束):");
        scanf("%d",&input);
        if(input==-1){
            break;
        }
        deleteNode (&head,input);
        printNode(head);
    }
    return 0;
}


//  輸出結果源碼:
開始輸入插入整數:
請輸入一個整數(輸入-1代表結束):9
9
請輸入一個整數(輸入-1代表結束):5
5 9
請輸入一個整數(輸入-1代表結束):1
1 5 9
請輸入一個整數(輸入-1代表結束):4
1 4 5 9
請輸入一個整數(輸入-1代表結束):5
1 4 5 5 9
請輸入一個整數(輸入-1代表結束):7
1 4 5 5 7 9
請輸入一個整數(輸入-1代表結束):6
1 4 5 5 6 7 9
請輸入一個整數(輸入-1代表結束):2
1 2 4 5 5 6 7 9
請輸入一個整數(輸入-1代表結束):5
1 2 4 5 5 5 6 7 9
請輸入一個整數(輸入-1代表結束):5
1 2 4 5 5 5 5 6 7 9
請輸入一個整數(輸入-1代表結束):-1
開始輸入要刪除的整數:
請輸入一個整數(輸入-1代表結束):2
1 4 5 5 5 5 6 7 9
請輸入一個整數(輸入-1代表結束):4
1 5 5 5 5 6 7 9
請輸入一個整數(輸入-1代表結束):5
1 5 5 5 6 7 9
請輸入一個整數(輸入-1代表結束):5
1 5 5 6 7 9
請輸入一個整數(輸入-1代表結束):5
1 5 6 7 9
請輸入一個整數(輸入-1代表結束):5
1 6 7 9
請輸入一個整數(輸入-1代表結束):1
6 7 9
請輸入一個整數(輸入-1代表結束):6
7 9
請輸入一個整數(輸入-1代表結束):7
9
請輸入一個整數(輸入-1代表結束):9

請輸入一個整數(輸入-1代表結束):-1

Process returned 0 (0x0)   execution time : 43.842 s
Press any key to continue.
//   下面是融合了多中基本操作的源碼:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
typedef struct node
{
    int data;
    struct node *next;
} LNode,*LinkList;
LinkList L;
LNode *head=(LNode *)malloc(sizeof(LNode));
LNode *creat_L()
{
    L=head;
    LNode *r=head;
    printf("請輸入數字用以創建鏈表,輸入0結束\n");
    int num;
    scanf("%d",&num);
    while(num!=0)
    {
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=num;
        r->next=p;
        r=p;
        scanf("%d",&num);
    }
    r->next=NULL;
    return L;
}
void show_L(LinkList L)
{
    if(L->next==NULL)
    {
        printf("Link is empty");
    }
    else
    {
        LNode *p=L->next;
        printf("%d",p->data);
        while(p->next!=NULL)
        {
            p=p->next;
            printf(" %d",p->data);
        }
        printf("\n");
    }
}
LNode *get_L(LinkList L,int location)
{
    LNode *p=L;
    int i=0;
    while(p->next!=NULL&&i<location)
    {
        p=p->next;
        i++;
    }
    if(i==location)
        return p;
    else
        return NULL;
}
int delete_L(LinkList L,int location)
{
    LNode *p,*s;
    p=get_L(L,location-1);
    if(p==NULL||p->next==NULL)
        return 0;
    else
    {
        s=p->next;
        p->next=s->next;
        free(s);
        return 1;
    }
}
int insert_L(LinkList L,int location,int num)
{
    LNode *p,*s;
    p=get_L(L,location-1);
    if(p==NULL)
        return 0;
    else
    {
        s=(LNode *)malloc(sizeof(LNode));
        s->data=num;
        s->next=p->next;
        p->next=s;
        return 1;
    }
}
int main()
{
    L=creat_L();
    char str[20];
    printf("請輸入相關指令,包括:insert,show,delete,get\n");
    while(~scanf("%s",str))
    {
        if(strcmp(str,"insert")==0)
        {
            printf("請輸入你所要插入的地址及數值\n");
            int location;
            int num;
            scanf("%d%d",&location,&num);
            int flag=insert_L(L,location,num);
            if(flag==1)
            {
                printf("insert OK\n");
                printf("插入成功,插入後的鏈表爲:\n");
                show_L(L);
            }
            else
                printf("insert fail\n");
        }
        if(strcmp(str,"show")==0)
        {
            printf("鏈表爲:\n");
            show_L(L);
        }
        if(strcmp(str,"delete")==0)
        {
            printf("請輸入需要刪除的位置:\n");
            int location;
            scanf("%d",&location);
            int flag=delete_L(L,location);
            if(flag==1)
            {
                printf("delete OK\n");
                show_L(L);
            }
            else
                printf("delete fail\n");
        }
        if(strcmp(str,"get")==0)
        {
            printf("請輸入你想要得到數值的位置:\n");
            int location;
            scanf("%d",&location);
            LNode *p=get_L(L,location);
            if(p==NULL)
            {
                printf("get fail\n");
            }
            else
            {
                printf("獲取成功,其數值爲:\n");
                printf("%d\n",p->data);
            }
        }
    }
    return 0;
}


//  輸出結果:
請輸入數字用以創建鏈表,輸入0結束
4 8 6 1 2 3 4 7 8 9
0
請輸入相關指令,包括:insert,show,delete,get
insert
請輸入你所要插入的地址及數值
5 78
insert OK
插入成功,插入後的鏈表爲:
4 8 6 1 78 2 3 4 7 8 9
show
鏈表爲:
4 8 6 1 78 2 3 4 7 8 9
delete
請輸入需要刪除的位置:
6
delete OK
4 8 6 1 78 3 4 7 8 9
get
請輸入你想要得到數值的位置:
9
獲取成功,其數值爲:
8

代碼整理不易,喜歡的話,就點個贊吧! 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章