單鏈表的創建和使用(數據結構)

這是單鏈表的簡單形式,我沒有嘗試雙鏈表,一些簡單的功能例如插入刪除顯示。

#include<stdio.h>
#include<malloc.h>


typedef int Position;
typedef struct LNode * PtrToLNode;


struct LNode
{
	char alp;
	struct LNode *next;
}; 
struct LNode * head;
typedef PtrToLNode List;


void show(  );
List MakeEmpty();
Position Find(char alp);
bool Insert(int X, char alp);
bool Delete(int X);
void DispList();
int Length();




int main()
{
	int i, X, n, cnt, c=0;
	char alp, k;
	show();
	while(1){
      //顯示菜單 
		scanf("%d", &n);
		if(n<-1||n>6)
		{
		printf("\n輸入無效,請重新輸入:");
		continue;
		}
		if(c==0&&(n>0&&n<7)&&n!=1)
		{
		printf("\n還未創建鏈表,請重新輸入:");
		continue; 
		}
		
		switch(n)
		{	
		case 1:
			{
			MakeEmpty();  //建立新的鏈表
			printf("新的鏈表爲:");
			struct LNode *p1=head;
			cnt = Length()-1;
			for( i=0; i<cnt; i++ )
		    {
			printf("%c", p1->alp);
			p1 = p1->next;
		    }
		    printf("\n");
			c=1;	
			}
        break;
        
		case 2:
			{
			printf("\n請輸入你要插入的位置:");
			scanf("%d", &X);
			printf("請輸入你要插入的字母:");
			scanf("\n%c", &alp);
			Insert(X, alp);
			}
		break;
		
		case 3:
			{
			printf("輸入你要刪除的位序號");
			scanf("%d", &X);
			Delete(X); //刪除元素
			}
		break;


		case 4: //顯示全部元素
			{
		    printf("新的鏈表爲:");
			struct LNode *p2=head;
			cnt = Length()-1;
			for( i=0; i<cnt; i++ )
		    {
				printf("%c", p2->alp);
				p2=p2->next;
		    }
			}
		break;
		
		case 5:
			{
			printf("輸入你要查找的字母");
			k = getchar();
			scanf("%c", &alp);
			if( i!=0 && i<=Length() )
			printf("你查找的字母在第%d個位置上!\n", Find(alp)+1);//查找元素函數
			else
			printf("抱歉,找不到該字母\n");
			}
		break;
		
		case 6:
		{
			printf("表長長度爲%d\n", Length()-1 ); //求得鏈表長度 	
		}
		break;	         
		}
		show();
		}
	return 0;
}


int Length() //查找元素長度
{       
    Position cnt = 0;
	struct LNode *p=head;
	while(p)
	{
		//printf("*cnt = %d*%c*\n",cnt, p);
		p = p->next;
		cnt++;
	}
	return cnt;
}
List MakeEmpty()
{
		head = (List)malloc(sizeof(struct LNode));   //在這裏申請大小爲struct LNode 的結構空間 
		struct LNode *p=(struct LNode *)malloc(sizeof(struct LNode));
		struct LNode *t=(struct LNode *)malloc(sizeof(struct LNode));
		p = head;
		char alp, k;
		printf("請輸入字母,輸入0爲結束按回車鍵:");//輸入連續的字母串,以0爲結束 
        k = getchar();
		while(true)
		{
			scanf("%c", &alp);
			if(alp=='0')
		    {
	    	p->next = NULL;
	    	break;
		    }
		p->alp = alp;
	    p->next = t;
	    p = t;
	    t = (struct LNode *)malloc(sizeof(struct LNode));
	    //printf("%c", p->alp);
		}	
}            


Position Find(char alp)//元素查找 
{
	struct LNode *t=head;
	Position i=0, cnt = Length();
	while(t->alp!=alp )
	{
		if( i>cnt )
		break;
	    t = t->next;
	    i++;    	
    }
    printf("%d", i);
	return i;
}


bool Insert(int X, char alp) //插入操作
{
	Position j;
	struct LNode *p=(struct LNode *)malloc(sizeof(struct LNode));
	p = head;
	struct LNode *m=(struct LNode *)malloc(sizeof(struct LNode));
/*  for( j=L->Last; j>=X-1; j-- )
	     p->alp[j+1] = p->alp[j];
	p->alp[i-1] = X;
	p->Last++;*/
	for( j = 0; j<=X-2; j++ )
	p = p->next;
	m->next = p->next;
	p->next = m;
	m->alp = alp;
}


bool Delete(int X) //刪除操作
{
	Position j, k;
	struct LNode *u=(struct LNode *)malloc(sizeof(struct LNode));
	struct LNode *o=(struct LNode *)malloc(sizeof(struct LNode));
	u = head;
	/*for( j=i; j<=L->Last; j++ )
	     L->Date[j-1] = L->Date[j];
	L->Last--;*/
	if( X>=2 )
	{
	for( j=0; j<X-2; j++ )
	u = u->next;
	o = u->next;
	u->next = o->next;
	free(o);
	k=1;
	}
	else
	head = u->next;
	if( k=1 )
	printf("刪除成功!");
	else
	printf("刪除失敗!請重試。");
}   


void show()
{
	printf("\n\n");
	printf("                    數字查詢系統          \n");
	printf("            ******************************\n");
	printf("            *       1------建   表       *\n");
	printf("            *       2------插   入       *\n");
	printf("            *       3------刪   除       *\n");
	printf("            *       4------顯   示       *\n");
	printf("            *       5------查   找       *\n");
	printf("            *       6------表   長       *\n");
	printf("            *       0------退   出       *\n");
	printf("            ******************************\n\n");
	printf("請選擇你需要的操作(0-6): ");
}

編譯器:DEV C++





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